Files
greeneclipse 8580c996c3 Initial commit of Descrybe v2 without local scratch artifacts.
Drop one-shot tmp/axe scripts and agent i18n scratch so the Gitea tree is deployable.
2026-08-09 22:47:43 +02:00

66 lines
1.5 KiB
Go
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package jobs
import "sync"
// DefaultSyncWorkers is the in-process bound for concurrent feed/Woo/Shopify syncs.
// Claim paths use FOR UPDATE SKIP LOCKED so each slot gets a distinct job.
const DefaultSyncWorkers = 1
// MaxSyncWorkers caps in-process sync parallelism (DB pool + upstream API RPM).
const MaxSyncWorkers = 2
// ClampSyncWorkers bounds n to [1, MaxSyncWorkers].
func ClampSyncWorkers(n int) int {
if n < 1 {
return 1
}
if n > MaxSyncWorkers {
return MaxSyncWorkers
}
return n
}
// SyncSlots limits concurrent sync Process* goroutines across feed/Woo/Shopify claims.
type SyncSlots struct {
Workers int
sem chan struct{}
wg sync.WaitGroup
}
// NewSyncSlots creates a bounded slot set for concurrent sync jobs.
func NewSyncSlots(workers int) *SyncSlots {
w := ClampSyncWorkers(workers)
return &SyncSlots{
Workers: w,
sem: make(chan struct{}, w),
}
}
// Wait blocks until all in-flight sync goroutines finish.
func (s *SyncSlots) Wait() {
s.wg.Wait()
}
// TryStart claims one free slot (non-blocking). claim must be SKIP LOCKEDsafe.
// If claim fails, the slot is released. On success, run executes in a new goroutine.
func (s *SyncSlots) TryStart(claim func() error, run func()) (started bool, claimErr error) {
select {
case s.sem <- struct{}{}:
default:
return false, nil
}
if err := claim(); err != nil {
<-s.sem
return false, err
}
s.wg.Add(1)
go func() {
defer s.wg.Done()
defer func() { <-s.sem }()
run()
}()
return true, nil
}