Skip to content

Instantly share code, notes, and snippets.

@cfsghost
Created June 16, 2020 17:36
Show Gist options
  • Save cfsghost/b505c4aac96d7fa6c1b67b73e1f16dc7 to your computer and use it in GitHub Desktop.
Save cfsghost/b505c4aac96d7fa6c1b67b73e1f16dc7 to your computer and use it in GitHub Desktop.
Reconnect to NATS Streaming
func Connect() error {
var nc *nats.Conn
// Connect to NATS
nc, err := nats.Connect(eb.host,
nats.PingInterval(10*time.Second),
nats.MaxPingsOutstanding(3),
nats.MaxReconnects(-1),
nats.ReconnectHandler(func(natsConn *nats.Conn) {
log.Println("Connected to NATS server")
// Re-connect to NATS Streaming
_Connect(nc)
}),
nats.DisconnectHandler(func(natsConn *nats.Conn) {
log.Println("Disconnected from NATS server")
}),
)
if err != nil {
return err
}
// Connect to NATS Streaming with NATS connection
_Connect(nc)
return nil
}
func _Connect(nc *nats.Conn) (stan.Conn, error) {
return stan.Connect(
"test-cluster",
"myclient",
stan.NatsConn(nc),
)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment