-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Receive: fix issue-7248 with parallel receive_forward (#7267)
* Receive: fix issue-7248 by introducing a worker pool Signed-off-by: Yi Jin <[email protected]> * fix unit test bug Signed-off-by: Yi Jin <[email protected]> * fix CLI flags not pass into the receive handler Signed-off-by: Yi Jin <[email protected]> * address comments Signed-off-by: Yi Jin <[email protected]> * init context in constructor Signed-off-by: Yi Jin <[email protected]> --------- Signed-off-by: Yi Jin <[email protected]>
- Loading branch information
Showing
5 changed files
with
143 additions
and
96 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,74 @@ | ||
// 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 | ||
ctx context.Context | ||
workCh chan Work | ||
cancel context.CancelFunc | ||
} | ||
|
||
func NewWorkerPool(workers uint) WorkerPool { | ||
ctx, cancel := context.WithCancel(context.Background()) | ||
return &workerPool{ | ||
ctx: ctx, | ||
cancel: cancel, | ||
workCh: make(chan Work, workers), | ||
} | ||
} | ||
|
||
func (p *workerPool) Init() { | ||
p.Do(func() { | ||
for i := 0; i < p.Size(); i++ { | ||
go func() { | ||
for { | ||
select { | ||
case <-p.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