Created
June 16, 2020 17:36
-
-
Save cfsghost/b505c4aac96d7fa6c1b67b73e1f16dc7 to your computer and use it in GitHub Desktop.
Reconnect to NATS Streaming
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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