Files
Gumble-Backend/internal/util/wait_group.go
Zane Walker 7e940c83a7
Some checks failed
Build & Test / build-test (push) Has been cancelled
Build & Test / swagger-codegen-cli (push) Has been cancelled
CodeQL / Analyze (go) (push) Has been cancelled
(Feat): Initial Commit
2026-07-03 19:41:31 +05:30

31 lines
736 B
Go

// nolint:revive
package util
import (
"errors"
"sync"
"time"
)
var (
ErrWaitTimeout = errors.New("WaitGroup has timed out")
)
// WaitTimeout waits for the waitgroup for the specified max timeout.
// Returns nil on completion or ErrWaitTimeout if waiting timed out.
// See https://stackoverflow.com/questions/32840687/timeout-for-waitgroup-wait
// Note that the spawned goroutine to wg.Wait() gets leaked and will continue running detached
func WaitTimeout(wg *sync.WaitGroup, timeout time.Duration) error {
waitChan := make(chan struct{})
go func() {
defer close(waitChan)
wg.Wait()
}()
select {
case <-waitChan:
return nil // completed normally
case <-time.After(timeout):
return ErrWaitTimeout // timed out
}
}