From 4a796e64ac576d6a3e3f2fe6174c4aa476ea0c5c Mon Sep 17 00:00:00 2001 From: Runxi Yu Date: Mon, 23 Mar 2026 03:25:44 +0000 Subject: refstore: Improve interfaces, errors, and make batch work --- refstore/files/update_lock_packed.go | 44 ++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 refstore/files/update_lock_packed.go (limited to 'refstore/files/update_lock_packed.go') 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++ + } + } +} -- cgit v1.3.1-10-gc9f91