cycle through dial targets regardless of error

This commit is contained in:
Travis 2020-04-08 12:29:15 -05:00
parent adb21589f0
commit 8f0ae1b6f5

View file

@ -80,14 +80,24 @@ func (c *GRPCClient) resetConn() error {
opts = append(opts, grpc.WithDefaultCallOptions(grpc.MaxCallRecvMsgSize(maxMsgSize)))
var err error
if c.conn, err = grpc.Dial(c.dialTargets[c.targetIndex], opts...); err != nil {
c.targetIndex = (c.targetIndex + 1) % len(c.dialTargets) // cycle through dialTargets
if c.conn, err = grpc.Dial(c.dialTargets[c.getTargetIndex()], opts...); err != nil {
return errors.Wrap(err, "creating new grpc client")
}
return nil
}
// getTargetIndex gets the current target index, then increments it for
// next time. Unprotected.
func (c *GRPCClient) getTargetIndex() int {
if len(c.dialTargets) == 0 {
return 0
}
ret := c.targetIndex
c.targetIndex = (c.targetIndex + 1) % len(c.dialTargets) // cycle through dialTargets
return ret
}
// Close closes any connections the client has opened.
func (c *GRPCClient) Close() error {
c.mu.RLock()