Skip to content
This repository has been archived by the owner on Jul 21, 2023. It is now read-only.

Commit

Permalink
Fixed inifinite retrylogic
Browse files Browse the repository at this point in the history
  • Loading branch information
dylanratcliffe committed May 30, 2022
1 parent b6592a4 commit 70e9b8f
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 7 deletions.
12 changes: 12 additions & 0 deletions common.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,18 @@ type CommonOptions struct {
RetryDelay time.Duration // Delay between connection attempts
}

// TotalTries Returns the total number of times we shoyuld try to connect,
// including the first try. For positive numbers this is retries + 1, for
// negative we just leave it alone
func (c CommonOptions) TotalTries() int {
if c.NumRetries >= 0 {
return c.NumRetries + 1
} else {
return c.NumRetries
}

}

// ConnectionOptions Options for connecting to each service, if these are nil,
// the service won't be connected
type ConnectionOptions struct {
Expand Down
6 changes: 3 additions & 3 deletions dgraph.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,13 @@ type DGraphConnectionOptions struct {

func (d DGraphConnectionOptions) Connect() (*dgo.Dgraph, error) {
var dGraphClient *dgo.Dgraph
retriesLeft := d.NumRetries
tries := d.TotalTries()

if d.ConnectionTimeout == 0 {
d.ConnectionTimeout = ConnectionTimeoutDefault
}

for retriesLeft != 0 {
for tries != 0 {
connections := make([]*grpc.ClientConn, 0)

for _, server := range d.Servers {
Expand Down Expand Up @@ -71,7 +71,7 @@ func (d DGraphConnectionOptions) Connect() (*dgo.Dgraph, error) {
"servers": d.Servers,
}).Error("Could not connect to any DGraph endpoints")

retriesLeft--
tries--
time.Sleep(d.RetryDelay)
continue
}
Expand Down
38 changes: 38 additions & 0 deletions dgraph_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,44 @@ func TestDGraphConnect(t *testing.T) {

ValidateDGraphConnection(t, client)
})

t.Run("with a good URL and defaults", func(t *testing.T) {
o := DGraphConnectionOptions{
Servers: []string{
"dgraph:9080",
"localhost:9080",
},
}

client, err := o.Connect()

if err != nil {
t.Error(err)
}

ValidateDGraphConnection(t, client)
})

t.Run("with a good URL and infinite retrues", func(t *testing.T) {
o := DGraphConnectionOptions{
Servers: []string{
"dgraph:9080",
"localhost:9080",
},
CommonOptions: CommonOptions{
NumRetries: -1,
RetryDelay: 100 * time.Millisecond,
},
}

client, err := o.Connect()

if err != nil {
t.Error(err)
}

ValidateDGraphConnection(t, client)
})
}

func ValidateDGraphConnection(t *testing.T, c *dgo.Dgraph) {
Expand Down
8 changes: 4 additions & 4 deletions nats.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,15 +142,15 @@ func (o NATSConnectionOptions) ToNatsOptions() (string, []nats.Option) {
// unavailable
func (o NATSConnectionOptions) Connect() (*nats.EncodedConn, error) {
servers, opts := o.ToNatsOptions()
retriesLeft := o.NumRetries + 1
tries := o.TotalTries()

var nc *nats.Conn
var enc *nats.EncodedConn
var err error

nats.RegisterEncoder("sdp", &sdp.ENCODER)

for retriesLeft != 0 {
for tries != 0 {
log.WithFields(log.Fields{
"servers": servers,
}).Info("NATS connecting")
Expand All @@ -165,7 +165,7 @@ func (o NATSConnectionOptions) Connect() (*nats.EncodedConn, error) {
"error": err.Error(),
}).Error("Error connecting to NATS")

retriesLeft--
tries--
time.Sleep(o.RetryDelay)
continue
}
Expand All @@ -177,7 +177,7 @@ func (o NATSConnectionOptions) Connect() (*nats.EncodedConn, error) {
"error": err.Error(),
}).Error("Error creating encoded connection")

retriesLeft--
tries--
time.Sleep(o.RetryDelay)
continue
}
Expand Down
21 changes: 21 additions & 0 deletions nats_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,27 @@ func TestNATSConnect(t *testing.T) {

ValidateNATSConnection(t, conn)
})

t.Run("with a good URL and infinite retries", func(t *testing.T) {
o := NATSConnectionOptions{
Servers: []string{
"nats://nats:4222",
"nats://localhost:4223",
},
CommonOptions: CommonOptions{
NumRetries: -1,
RetryDelay: 100 * time.Millisecond,
},
}

conn, err := o.Connect()

if err != nil {
t.Error(err)
}

ValidateNATSConnection(t, conn)
})
}

func ValidateNATSConnection(t *testing.T, enc *nats.EncodedConn) {
Expand Down

0 comments on commit 70e9b8f

Please sign in to comment.