record err time when remote node is offline (#17262)

This commit is contained in:
jiuker 2023-05-31 01:07:26 +08:00 committed by GitHub
parent 6fe028b7c5
commit fb5ce3b87a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -99,6 +99,7 @@ type Client struct {
sync.RWMutex // mutex for lastErr
lastErr error
lastErrTime time.Time
}
type restError string
@ -339,7 +340,7 @@ func (c *Client) LastConn() time.Time {
func (c *Client) LastError() error {
c.RLock()
defer c.RUnlock()
return c.lastErr
return fmt.Errorf("[%s] %w", c.lastErrTime.Format(time.RFC3339), c.lastErr)
}
// computes the exponential backoff duration according to
@ -370,6 +371,7 @@ func exponentialBackoffWait(r *rand.Rand, unit, cap time.Duration) func(uint) ti
func (c *Client) MarkOffline(err error) bool {
c.Lock()
c.lastErr = err
c.lastErrTime = time.Now()
c.Unlock()
// Start goroutine that will attempt to reconnect.
// If server is already trying to reconnect this will have no effect.