Skip to content

Commit 765f492

Browse files
authored
Merge pull request #87 from netlify/upgrade-nats
update nats version and cleanup config
2 parents 9bad597 + 4051881 commit 765f492

File tree

5 files changed

+117
-101
lines changed

5 files changed

+117
-101
lines changed

go.mod

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,11 @@ require (
1515
github.com/konsorten/go-windows-terminal-sequences v1.0.2 // indirect
1616
github.com/kr/pretty v0.1.0 // indirect
1717
github.com/nats-io/gnatsd v1.4.1 // indirect
18-
github.com/nats-io/go-nats v1.3.0
19-
github.com/nats-io/go-nats-streaming v0.3.4
18+
github.com/nats-io/go-nats v1.3.0 // indirect
2019
github.com/nats-io/nats-server v1.4.1 // indirect
2120
github.com/nats-io/nats-streaming-server v0.15.1 // indirect
21+
github.com/nats-io/nats.go v1.8.1
22+
github.com/nats-io/stan.go v0.5.0
2223
github.com/opentracing/opentracing-go v1.1.0
2324
github.com/pelletier/go-toml v1.3.0 // indirect
2425
github.com/philhofer/fwd v1.0.0 // indirect

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,8 +83,6 @@ github.com/nats-io/gnatsd v1.4.1 h1:RconcfDeWpKCD6QIIwiVFcvForlXpWeJP7i5/lDLy44=
8383
github.com/nats-io/gnatsd v1.4.1/go.mod h1:nqco77VO78hLCJpIcVfygDP2rPGfsEHkGTUk94uh5DQ=
8484
github.com/nats-io/go-nats v1.3.0 h1:CrvnAwoB2A2Yma+PcM+5tC++3/wswhcy8OvzqbsUXZQ=
8585
github.com/nats-io/go-nats v1.3.0/go.mod h1:+t7RHT5ApZebkrQdnn6AhQJmhJJiKAvJUio1PiiCtj0=
86-
github.com/nats-io/go-nats-streaming v0.3.4 h1:4z1stoQQfetddeodZ4huO24LK0QY75Mg4r9037J90MI=
87-
github.com/nats-io/go-nats-streaming v0.3.4/go.mod h1:gfq4R3c9sKAINOpelo0gn/b9QDMBZnmrttcsNF+lqyo=
8886
github.com/nats-io/jwt v0.2.6 h1:eAyoYvGgGLXR2EpnsBUvi/FcFrBqN6YKFVbOoEfPN4k=
8987
github.com/nats-io/jwt v0.2.6/go.mod h1:mQxQ0uHQ9FhEVPIcTSKwx2lqZEpXWWcCgA7R6NrWvvY=
9088
github.com/nats-io/nats-server v1.4.1 h1:Ul1oSOGNV/L8kjr4v6l2f9Yet6WY+LevH1/7cRZ/qyA=
@@ -101,6 +99,8 @@ github.com/nats-io/nuid v1.0.1 h1:5iA8DT8V7q8WK2EScv2padNa/rTESc1KdnPw4TC2paw=
10199
github.com/nats-io/nuid v1.0.1/go.mod h1:19wcPz3Ph3q0Jbyiqsd0kePYG7A95tJPxeL+1OSON2c=
102100
github.com/nats-io/stan.go v0.4.5 h1:lPZ9y1jVGiXcTaUc1SnEIWPYfh0avuEiHBePNJYgpPk=
103101
github.com/nats-io/stan.go v0.4.5/go.mod h1:Ji7mK6gRZJSH1nc3ZJH6vi7zn/QnZhpR9Arm4iuzsUQ=
102+
github.com/nats-io/stan.go v0.5.0 h1:ZaSPMb6jnDXsSlOACynJrUiB3Evleg3ZyyX+rnf3TlQ=
103+
github.com/nats-io/stan.go v0.5.0/go.mod h1:dYqB+vMN3C2F9pT1FRQpg9eHbjPj6mP0yYuyBNuXHZE=
104104
github.com/opentracing/opentracing-go v1.1.0 h1:pWlfV3Bxv7k65HYwkikxat0+s3pV4bsqf19k25Ur8rU=
105105
github.com/opentracing/opentracing-go v1.1.0/go.mod h1:UkNAQd3GIcIGf0SeVgPpRdFStlNbqXla1AfSYxPUl2o=
106106
github.com/pascaldekloe/goe v0.1.0 h1:cBOtyMzM9HTpWjXfbbunk26uA6nG3a8n06Wieeh0MwY=

messaging/config.go

Lines changed: 103 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,107 @@ package messaging
33
import (
44
"fmt"
55
"strings"
6-
7-
stan "github.com/nats-io/go-nats-streaming"
8-
"github.com/nats-io/go-nats-streaming/pb"
6+
"time"
97

108
"github.com/netlify/netlify-commons/nconf"
9+
10+
"github.com/pkg/errors"
11+
12+
"github.com/nats-io/stan.go"
13+
"github.com/nats-io/stan.go/pb"
14+
15+
"github.com/netlify/netlify-commons/discovery"
16+
"github.com/sirupsen/logrus"
1117
)
1218

13-
func StartPoint(config *nconf.NatsConfig) (stan.SubscriptionOption, error) {
14-
switch v := strings.ToLower(config.StartPos); v {
19+
const (
20+
NatsAuthMethodUser = "user"
21+
NatsAuthMethodToken = "token"
22+
NatsAuthMethodTLS = "tls"
23+
)
24+
25+
type NatsAuth struct {
26+
Method string `mapstructure:"method"`
27+
User string `mapstructure:"user"`
28+
Password string `mapstructure:"password"`
29+
Token string `mapstructure:"token"`
30+
}
31+
32+
type NatsConfig struct {
33+
TLS *nconf.TLSConfig `mapstructure:"tls_conf"`
34+
DiscoveryName string `mapstructure:"discovery_name" split_words:"true"`
35+
Servers []string `mapstructure:"servers"`
36+
Auth NatsAuth `mapstructure:"auth"`
37+
38+
// for streaming
39+
ClusterID string `mapstructure:"cluster_id" split_words:"true"`
40+
ClientID string `mapstructure:"client_id" split_words:"true"`
41+
StartPos string `mapstructure:"start_pos" split_words:"true"`
42+
}
43+
44+
type NatsClientConfig struct {
45+
NatsConfig
46+
Subject string `mapstructure:"command_subject"`
47+
Group string `mapstructure:"command_group"`
48+
49+
// StartAt will configure where the client should resume the stream:
50+
// - `all`: all the messages available
51+
// - `last`: from where the client left off
52+
// - `new`: all new messages for the client
53+
// - `first`: from the first message available (default)
54+
// - other: if it isn't one of the above fields, it will try and parse the param as a go duration (e.g. 30s, 1h)
55+
StartAt string `mapstructure:"start_at"`
56+
}
57+
58+
func (c *NatsConfig) LoadServerNames() error {
59+
if c.DiscoveryName == "" {
60+
return nil
61+
}
62+
63+
natsURLs := []string{}
64+
endpoints, err := discovery.DiscoverEndpoints(c.DiscoveryName)
65+
if err != nil {
66+
return err
67+
}
68+
69+
for _, endpoint := range endpoints {
70+
natsURLs = append(natsURLs, fmt.Sprintf("nats://%s:%d", endpoint.Target, endpoint.Port))
71+
}
72+
73+
c.Servers = natsURLs
74+
return nil
75+
}
76+
77+
// ServerString will build the proper string for nats connect
78+
func (c *NatsConfig) ServerString() string {
79+
return strings.Join(c.Servers, ",")
80+
}
81+
82+
func (c *NatsConfig) Fields() logrus.Fields {
83+
f := logrus.Fields{
84+
"servers": strings.Join(c.Servers, ","),
85+
}
86+
87+
if c.Auth.Method != "" {
88+
f["auth_method"] = c.Auth.Method
89+
}
90+
91+
if c.TLS != nil {
92+
f["ca_files"] = strings.Join(c.TLS.CAFiles, ",")
93+
f["key_file"] = c.TLS.KeyFile
94+
f["cert_file"] = c.TLS.CertFile
95+
}
96+
97+
if c.ClusterID != "" {
98+
f["client_id"] = c.ClientID
99+
f["cluster_id"] = c.ClusterID
100+
}
101+
102+
return f
103+
}
104+
105+
func (c *NatsConfig) StartPoint() (stan.SubscriptionOption, error) {
106+
switch v := strings.ToLower(c.StartPos); v {
15107
case "all":
16108
return stan.DeliverAllAvailable(), nil
17109
case "last":
@@ -20,6 +112,11 @@ func StartPoint(config *nconf.NatsConfig) (stan.SubscriptionOption, error) {
20112
return stan.StartAt(pb.StartPosition_NewOnly), nil
21113
case "", "first":
22114
return stan.StartAt(pb.StartPosition_First), nil
115+
default:
116+
dur, err := time.ParseDuration(v)
117+
if err != nil {
118+
return nil, errors.Wrap(err, "Failed to parse field as a duration")
119+
}
120+
return stan.StartAtTimeDelta(dur), nil
23121
}
24-
return nil, fmt.Errorf("Unknown start position '%s', possible values are all, last, new, first and ''", config.StartPos)
25122
}

messaging/nats.go

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,8 @@ import (
77
"strings"
88
"time"
99

10-
"github.com/nats-io/go-nats"
11-
stan "github.com/nats-io/go-nats-streaming"
12-
"github.com/netlify/netlify-commons/nconf"
10+
"github.com/nats-io/nats.go"
11+
"github.com/nats-io/stan.go"
1312
"github.com/pkg/errors"
1413
"github.com/sirupsen/logrus"
1514
)
@@ -22,7 +21,7 @@ func init() {
2221
silent = logrus.NewEntry(l)
2322
}
2423

25-
func ConfigureNatsConnection(config *nconf.NatsConfig, log logrus.FieldLogger) (*nats.Conn, error) {
24+
func ConfigureNatsConnection(config *NatsConfig, log logrus.FieldLogger) (*nats.Conn, error) {
2625
if log == nil {
2726
log = silent
2827
}
@@ -49,7 +48,7 @@ func ConfigureNatsConnection(config *nconf.NatsConfig, log logrus.FieldLogger) (
4948
return nc, nil
5049
}
5150

52-
func ConnectToNats(config *nconf.NatsConfig, opts ...nats.Option) (*nats.Conn, error) {
51+
func ConnectToNats(config *NatsConfig, opts ...nats.Option) (*nats.Conn, error) {
5352
tlsConfig, err := config.TLS.TLSConfig()
5453
if err != nil {
5554
return nil, errors.Wrap(err, "Failed to configure TLS")
@@ -61,11 +60,11 @@ func ConnectToNats(config *nconf.NatsConfig, opts ...nats.Option) (*nats.Conn, e
6160
}
6261

6362
switch strings.ToLower(config.Auth.Method) {
64-
case nconf.NatsAuthMethodUser:
63+
case NatsAuthMethodUser:
6564
opts = append(opts, nats.UserInfo(config.Auth.User, config.Auth.Password))
66-
case nconf.NatsAuthMethodToken:
65+
case NatsAuthMethodToken:
6766
opts = append(opts, nats.Token(config.Auth.Token))
68-
case nconf.NatsAuthMethodTLS:
67+
case NatsAuthMethodTLS:
6968
// if using TLS auth, make sure the client certificate is loaded
7069
if tlsConfig == nil || len(tlsConfig.Certificates) == 0 {
7170
return nil, fmt.Errorf("TLS auth method is configured but no certificate was loaded")
@@ -78,7 +77,7 @@ func ConnectToNats(config *nconf.NatsConfig, opts ...nats.Option) (*nats.Conn, e
7877
return nats.Connect(config.ServerString(), opts...)
7978
}
8079

81-
func ConfigureNatsStreaming(config *nconf.NatsConfig, log logrus.FieldLogger) (stan.Conn, error) {
80+
func ConfigureNatsStreaming(config *NatsConfig, log logrus.FieldLogger) (stan.Conn, error) {
8281
// connect to the underlying instance
8382
nc, err := ConfigureNatsConnection(config, log)
8483
if err != nil {
@@ -93,7 +92,7 @@ func ConfigureNatsStreaming(config *nconf.NatsConfig, log logrus.FieldLogger) (s
9392
return conn, nil
9493
}
9594

96-
func ConnectToNatsStreaming(nc *nats.Conn, config *nconf.NatsConfig, log logrus.FieldLogger) (stan.Conn, error) {
95+
func ConnectToNatsStreaming(nc *nats.Conn, config *NatsConfig, log logrus.FieldLogger) (stan.Conn, error) {
9796
if config.ClusterID == "" {
9897
return nil, errors.New("Must provide a cluster ID to connect to streaming nats")
9998
}

nconf/nats.go

Lines changed: 0 additions & 81 deletions
This file was deleted.

0 commit comments

Comments
 (0)