Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement (dump) NATS reconnection attempts on startup #38

Merged
merged 1 commit into from
Nov 11, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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