forked from thanos-io/thanos
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #97 from vinted/pull_worker_change
Pull worker change
- Loading branch information
Showing
5 changed files
with
163 additions
and
114 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
// Copyright (c) The Thanos Authors. | ||
// Licensed under the Apache License 2.0. | ||
|
||
package pool | ||
|
||
import ( | ||
"context" | ||
"sync" | ||
) | ||
|
||
// Work is a unit of item to be worked on, like Java Runnable. | ||
type Work func() | ||
|
||
// WorkerPool is a pool of goroutines that are reusable, similar to Java ThreadPool. | ||
type WorkerPool interface { | ||
// Init initializes the worker pool. | ||
Init() | ||
|
||
// Go waits until the next worker becomes available and executes the given work. | ||
Go(work Work) | ||
|
||
// Close cancels all workers and waits for them to finish. | ||
Close() | ||
|
||
// Size returns the number of workers in the pool. | ||
Size() int | ||
} | ||
|
||
type workerPool struct { | ||
sync.Once | ||
workCh chan Work | ||
cancel context.CancelFunc | ||
} | ||
|
||
func NewWorkerPool(workers uint) WorkerPool { | ||
return &workerPool{ | ||
workCh: make(chan Work, workers), | ||
} | ||
} | ||
|
||
func (p *workerPool) Init() { | ||
p.Do(func() { | ||
ctx, cancel := context.WithCancel(context.Background()) | ||
p.cancel = cancel | ||
|
||
for i := 0; i < p.Size(); i++ { | ||
go func() { | ||
for { | ||
select { | ||
case <-ctx.Done(): | ||
// TODO: exhaust workCh before exit | ||
return | ||
case work := <-p.workCh: | ||
work() | ||
} | ||
} | ||
}() | ||
} | ||
}) | ||
} | ||
|
||
func (p *workerPool) Go(work Work) { | ||
p.Init() | ||
p.workCh <- work | ||
} | ||
|
||
func (p *workerPool) Close() { | ||
p.cancel() | ||
} | ||
|
||
func (p *workerPool) Size() int { | ||
return cap(p.workCh) | ||
} |
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,35 @@ | ||
// Copyright (c) The Thanos Authors. | ||
// Licensed under the Apache License 2.0. | ||
|
||
package pool | ||
|
||
import ( | ||
"sync" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestGo(t *testing.T) { | ||
var expectedWorksDone uint32 | ||
var workerPoolSize uint | ||
var mu sync.Mutex | ||
workerPoolSize = 5 | ||
p := NewWorkerPool(workerPoolSize) | ||
p.Init() | ||
defer p.Close() | ||
|
||
var wg sync.WaitGroup | ||
for i := 0; i < int(workerPoolSize*3); i++ { | ||
wg.Add(1) | ||
p.Go(func() { | ||
mu.Lock() | ||
defer mu.Unlock() | ||
expectedWorksDone++ | ||
wg.Done() | ||
}) | ||
} | ||
wg.Wait() | ||
require.Equal(t, uint32(workerPoolSize*3), expectedWorksDone) | ||
require.Equal(t, int(workerPoolSize), p.Size()) | ||
} |
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