Changeset 735 in code


Ignore:
Timestamp:
Dec 2, 2021, 11:14:35 AM (4 years ago)
Author:
contact
Message:

Add exponential backoff when re-connecting to upstream

The first reconnection attempt waits for 1min, the second the 2min,
and so on up to 10min. There's a 1min jitter so that multiple failed
connections don't try to reconnect at the exact same time.

Closes: https://todo.sr.ht/~emersion/soju/161

Location:
trunk
Files:
1 added
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/server.go

    r734 r735  
    2424
    2525// TODO: make configurable
    26 var retryConnectDelay = time.Minute
     26var retryConnectMinDelay = time.Minute
     27var retryConnectMaxDelay = 10 * time.Minute
     28var retryConnectJitter = time.Minute
    2729var connectTimeout = 15 * time.Second
    2830var writeTimeout = 10 * time.Second
  • trunk/user.go

    r734 r735  
    191191
    192192        var lastTry time.Time
     193        backoff := newBackoffer(retryConnectMinDelay, retryConnectMaxDelay, retryConnectJitter)
    193194        for {
    194195                if net.isStopped() {
     
    196197                }
    197198
    198                 if dur := time.Now().Sub(lastTry); dur < retryConnectDelay {
    199                         delay := retryConnectDelay - dur
     199                delay := backoff.Next() - time.Now().Sub(lastTry)
     200                if delay > 0 {
    200201                        net.logger.Printf("waiting %v before trying to reconnect to %q", delay.Truncate(time.Second), net.Addr)
    201202                        time.Sleep(delay)
     
    248249
    249250                net.user.srv.metrics.upstreams.Add(-1)
     251                backoff.Reset()
    250252        }
    251253}
Note: See TracChangeset for help on using the changeset viewer.