Skip to content

Commit

Permalink
Implement (dump) NATS reconnection attempts on startup (#38)
Browse files Browse the repository at this point in the history
fixes #37
  • Loading branch information
David Arnold authored Nov 11, 2020
1 parent 1a170bf commit 9c11fb3
Showing 1 changed file with 15 additions and 0 deletions.
15 changes: 15 additions & 0 deletions server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,14 @@ import (
"github.com/pkg/errors"
)

// DefaultReconnectionAttemts is the default number of reconnection attempts
// It implements a hard coded fault tolerance for a starting NATS cluster
const DefaultNATSReconnectionAttemts = 5

// DefaultNATSReconnectionWait is the default wating time between each reconnection
// attempt
const DefaultNATSReconnectionWait = 5 * time.Second

// Server describes the asterisk-facing ARI proxy server
type Server struct {
// Application is the name of the ARI application of this server
Expand Down Expand Up @@ -73,6 +81,13 @@ func (s *Server) Listen(ctx context.Context, ariOpts *native.Options, natsURI st

// Connect to NATS
nc, err := nats.Connect(natsURI)
reconnectionAttempts := DefaultNATSReconnectionAttemts
for err == nats.ErrNoServers && reconnectionAttempts > 0 {
s.Log.Info("retrying to connect to NATS server", "attempts", reconnectionAttempts)
time.Sleep(DefaultNATSReconnectionWait)
nc, err = nats.Connect(natsURI)
reconnectionAttempts -= 1
}
if err != nil {
return errors.Wrap(err, "failed to connect to NATS")
}
Expand Down

0 comments on commit 9c11fb3

Please sign in to comment.