-
Notifications
You must be signed in to change notification settings - Fork 48
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
05f5243
commit b9c8bd7
Showing
4 changed files
with
132 additions
and
49 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
package common | ||
|
||
import ( | ||
"context" | ||
"time" | ||
|
||
"golang.org/x/sync/errgroup" | ||
"golang.org/x/time/rate" | ||
) | ||
|
||
// WorkerPool a pool that can control the total amount and rate of concurrency | ||
type WorkerPool struct { | ||
job chan Job | ||
g *errgroup.Group | ||
subCtx context.Context | ||
|
||
workerNum int | ||
lim *rate.Limiter | ||
} | ||
|
||
type Job func(ctx context.Context) error | ||
|
||
// NewWorkerPool build a worker pool, rps 0 is unlimited | ||
func NewWorkerPool(ctx context.Context, workerNum int, rps int32) *WorkerPool { | ||
g, subCtx := errgroup.WithContext(ctx) | ||
|
||
var lim *rate.Limiter | ||
if rps != 0 { | ||
lim = rate.NewLimiter(rate.Every(time.Second/time.Duration(rps)), 1) | ||
} | ||
|
||
return &WorkerPool{job: make(chan Job), workerNum: workerNum, g: g, lim: lim, subCtx: subCtx} | ||
} | ||
|
||
func (p *WorkerPool) Start() { | ||
for i := 0; i < p.workerNum; i++ { | ||
p.g.Go(p.work) | ||
} | ||
} | ||
|
||
func (p *WorkerPool) work() error { | ||
for job := range p.job { | ||
if p.lim != nil { | ||
if err := p.lim.Wait(p.subCtx); err != nil { | ||
return err | ||
} | ||
} | ||
|
||
if err := job(p.subCtx); err != nil { | ||
return err | ||
} | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func (p *WorkerPool) Submit(job Job) { p.job <- job } | ||
func (p *WorkerPool) Done() { close(p.job) } | ||
func (p *WorkerPool) Wait() error { return p.g.Wait() } |