aboutsummaryrefslogtreecommitdiff
path: root/refstore/files/transaction_lock_packed.go
diff options
context:
space:
mode:
authorGravatar Runxi Yu2026-03-07 18:09:20 +0800
committerGravatar Runxi Yu2026-03-07 18:17:54 +0800
commite667c3c52a535ee67fe895bb0240fbad6e920087 (patch)
tree0815f7cc9b2c4a06d00722bce4c3ac57c515288b /refstore/files/transaction_lock_packed.go
parentreceivepack: Connect protocol with service (diff)
refstore/files: Accept timeout instead of reading from config
And split things up again.
Diffstat (limited to 'refstore/files/transaction_lock_packed.go')
-rw-r--r--refstore/files/transaction_lock_packed.go44
1 files changed, 44 insertions, 0 deletions
diff --git a/refstore/files/transaction_lock_packed.go b/refstore/files/transaction_lock_packed.go
new file mode 100644
index 00000000..4538e5e5
--- /dev/null
+++ b/refstore/files/transaction_lock_packed.go
@@ -0,0 +1,44 @@
+package files
+
+import (
+ "errors"
+ "os"
+ "time"
+)
+
+func (tx *Transaction) createPackedLock(timeout time.Duration) error {
+ const (
+ initialBackoffMs = 1
+ backoffMaxMultiplier = 1000
+ )
+
+ deadline := time.Now().Add(timeout)
+ multiplier := 1
+ n := 1
+
+ for {
+ file, err := tx.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 + tx.store.lockRand.Intn(500)) * backoffMs / 1000
+ time.Sleep(time.Duration(waitMs) * time.Millisecond)
+
+ multiplier += 2*n + 1
+ if multiplier > backoffMaxMultiplier {
+ multiplier = backoffMaxMultiplier
+ } else {
+ n++
+ }
+ }
+}