some commonly used go concurrency models
concurrent requests with timeout capability
tasks := make([]TaskFunc, 0, testSize)
tasks = append(tasks, func (ctx context.Context, i int) (Result, error) {
// do your things, like rpc call
})
wo := New(tasks)
result, err := wo.ParallelingWithTimeout(context.Background(), 5*time.Second)
there are 3 version about ParallelingWithTimeout
function
ParallelingWithTimeout
original concurrency modelsParallelingWithTimeoutV2
use WaitGroup secure synchronization about goroutinesParallelingWithTimeoutV3
use errgroup streamline the code
v3 is recommended
- If you want implement a function with timeout, do not use sync.WaitGroup.Wait on the main goroutine, because the function you want to implement needs to exit during timeout, If the main goroutine is synchronized using wait, main goroutine can not quit midway.
- When you use channel, try to make sure, who send the data, who close it. If you make sure there are no one to send data, you can close it anywhere.
- In fact, the
close
function is a very useful method, it can be used to tell multiple coroutines to end blocking, like thecancelCtx
do. - Please ensure that all new gorutine can be exited, unless you have a special purpose