Skip to content
Open
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
23 changes: 22 additions & 1 deletion memcache/memcache.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,8 @@ const (
// DefaultMaxIdleConns is the default maximum number of idle connections
// kept for any single address.
DefaultMaxIdleConns = 2
// maximum number of times to attempt to reconnect
maxRetries = 10

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @QuChen88 ! Could this setting be exportable/settable as an option by the lib user? Thank you!

)

const buffered = 8 // arbitrary buffered channel size, for readability
Expand Down Expand Up @@ -370,7 +372,26 @@ func (c *Client) withAddrRw(addr net.Addr, fn func(*conn) error) (err error) {
return err
}
defer cn.condRelease(&err)
return fn(cn)

// Exponential backoff (based on Ethernet)
retries := 0
err = fn(cn)
if err == io.EOF { // Bad connection
cn.nc.Close()
for err != nil && retries < maxRetries {
retries++
backoffCoefficient := int(math.Pow(float64(2), float64(retries))) - 1
sleepFor := time.Nanosecond * time.Duration(50000*backoffCoefficient)
time.Sleep(sleepFor)
cn, err = c.getConn(addr)
if err != nil {
continue
}
err = fn(cn)
}
}

return
}

func (c *Client) withKeyRw(key string, fn func(*conn) error) error {
Expand Down
Loading