aboutsummaryrefslogtreecommitdiff
path: root/funcs.go
diff options
context:
space:
mode:
authorMatt Joiner <anacrolix@gmail.com>2020-08-31 14:06:50 +1000
committerMatt Joiner <anacrolix@gmail.com>2020-09-10 09:22:35 +1000
commitd6711ea6dec3d49b182dbe60a1e1db6c148adede (patch)
tree8ebc8051594455b3d30bcf0bc3050388bb67c671 /funcs.go
parentAdd .circleci/config.yml (diff)
downloadstm-d6711ea6dec3d49b182dbe60a1e1db6c148adede.tar.gz
stm-d6711ea6dec3d49b182dbe60a1e1db6c148adede.tar.xz
Add exponentially longer sleeping between transaction attempts
Diffstat (limited to 'funcs.go')
-rw-r--r--funcs.go15
1 files changed, 15 insertions, 0 deletions
diff --git a/funcs.go b/funcs.go
index aad9c90..650a559 100644
--- a/funcs.go
+++ b/funcs.go
@@ -1,8 +1,10 @@
package stm
import (
+ "math/rand"
"runtime/pprof"
"sync"
+ "time"
)
var (
@@ -43,8 +45,21 @@ func Atomically(op Operation) interface{} {
expvars.Add("atomically", 1)
// run the transaction
tx := newTx()
+ tx.tries = 0
retry:
+ tx.tries++
tx.reset()
+ shift := int64(tx.tries - 1)
+ const maxShift = 30
+ if shift > maxShift {
+ shift = maxShift
+ }
+ ns := int64(1) << shift
+ ns = rand.Int63n(ns)
+ if ns > 0 {
+ tx.updateWatchers()
+ time.Sleep(time.Duration(ns))
+ }
ret, retry := catchRetry(op, tx)
if retry {
expvars.Add("retries", 1)