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
}