github.com/go-ext/syncsafe

Package syncsafe package provides synchronization mechanisms similar to native sync package but in more defensive way


License
MIT
Install
go get github.com/go-ext/syncsafe

Documentation

Go Report Card Codacy Badge CI codecov GoDoc Licenses

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()