4
4
package nats
5
5
6
6
import (
7
+ "fmt"
7
8
"log"
8
9
"sync"
9
10
"time"
@@ -13,30 +14,41 @@ import (
13
14
)
14
15
15
16
const queueGroup = "openfaas_nats_worker_group"
17
+
16
18
const clientName = "openfaas_connector"
17
19
18
20
// BrokerConfig high level config for the broker
19
21
type BrokerConfig struct {
20
- Host string
22
+
23
+ // Host is the NATS address, the port is hard-coded to 4222
24
+ Host string
25
+
26
+ // ConnTimeout is the timeout for Dial on a connection.
21
27
ConnTimeout time.Duration
22
28
}
23
29
24
30
// Broker used to subscribe to NATS subjects
25
31
type Broker interface {
26
- Subscribe (types.Controller , []string )
32
+ Subscribe (types.Controller , []string ) error
27
33
}
28
34
29
35
type broker struct {
30
36
client * nats.Conn
31
37
}
32
38
39
+ // NATSPort hard-coded port for NATS
40
+ const NATSPort = "4222"
41
+
33
42
// NewBroker loops until we are able to connect to the NATS server
34
- func NewBroker (config BrokerConfig ) Broker {
43
+ func NewBroker (config BrokerConfig ) ( Broker , error ) {
35
44
broker := & broker {}
45
+ brokerURL := fmt .Sprintf ("nats://%s:%s" , config .Host , NATSPort )
36
46
37
- brokerURL := "nats://" + config .Host + ":4222"
38
47
for {
39
- client , err := nats .Connect (brokerURL , nats .Timeout (config .ConnTimeout ), nats .Name (clientName ))
48
+ client , err := nats .Connect (brokerURL ,
49
+ nats .Timeout (config .ConnTimeout ),
50
+ nats .Name (clientName ))
51
+
40
52
if client != nil && err == nil {
41
53
broker .client = client
42
54
break
@@ -45,30 +57,50 @@ func NewBroker(config BrokerConfig) Broker {
45
57
if client != nil {
46
58
client .Close ()
47
59
}
60
+
48
61
log .Println ("Wait for brokers to come up.. " , brokerURL )
49
62
time .Sleep (1 * time .Second )
50
63
// TODO Add healthcheck
51
64
}
52
- return broker
65
+
66
+ return broker , nil
53
67
}
54
68
55
69
// Subscribe to a list of NATS subjects and block until interrupted
56
- func (b * broker ) Subscribe (controller types.Controller , topics []string ) {
70
+ func (b * broker ) Subscribe (controller types.Controller , topics []string ) error {
57
71
log .Printf ("Configured topics: %v" , topics )
58
72
73
+ if b .client == nil {
74
+ return fmt .Errorf ("client was nil, try to reconnect" )
75
+ }
76
+
59
77
wg := sync.WaitGroup {}
60
78
wg .Add (1 )
61
79
80
+ subs := []* nats.Subscription {}
62
81
for _ , topic := range topics {
63
- log .Printf ("Binding to topic: %v" , topic )
64
- // check client not nil
65
- b .client .QueueSubscribe (topic , queueGroup , func (m * nats.Msg ) {
66
- log .Printf ("Received topic: %s, message: %s" , m .Subject , string (m .Data ))
82
+ log .Printf ("Binding to topic: %q" , topic )
83
+
84
+ sub , err := b .client .QueueSubscribe (topic , queueGroup , func (m * nats.Msg ) {
85
+ log .Printf ("Topic: %s, message: %q" , m .Subject , string (m .Data ))
86
+
67
87
controller .Invoke (m .Subject , & m .Data )
68
88
})
89
+ subs = append (subs , sub )
90
+
91
+ if err != nil {
92
+ log .Printf ("Unable to bind to topic: %s" , topic )
93
+ }
94
+ }
95
+
96
+ for _ , sub := range subs {
97
+ log .Printf ("Subscription: %s ready" , sub .Subject )
69
98
}
70
99
71
100
// interrupt handling
72
101
wg .Wait ()
102
+
73
103
b .client .Close ()
104
+
105
+ return nil
74
106
}
0 commit comments