From d6711ea6dec3d49b182dbe60a1e1db6c148adede Mon Sep 17 00:00:00 2001 From: Matt Joiner Date: Mon, 31 Aug 2020 14:06:50 +1000 Subject: Add exponentially longer sleeping between transaction attempts --- funcs.go | 15 +++++++++++++++ 1 file changed, 15 insertions(+) (limited to 'funcs.go') 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) -- cgit v1.2.3