-
Notifications
You must be signed in to change notification settings - Fork 21
/
connection.go
63 lines (51 loc) · 1.83 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
package nats
import (
"github.com/nats-io/nats.go"
)
type Connection interface {
// QueueSubscribe subscribes to a NATS subject, equivalent to default Subscribe if queuegroup not supplied.
QueueSubscribe(string, string, nats.MsgHandler) (*nats.Subscription, error)
// PublishMsg sends the provided NATS message to the broker.
PublishMsg(*nats.Msg) error
// Drain will end all active subscription interest and attempt to wait for in-flight messages to process before closing.
Drain() error
// Close will close the connection
Close()
}
// jsConnection mimics the core NATS publish/subscribe API
// so that NATS and jetstream can use the same client orchestration.
type jsConnection struct {
conn *nats.Conn
js nats.JetStreamContext
cfg JetStreamConfig
}
// Subscribe subscribes to a JetStream subject
func (j jsConnection) QueueSubscribe(s string, q string, handler nats.MsgHandler) (*nats.Subscription, error) {
opts := j.cfg.SubscribeOptions
if durable := j.cfg.CalculateDurableName(s); durable != "" {
opts = append(opts, nats.Durable(durable))
} else {
// find & bind stream based on subscription subject
opts = append(opts, nats.BindStream(""))
}
return j.js.QueueSubscribe(s, q, handler, opts...)
}
// PublishMsg publishes a message to JetStream
func (j jsConnection) PublishMsg(msg *nats.Msg) (err error) {
publishOpts := j.cfg.PublishOptions
if j.cfg.TrackMsgId {
if msgID := msg.Header.Get(WatermillUUIDHdr); msgID != "" {
publishOpts = append(publishOpts, nats.MsgId(msgID))
}
}
_, err = j.js.PublishMsg(msg, publishOpts...)
return
}
// Drain will remove all subscription interest and attempt to wait until all messages have finished processing to close and return.
func (j jsConnection) Drain() error {
return j.conn.Drain()
}
// Close will close the connection
func (j jsConnection) Close() {
j.conn.Close()
}