Skip to content

Latest commit

 

History

History
44 lines (31 loc) · 892 Bytes

README.md

File metadata and controls

44 lines (31 loc) · 892 Bytes

parallelworker

this is a simple lib to do work parallel.

for example: you need do some check, called check A, check B, check C, and those check are not interdependent, so we can do those check at the same time

like this

Usage

  1. make a content
ctx := context.Background()
  1. make worker
worker := parallelworker.NewParallelWorker(ctx)
  1. add all your worker
worker.AddWorker(func(ctx context.Context) {
    select {
    	case <-ctx.Done():
            // if ctx is cancel, you should return your function
            return
        default:
    }
    // do something
})
  1. wait for finish

    this function will be blocked, so you need add a timeout context to control it, this context is different from the first one

timeOutCtx := context.WithTimeout(ctx, time.Second * 5)
worker.WaitContext(timeOutCtx)