Skip to content
quarry Artifact [cd36446c33]
Login

Artifact [cd36446c33]

Artifact cd36446c33951f8652488b080724255fe4f4fec06d6d7a2ced72e0313e5295a2:


package api

import "sync"

// restacking tracks repositories currently mid-way through a multi-rung
// auto-restack cascade in landStack (#507). While a cascade is writing
// sequential rung updates, background fold ticks skip enqueuing
// intermediate CI runs; landStack enqueues once after the cascade ends.
var (
	restackMu     sync.Mutex
	restackCounts = map[string]int{}
)

// EnterRestack marks repo as mid-restack and returns an idempotent
// release function.
func EnterRestack(repo string) func() {
	restackMu.Lock()
	restackCounts[repo]++
	restackMu.Unlock()
	var once sync.Once
	return func() {
		once.Do(func() {
			restackMu.Lock()
			if n := restackCounts[repo] - 1; n <= 0 {
				delete(restackCounts, repo)
			} else {
				restackCounts[repo] = n
			}
			restackMu.Unlock()
		})
	}
}

// Restacking reports whether repo is currently mid-way through a
// multi-rung auto-restack cascade.
func Restacking(repo string) bool {
	restackMu.Lock()
	defer restackMu.Unlock()
	return restackCounts[repo] > 0
}