|
|
@ -119,8 +119,9 @@ func (c *Client) Call(ctx context.Context, method string, values url.Values, bod |
|
|
|
resp, err := c.httpClient.Do(req) |
|
|
|
resp, err := c.httpClient.Do(req) |
|
|
|
if err != nil { |
|
|
|
if err != nil { |
|
|
|
if c.HealthCheckFn != nil && xnet.IsNetworkOrHostDown(err, c.ExpectTimeouts) { |
|
|
|
if c.HealthCheckFn != nil && xnet.IsNetworkOrHostDown(err, c.ExpectTimeouts) { |
|
|
|
logger.LogIf(ctx, fmt.Errorf("Marking %s temporary offline; caused by %w", c.url.String(), err)) |
|
|
|
if c.MarkOffline() { |
|
|
|
c.MarkOffline() |
|
|
|
logger.LogIf(ctx, fmt.Errorf("Marking %s temporary offline; caused by %w", c.url.String(), err)) |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
return nil, &NetworkError{err} |
|
|
|
return nil, &NetworkError{err} |
|
|
|
} |
|
|
|
} |
|
|
@ -150,8 +151,9 @@ func (c *Client) Call(ctx context.Context, method string, values url.Values, bod |
|
|
|
b, err := ioutil.ReadAll(io.LimitReader(resp.Body, c.MaxErrResponseSize)) |
|
|
|
b, err := ioutil.ReadAll(io.LimitReader(resp.Body, c.MaxErrResponseSize)) |
|
|
|
if err != nil { |
|
|
|
if err != nil { |
|
|
|
if c.HealthCheckFn != nil && xnet.IsNetworkOrHostDown(err, c.ExpectTimeouts) { |
|
|
|
if c.HealthCheckFn != nil && xnet.IsNetworkOrHostDown(err, c.ExpectTimeouts) { |
|
|
|
logger.LogIf(ctx, fmt.Errorf("Marking %s temporary offline; caused by %w", c.url.String(), err)) |
|
|
|
if c.MarkOffline() { |
|
|
|
c.MarkOffline() |
|
|
|
logger.LogIf(ctx, fmt.Errorf("Marking %s temporary offline; caused by %w", c.url.String(), err)) |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
return nil, err |
|
|
|
return nil, err |
|
|
|
} |
|
|
|
} |
|
|
@ -190,7 +192,8 @@ func (c *Client) IsOnline() bool { |
|
|
|
|
|
|
|
|
|
|
|
// MarkOffline - will mark a client as being offline and spawns
|
|
|
|
// MarkOffline - will mark a client as being offline and spawns
|
|
|
|
// a goroutine that will attempt to reconnect if HealthCheckFn is set.
|
|
|
|
// a goroutine that will attempt to reconnect if HealthCheckFn is set.
|
|
|
|
func (c *Client) MarkOffline() { |
|
|
|
// returns true if the node changed state from online to offline
|
|
|
|
|
|
|
|
func (c *Client) MarkOffline() bool { |
|
|
|
// Start goroutine that will attempt to reconnect.
|
|
|
|
// Start goroutine that will attempt to reconnect.
|
|
|
|
// If server is already trying to reconnect this will have no effect.
|
|
|
|
// If server is already trying to reconnect this will have no effect.
|
|
|
|
if c.HealthCheckFn != nil && atomic.CompareAndSwapInt32(&c.connected, online, offline) { |
|
|
|
if c.HealthCheckFn != nil && atomic.CompareAndSwapInt32(&c.connected, online, offline) { |
|
|
@ -201,12 +204,15 @@ func (c *Client) MarkOffline() { |
|
|
|
return |
|
|
|
return |
|
|
|
} |
|
|
|
} |
|
|
|
if c.HealthCheckFn() { |
|
|
|
if c.HealthCheckFn() { |
|
|
|
atomic.CompareAndSwapInt32(&c.connected, offline, online) |
|
|
|
if atomic.CompareAndSwapInt32(&c.connected, offline, online) { |
|
|
|
logger.Info("Client %s online", c.url.String()) |
|
|
|
logger.Info("Client %s online", c.url.String()) |
|
|
|
|
|
|
|
} |
|
|
|
return |
|
|
|
return |
|
|
|
} |
|
|
|
} |
|
|
|
time.Sleep(time.Duration(r.Float64() * float64(c.HealthCheckInterval))) |
|
|
|
time.Sleep(time.Duration(r.Float64() * float64(c.HealthCheckInterval))) |
|
|
|
} |
|
|
|
} |
|
|
|
}() |
|
|
|
}() |
|
|
|
|
|
|
|
return true |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
return false |
|
|
|
} |
|
|
|
} |
|
|
|