-
Notifications
You must be signed in to change notification settings - Fork 0
/
connection.go
73 lines (63 loc) · 1.59 KB
/
connection.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
package main
import (
"fmt"
"os"
"strconv"
"sync"
"time"
"github.com/apache/pulsar-client-go/pulsar"
"github.com/joho/godotenv"
"github.com/rs/zerolog/log"
)
var (
_ = godotenv.Load()
brokers = os.Getenv("PULSAR_BROKERS")
opTimeout = os.Getenv("PULSAR_OPERATION_TIMEOUT_SEC")
clientTimeout = os.Getenv("PULSAR_CLIENT_TIMEOUT_SEC")
)
// Connection instance
type Connection struct {
//lock *sync.Cond
sync.Mutex
con pulsar.Client
}
// NewConnection create a new connection instance
func NewConnection() *Connection {
conn := new(Connection)
//conn.lock = sync.NewCond(&sync.Mutex{})
conn.Connect()
return conn
}
// Get retrieve pulsar client instance
func (c *Connection) Get() pulsar.Client {
return c.con
}
// Close drops pulsar connection
func (c *Connection) Close() {
c.con.Close()
}
// Connect connect pulsar client
func (c *Connection) Connect() {
c.Lock()
defer c.Unlock()
clientOpt := pulsar.ClientOptions{
ConnectionMaxIdleTime: -1,
MaxConnectionsPerBroker: 50,
EnableTransaction: true,
KeepAliveInterval: time.Second * 5,
URL: fmt.Sprintf("pulsar://%s", brokers),
}
if operationTimeout, err := strconv.
ParseInt(opTimeout, 10, 64); err != nil {
clientOpt.OperationTimeout = time.Duration(operationTimeout) * time.Second
}
if cTimeout, err := strconv.
ParseInt(clientTimeout, 10, 64); err != nil {
clientOpt.ConnectionTimeout = time.Duration(cTimeout) * time.Second
}
client, err := pulsar.NewClient(clientOpt)
if err != nil {
log.Fatal().Err(err).Msg("failed to create pulsar client")
}
c.con = client
}