Skip to content

Commit

Permalink
Randomly offset retention and expiration time. (thought-machine#270)
Browse files Browse the repository at this point in the history
* Randomly offset retention and expiration time.
  • Loading branch information
fische authored and Hamishpk committed Mar 7, 2024
1 parent af61dde commit 81d9fe3
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 4 deletions.
6 changes: 6 additions & 0 deletions ChangeLog
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
Version 11.6.1
--------------
* Randomly offset retentionTime and expiryTime for each job so the
goroutines don't all wake up at the same time. This is a bit of a hack,
a proper fix will be raised soon.

Version 11.6.0
--------------
* Add rate-limiter to Redis client so we don't fail slowly
Expand Down
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
11.6.0
11.6.1
7 changes: 4 additions & 3 deletions mettle/api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ package api
import (
"context"
"fmt"
"math/rand"
"net"
"strings"
"sync"
Expand Down Expand Up @@ -563,7 +564,7 @@ func (s *server) process(msg *pubsub.Message) {

// deleteJob waits for a period then removes the given job from memory.
func (s *server) deleteJob(hash string, j *job) {
time.Sleep(retentionTime)
time.Sleep(retentionTime + time.Duration(rand.Int63n(int64(retentionTime))))
s.mutex.Lock()
defer s.mutex.Unlock()
// Check the action hasn't been replaced since deleteJob was called
Expand All @@ -576,11 +577,11 @@ func (s *server) deleteJob(hash string, j *job) {

// expireJob expires an action that hasn't progressed.
func (s *server) expireJob(hash string) {
time.Sleep(expiryTime)
time.Sleep(expiryTime + time.Duration(rand.Int63n(int64(expiryTime))))
if s.maybeExpireJob(hash, false) {
return
}
time.Sleep(expiryTime)
time.Sleep(expiryTime + time.Duration(rand.Int63n(int64(expiryTime))))
s.maybeExpireJob(hash, true)
}

Expand Down

0 comments on commit 81d9fe3

Please sign in to comment.