# README
sync.safe 🛟
Introduction
syncsafe package provides synchronization mechanisms similar to native sync package but in more defensive way.
- WaitGroup implementation gives you a way of waiting with context addressing the risk of indefinite hanging because of stuck jobs inside whatever reasons are.
- TaggedWaitGroup provides a way of having more insights on pending counters tagging every Add operation.
Usage
Installation
go get github.com/go-ext/syncsafe
WaitGroup examples
ctx, cancel := context.WithTimeout(context.Background(), time.Second*1)
defer cancel()
wg := NewWaitGroup()
for i := 0; i < 3; i++ {
wg.Add(1)
go func(int i) {
defer wg.Done()
time.Sleep(time.Second * time.Duration(i))
}(i)
}
if err := wg.WaitContext(ctx); err != nil {
log.Fatal(err, err.StackTrace())
}
TaggedWaitGroup examples
wg := NewTaggedWaitGroup()
doneCalcJob := wg.Add("calculate-job", 1)
doneSendJob := wg.Add("send-job", 1)
go func() {
// After a while
doneCalcJob()
fmt.Println(wg.Counters()) // Will print map[send-job:1]
doneSendJob()
}()
wg.Wait()
# Functions
NewTaggedWaitGroup returns a new instance of WaitGroup.
NewWaitGroup returns a new instance of WaitGroup.
# Structs
TaggedWaitGroup provides a way to wait for all async routines to be done exactly as sync.WaitGroup does it but providing more controllable ways of waiting, avoiding infinite blocking in case of any unexpected circumstances.
WaitGroup provides a way to wait for all async routines to be done exactly as sync.WaitGroup does it but providing more controllable ways of waiting, avoiding infinite blocking in case of any unexpected circumstances WaitGroup instance is a single use only to prevent potential unnecessary mess in case of re-using.
# Interfaces
StackError specifies an object providing an error along with stack trace.
# Type aliases
DoneFn specifies a type of TaggedWaitGroup done function which decreases counter increased with Add call previously.