-
-
Notifications
You must be signed in to change notification settings - Fork 347
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add a worker pool to limit parallel checks
- Loading branch information
Showing
11 changed files
with
211 additions
and
23 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
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,76 @@ | ||
package checkgroup | ||
|
||
import "context" | ||
|
||
type ( | ||
PoolOption func(*workerPool) | ||
ctxKey string | ||
) | ||
|
||
const poolCtxKey ctxKey = "pool" | ||
|
||
// WithPool returns a new context that contains the pool. The pool will be used by the checkgroup and the binary operators (or, and) when spawning subchecks. | ||
func WithPool(ctx context.Context, pool Pool) context.Context { | ||
return context.WithValue(ctx, poolCtxKey, pool) | ||
} | ||
|
||
// PoolFromContext returns the pool from the context, or a pool that does not | ||
// limit the number of parallel jobs if none found. | ||
func PoolFromContext(ctx context.Context) Pool { | ||
if p, ok := ctx.Value(poolCtxKey).(*workerPool); !ok { | ||
return new(limitlessPool) | ||
} else { | ||
return p | ||
} | ||
} | ||
|
||
// NewPool creates a new worker pool. With no options, this yields a pool with | ||
// exactly one worker, meaning that all tasks that are added will run | ||
// sequentially. | ||
func NewPool(opts ...PoolOption) Pool { | ||
pool := &workerPool{ | ||
numWorkers: 1, | ||
} | ||
for _, opt := range opts { | ||
opt(pool) | ||
} | ||
|
||
pool.jobs = make(chan func(), pool.numWorkers) | ||
for i := 0; i < pool.numWorkers; i++ { | ||
go worker(pool.jobs) | ||
} | ||
|
||
if pool.ctx != nil { | ||
go func() { | ||
<-pool.ctx.Done() | ||
close(pool.jobs) | ||
}() | ||
} | ||
|
||
return pool | ||
} | ||
|
||
func worker(jobs <-chan func()) { | ||
for job := range jobs { | ||
job() | ||
} | ||
} | ||
|
||
func WithWorkers(count int) PoolOption { | ||
return func(p *workerPool) { p.numWorkers = count } | ||
} | ||
func WithContext(ctx context.Context) PoolOption { | ||
return func(p *workerPool) { p.ctx = ctx } | ||
} | ||
|
||
// Add adds the function to the pool and schedules it. The function will only be | ||
// run if there is a free worker available in the pool, thus limiting the | ||
// concurrent workloads in flight. | ||
func (p *workerPool) Add(check func()) { | ||
p.jobs <- check | ||
} | ||
|
||
// Add on a limitless pool just runs the function in a go routine. | ||
func (p *limitlessPool) Add(check func()) { | ||
go check() | ||
} |
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,40 @@ | ||
package checkgroup_test | ||
|
||
import ( | ||
"context" | ||
"sync" | ||
"sync/atomic" | ||
"testing" | ||
"time" | ||
|
||
"github.com/ory/keto/internal/check/checkgroup" | ||
) | ||
|
||
func TestPool(t *testing.T) { | ||
ctx, cancel := context.WithCancel(context.Background()) | ||
defer cancel() | ||
|
||
numWorkers := 5 | ||
p := checkgroup.NewPool( | ||
checkgroup.WithWorkers(numWorkers), | ||
checkgroup.WithContext(ctx), | ||
) | ||
|
||
var ( | ||
jobsCount int32 | ||
wg sync.WaitGroup | ||
) | ||
|
||
wg.Add(1000) | ||
for i := 0; i < 1000; i++ { | ||
p.Add(func() { | ||
defer wg.Done() | ||
if jobs := atomic.AddInt32(&jobsCount, 1); jobs > int32(numWorkers) { | ||
t.Errorf("%d jobs in flight, more than %d", jobs, numWorkers) | ||
} | ||
time.Sleep(1 * time.Millisecond) | ||
atomic.AddInt32(&jobsCount, -1) | ||
}) | ||
} | ||
wg.Wait() | ||
} |
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