aboutsummaryrefslogtreecommitdiff
path: root/ref/store/files/update_lock_packed.go
diff options
context:
space:
mode:
authorGravatar Runxi Yu2026-03-25 14:31:16 +0000
committerGravatar Runxi Yu2026-03-25 14:31:16 +0000
commit48ff647cf4a8bb8f23fcd6b8616f56a8ef72b980 (patch)
treeae199c38042adaa544d5f7d31351661d5831381e /ref/store/files/update_lock_packed.go
parent*: objectstore -> object/store (diff)
signatureNo signature
*: refstore -> ref/store
Diffstat (limited to 'ref/store/files/update_lock_packed.go')
-rw-r--r--ref/store/files/update_lock_packed.go44
1 files changed, 44 insertions, 0 deletions
diff --git a/ref/store/files/update_lock_packed.go b/ref/store/files/update_lock_packed.go
new file mode 100644
index 00000000..f74a4f5e
--- /dev/null
+++ b/ref/store/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++
+ }
+ }
+}