package useCases import ( "RenewCMS/internal/domain/image" "errors" "sync" ) var errFlightAbandoned = errors.New("shared call ended without a result") type flightCall struct { done chan struct{} value image.VariantFile err error } type flightGroup struct { mu sync.Mutex calls map[string]*flightCall } func (g *flightGroup) do(key string, work func() (image.VariantFile, error)) (image.VariantFile, error) { g.mu.Lock() if existing, running := g.calls[key]; running { g.mu.Unlock() <-existing.done return existing.value, existing.err } if g.calls == nil { g.calls = map[string]*flightCall{} } call := &flightCall{done: make(chan struct{}), err: errFlightAbandoned} g.calls[key] = call g.mu.Unlock() defer func() { g.mu.Lock() delete(g.calls, key) g.mu.Unlock() close(call.done) }() call.value, call.err = work() return call.value, call.err }