Skip to content

Commit 98e1110

Browse files
committed
Make NATS more verbose
Signed-off-by: Alex Ellis (OpenFaaS Ltd) <[email protected]>
1 parent 8831593 commit 98e1110

File tree

2 files changed

+56
-14
lines changed

2 files changed

+56
-14
lines changed

main.go

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
package main
55

66
import (
7+
"log"
8+
79
"github.com/openfaas-incubator/connector-sdk/types"
810
"github.com/openfaas-incubator/nats-connector/config"
911
"github.com/openfaas-incubator/nats-connector/nats"
@@ -29,9 +31,17 @@ func main() {
2931

3032
brokerConfig := nats.BrokerConfig{
3133
Host: config.Broker,
32-
ConnTimeout: config.UpstreamTimeout,
34+
ConnTimeout: config.UpstreamTimeout, // ConnTimeout isn't the same as UpstreamTimeout, it's just the delay to connect to NATS.
3335
}
3436

35-
broker := nats.NewBroker(brokerConfig)
36-
broker.Subscribe(controller, config.Topics)
37+
broker, err := nats.NewBroker(brokerConfig)
38+
39+
if err != nil {
40+
log.Fatal(err)
41+
}
42+
43+
err = broker.Subscribe(controller, config.Topics)
44+
if err != nil {
45+
log.Fatal(err)
46+
}
3747
}

nats/broker.go

Lines changed: 43 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
package nats
55

66
import (
7+
"fmt"
78
"log"
89
"sync"
910
"time"
@@ -13,30 +14,41 @@ import (
1314
)
1415

1516
const queueGroup = "openfaas_nats_worker_group"
17+
1618
const clientName = "openfaas_connector"
1719

1820
// BrokerConfig high level config for the broker
1921
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.
2127
ConnTimeout time.Duration
2228
}
2329

2430
// Broker used to subscribe to NATS subjects
2531
type Broker interface {
26-
Subscribe(types.Controller, []string)
32+
Subscribe(types.Controller, []string) error
2733
}
2834

2935
type broker struct {
3036
client *nats.Conn
3137
}
3238

39+
// NATSPort hard-coded port for NATS
40+
const NATSPort = "4222"
41+
3342
// 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) {
3544
broker := &broker{}
45+
brokerURL := fmt.Sprintf("nats://%s:%s", config.Host, NATSPort)
3646

37-
brokerURL := "nats://" + config.Host + ":4222"
3847
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+
4052
if client != nil && err == nil {
4153
broker.client = client
4254
break
@@ -45,30 +57,50 @@ func NewBroker(config BrokerConfig) Broker {
4557
if client != nil {
4658
client.Close()
4759
}
60+
4861
log.Println("Wait for brokers to come up.. ", brokerURL)
4962
time.Sleep(1 * time.Second)
5063
// TODO Add healthcheck
5164
}
52-
return broker
65+
66+
return broker, nil
5367
}
5468

5569
// 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 {
5771
log.Printf("Configured topics: %v", topics)
5872

73+
if b.client == nil {
74+
return fmt.Errorf("client was nil, try to reconnect")
75+
}
76+
5977
wg := sync.WaitGroup{}
6078
wg.Add(1)
6179

80+
subs := []*nats.Subscription{}
6281
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+
6787
controller.Invoke(m.Subject, &m.Data)
6888
})
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)
6998
}
7099

71100
// interrupt handling
72101
wg.Wait()
102+
73103
b.client.Close()
104+
105+
return nil
74106
}

0 commit comments

Comments
 (0)