aboutsummaryrefslogtreecommitdiff
path: root/refstore/files/update_lock_packed.go
diff options
context:
space:
mode:
Diffstat (limited to 'refstore/files/update_lock_packed.go')
-rw-r--r--refstore/files/update_lock_packed.go44
1 files changed, 44 insertions, 0 deletions
diff --git a/refstore/files/update_lock_packed.go b/refstore/files/update_lock_packed.go
new file mode 100644
index 00000000..f74a4f5e
--- /dev/null
+++ b/refstore/files/update_lock_packed.go
@@ -0,0 +1,44 @@
+package files
+
+import (
+ "errors"
+ "os"
+ "time"
+)
+
+func (executor *refUpdateExecutor) createPackedRefsLock(timeout time.Duration) error {
+ const (
+ initialBackoffMs = 1
+ backoffMaxMultiplier = 1000
+ )
+
+ deadline := time.Now().Add(timeout)
+ multiplier := 1
+ n := 1
+
+ for {
+ file, err := executor.store.commonRoot.OpenFile("packed-refs.lock", os.O_WRONLY|os.O_CREATE|os.O_EXCL, 0o644)
+ if err == nil {
+ return file.Close()
+ }
+
+ if !errors.Is(err, os.ErrExist) {
+ return err
+ }
+
+ if timeout == 0 || (timeout > 0 && time.Now().After(deadline)) {
+ return err
+ }
+
+ backoffMs := multiplier * initialBackoffMs
+ waitMs := (750 + executor.store.lockRand.Intn(500)) * backoffMs / 1000
+ time.Sleep(time.Duration(waitMs) * time.Millisecond)
+
+ multiplier += 2*n + 1
+ if multiplier > backoffMaxMultiplier {
+ multiplier = backoffMaxMultiplier
+ } else {
+ n++
+ }
+ }
+}