Compare commits
4 Commits
Author | SHA1 | Date | |
---|---|---|---|
|
282b5f5ff0 | ||
|
fabfbfe174 | ||
|
d24c89dc41 | ||
|
d159a8e392 |
40
README.md
40
README.md
@@ -1,2 +1,42 @@
|
||||
# nacl
|
||||
|
||||
Client helper for NATS/STAN
|
||||
|
||||
## examples
|
||||
|
||||
connect to NATS
|
||||
|
||||
```go
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/jar3b/grawt"
|
||||
"github.com/jar3b/nacl"
|
||||
"github.com/kelseyhightower/envconfig"
|
||||
)
|
||||
|
||||
func main() {
|
||||
// get params
|
||||
conf := config.NewConfig()
|
||||
|
||||
// init waiter (github.com/jar3b/grawt)
|
||||
var waiter = grawt.NewWaiter()
|
||||
|
||||
// init nats
|
||||
var subscriptions []*nacl.NatsSubscription
|
||||
if err := nacl.SetupNats(conf.Nats.Host, conf.Nats.Port, conf.Nats.User, conf.Nats.Pass,
|
||||
// handler called before app closed
|
||||
// we need to terminate sub's properly and sometimes doing another actions (finalizers)
|
||||
waiter.AddCloseHandler(func() {
|
||||
nacl.FinalizeNats(&subscriptions)
|
||||
}, false),
|
||||
); err != nil {
|
||||
waiter.Halt(fmt.Errorf("cannot connect to nats: %v", err))
|
||||
}
|
||||
defer nacl.NatsClient.Close()
|
||||
|
||||
// here we add some subscriptions and wait (using blocking call)
|
||||
// ...
|
||||
}
|
||||
```
|
14
go.mod
14
go.mod
@@ -1,11 +1,13 @@
|
||||
module github.com/jar3b/nacl
|
||||
|
||||
go 1.13
|
||||
go 1.15
|
||||
|
||||
require (
|
||||
github.com/golang/protobuf v1.3.3 // indirect
|
||||
github.com/jar3b/grawt v0.1.5
|
||||
github.com/nats-io/nats-streaming-server v0.17.0 // indirect
|
||||
github.com/nats-io/nats.go v1.9.1
|
||||
github.com/nats-io/stan.go v0.6.0
|
||||
github.com/golang/protobuf v1.4.2 // indirect
|
||||
github.com/jar3b/grawt v0.1.6
|
||||
github.com/nats-io/nats-server/v2 v2.1.8 // indirect
|
||||
github.com/nats-io/nats-streaming-server v0.18.0 // indirect
|
||||
github.com/nats-io/nats.go v1.10.0
|
||||
github.com/nats-io/stan.go v0.7.0
|
||||
google.golang.org/protobuf v1.25.0 // indirect
|
||||
)
|
||||
|
20
nacl.go
20
nacl.go
@@ -34,7 +34,9 @@ func SetupNats(host string, port int, user string, pass string, closeHandler *gr
|
||||
NatsClient, err = nats.Connect(
|
||||
connectionString,
|
||||
nats.ClosedHandler(func(conn *nats.Conn) {
|
||||
closeHandler.Halt(nil)
|
||||
if closeHandler != nil {
|
||||
closeHandler.Halt(nil)
|
||||
}
|
||||
}),
|
||||
nats.MaxReconnects(5),
|
||||
nats.ReconnectWait(time.Second*2),
|
||||
@@ -68,8 +70,11 @@ func FinalizeStan(subscriptions *[]Subscription) error {
|
||||
if StanClient == nil {
|
||||
return fmt.Errorf("stan client is not initialized")
|
||||
}
|
||||
for _, subscription := range *subscriptions {
|
||||
_ = subscription.Unsubscribe()
|
||||
|
||||
if subscriptions != nil {
|
||||
for _, subscription := range *subscriptions {
|
||||
_ = subscription.Unsubscribe()
|
||||
}
|
||||
}
|
||||
|
||||
if err := StanClient.Close(); err != nil {
|
||||
@@ -80,11 +85,14 @@ func FinalizeStan(subscriptions *[]Subscription) error {
|
||||
}
|
||||
|
||||
func FinalizeNats(subscriptions *[]*NatsSubscription) error {
|
||||
if StanClient == nil {
|
||||
if NatsClient == nil {
|
||||
return fmt.Errorf("stan client is not initialized")
|
||||
}
|
||||
for _, subscription := range *subscriptions {
|
||||
_ = subscription.Unsubscribe()
|
||||
|
||||
if subscriptions != nil {
|
||||
for _, subscription := range *subscriptions {
|
||||
_ = subscription.Unsubscribe()
|
||||
}
|
||||
}
|
||||
|
||||
NatsClient.Close()
|
||||
|
Reference in New Issue
Block a user