Skip to content

Commit

Permalink
Add random initial delay for starting backfill queue
Browse files Browse the repository at this point in the history
  • Loading branch information
tulir committed Jan 29, 2024
1 parent b48fea0 commit f0d40c9
Showing 1 changed file with 12 additions and 7 deletions.
19 changes: 12 additions & 7 deletions backfill.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"crypto/sha256"
"encoding/base64"
"fmt"
"math/rand"
"slices"
"strconv"
"sync"
Expand Down Expand Up @@ -137,14 +138,16 @@ func (user *User) BackfillLoop() {
}
ctx = log.WithContext(ctx)
var extraTime time.Duration
sleepBetweenTasks := user.bridge.Config.Bridge.Backfill.Queue.SleepBetweenTasks
initialSleep := time.Duration(rand.Int63n(sleepBetweenTasks.Nanoseconds())) + (sleepBetweenTasks / 2)
log.Debug().Stringer("sleep_duration", initialSleep).Msg("Starting backfill loop after initial delay")
select {
case <-time.After(initialSleep):
case <-ctx.Done():
return
}
log.Debug().Msg("Backfill loop started")
for {
select {
case <-time.After(user.bridge.Config.Bridge.Backfill.Queue.SleepBetweenTasks + extraTime):
case <-ctx.Done():
return
}

task, err := user.bridge.DB.BackfillTask.GetNext(ctx, user.MXID)
if err != nil {
log.Err(err).Msg("Failed to get next backfill task")
Expand All @@ -154,7 +157,9 @@ func (user *User) BackfillLoop() {
} else if extraTime < 1*time.Minute {
extraTime += 5 * time.Second
}
if ctx.Err() != nil {
select {
case <-time.After(sleepBetweenTasks + extraTime):
case <-ctx.Done():
return
}
}
Expand Down

0 comments on commit f0d40c9

Please sign in to comment.