Skip to content

workshop-depot/jobgroup

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

License: MIT PkgGoDev Go Report Card Maintainability Test Coverage

jobgroup

To manage a group of goroutines, it's possible to use an instance of context.Context. And to wait for them to finish, it's possible to use an instance of sync.WaitGroup. The jobgroup provides a utility, which combines these functionalities, under a simple API.

example usage

Assume a set of jobs need to be finished inside a time-window. For this purpose a context can be used.

ctx, cancel := context.WithTimeout(context.Background(), delay)
defer cancel()

Then, using that context, we create a job-group.

grp := jobgroup.New(ctx)

Each goroutine can be registered in this job-group.

grp.Go(func(ctx context.Context) {
    // ...
    // cancel this job if the context is canceled/timeout
    // <-ctx.Done()
})

And then we wait for them to finish.

grp.Wait()

Also, it's possible to wait for them to finish for limited duration of time.

err := grp.Wait(time.Second * 5)

The returned err will be nil, if all jobs are finished. Or jobgroup.ErrTimeout if any job fails to stop after five seconds of waiting.

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Packages

No packages published