Skip to content

Commit 1d502ec

Browse files
committed
update nats version and cleanup config
1 parent 4dd3ade commit 1d502ec

File tree

5 files changed

+58
-44
lines changed

5 files changed

+58
-44
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: 1 addition & 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,7 @@ 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/go.mod h1:dYqB+vMN3C2F9pT1FRQpg9eHbjPj6mP0yYuyBNuXHZE=
104103
github.com/opentracing/opentracing-go v1.1.0 h1:pWlfV3Bxv7k65HYwkikxat0+s3pV4bsqf19k25Ur8rU=
105104
github.com/opentracing/opentracing-go v1.1.0/go.mod h1:UkNAQd3GIcIGf0SeVgPpRdFStlNbqXla1AfSYxPUl2o=
106105
github.com/pascaldekloe/goe v0.1.0 h1:cBOtyMzM9HTpWjXfbbunk26uA6nG3a8n06Wieeh0MwY=

messaging/config.go

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

messaging/nats.go

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

10-
"github.com/nats-io/go-nats"
11-
stan "github.com/nats-io/go-nats-streaming"
10+
"github.com/nats-io/nats.go"
11+
"github.com/nats-io/stan.go"
1212
"github.com/netlify/netlify-commons/nconf"
1313
"github.com/pkg/errors"
1414
"github.com/sirupsen/logrus"

nconf/nats.go

Lines changed: 52 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,12 @@ package nconf
33
import (
44
"fmt"
55
"strings"
6+
"time"
7+
8+
"github.com/pkg/errors"
9+
10+
"github.com/nats-io/stan.go"
11+
"github.com/nats-io/stan.go/pb"
612

713
"github.com/netlify/netlify-commons/discovery"
814
"github.com/sirupsen/logrus"
@@ -33,6 +39,20 @@ type NatsConfig struct {
3339
StartPos string `mapstructure:"start_pos" split_words:"true"`
3440
}
3541

42+
type NatsClientConfig struct {
43+
NatsConfig
44+
Subject string `mapstructure:"command_subject"`
45+
Group string `mapstructure:"command_group"`
46+
47+
// StartAt will configure where the client should resume the stream:
48+
// - `all`: all the messages available
49+
// - `last`: from where the client left off
50+
// - `new`: all new messages for the client
51+
// - `first`: from the first message available (default)
52+
// - 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)
53+
StartAt string `mapstructure:"start_at"`
54+
}
55+
3656
func (c *NatsConfig) LoadServerNames() error {
3757
if c.DiscoveryName == "" {
3858
return nil
@@ -53,29 +73,48 @@ func (c *NatsConfig) LoadServerNames() error {
5373
}
5474

5575
// ServerString will build the proper string for nats connect
56-
func (config *NatsConfig) ServerString() string {
57-
return strings.Join(config.Servers, ",")
76+
func (c *NatsConfig) ServerString() string {
77+
return strings.Join(c.Servers, ",")
5878
}
5979

60-
func (config *NatsConfig) Fields() logrus.Fields {
80+
func (c *NatsConfig) Fields() logrus.Fields {
6181
f := logrus.Fields{
62-
"servers": strings.Join(config.Servers, ","),
82+
"servers": strings.Join(c.Servers, ","),
6383
}
6484

65-
if config.Auth.Method != "" {
66-
f["auth_method"] = config.Auth.Method
85+
if c.Auth.Method != "" {
86+
f["auth_method"] = c.Auth.Method
6787
}
6888

69-
if config.TLS != nil {
70-
f["ca_files"] = strings.Join(config.TLS.CAFiles, ",")
71-
f["key_file"] = config.TLS.KeyFile
72-
f["cert_file"] = config.TLS.CertFile
89+
if c.TLS != nil {
90+
f["ca_files"] = strings.Join(c.TLS.CAFiles, ",")
91+
f["key_file"] = c.TLS.KeyFile
92+
f["cert_file"] = c.TLS.CertFile
7393
}
7494

75-
if config.ClusterID != "" {
76-
f["client_id"] = config.ClientID
77-
f["cluster_id"] = config.ClusterID
95+
if c.ClusterID != "" {
96+
f["client_id"] = c.ClientID
97+
f["cluster_id"] = c.ClusterID
7898
}
7999

80100
return f
81101
}
102+
103+
func (c *NatsConfig) StartPoint() (stan.SubscriptionOption, error) {
104+
switch v := strings.ToLower(c.StartPos); v {
105+
case "all":
106+
return stan.DeliverAllAvailable(), nil
107+
case "last":
108+
return stan.StartWithLastReceived(), nil
109+
case "new":
110+
return stan.StartAt(pb.StartPosition_NewOnly), nil
111+
case "", "first":
112+
return stan.StartAt(pb.StartPosition_First), nil
113+
default:
114+
dur, err := time.ParseDuration(v)
115+
if err != nil {
116+
return nil, errors.Wrap(err, "Failed to parse field as a duration")
117+
}
118+
return stan.StartAtTimeDelta(dur), nil
119+
}
120+
}

0 commit comments

Comments
 (0)