-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(pool): introduce pool.Manager (#251)
* refactor(pool): introduce pool.Manager Change-Id: I72f76d24fe267ef9e2bfc8d3df376d22d56df205 * refactor(pool): introduce pool.Manager Change-Id: I72f76d24fe267ef9e2bfc8d3df376d22d56df205 * pool manager set maxDuration default value 10 Minutes (#252) * fix: comment about provide * fix: manager's maxDuration default 10 Minutes Co-authored-by: guxi.reasno <guxi.reasno@bytedance.com> Co-authored-by: rock G <35254251+GGXXLL@users.noreply.github.com>
- Loading branch information
1 parent
e7c857f
commit 8fffa99
Showing
13 changed files
with
297 additions
and
193 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,97 @@ | ||
package pool | ||
|
||
import ( | ||
"context" | ||
"sync" | ||
"time" | ||
|
||
"github.com/DoNewsCode/core/ctxmeta" | ||
) | ||
|
||
// Manager manages a pool of workers. | ||
type Manager struct { | ||
workers chan *Worker | ||
maxDuration time.Duration | ||
startWorkerCh chan *Worker | ||
managerStoppedCh chan struct{} | ||
} | ||
|
||
// NewManager returns a new manager. | ||
func NewManager() *Manager { | ||
return &Manager{ | ||
workers: make(chan *Worker, 1000), | ||
startWorkerCh: make(chan *Worker), | ||
managerStoppedCh: make(chan struct{}), | ||
maxDuration: 10 * time.Minute, | ||
} | ||
} | ||
|
||
// Get returns a worker from the free list. If the free list is empty, create a new one. | ||
func (m *Manager) Get() *Worker { | ||
var w *Worker | ||
select { | ||
case w = <-m.workers: | ||
default: | ||
w = NewWorker() | ||
select { | ||
case m.startWorkerCh <- w: | ||
case <-m.managerStoppedCh: | ||
w.Stop() | ||
} | ||
} | ||
return w | ||
} | ||
|
||
// Release put the worker back into the free list. If the free list is full, | ||
// discard the worker. If the worker has surpassed the max duration, discard and | ||
// managerStoppedCh the worker. | ||
func (m *Manager) Release(w *Worker) { | ||
if time.Since(w.startTime) > m.maxDuration { | ||
w.Stop() | ||
return | ||
} | ||
|
||
select { | ||
case m.workers <- w: | ||
default: | ||
} | ||
} | ||
|
||
// Go runs function with no concurrency limit. | ||
func (m *Manager) Go(ctx context.Context, f func(context.Context)) { | ||
w := m.Get() | ||
ctx = ctxmeta.WithoutCancel(ctx) | ||
fn := func() { | ||
f(ctx) | ||
m.Release(w) | ||
} | ||
select { | ||
case w.jobCh <- fn: | ||
case <-w.stopCh: // only executed if manager.Run is cancelled | ||
fn() | ||
} | ||
} | ||
|
||
// Run starts the manager. It should be called during the initialization of the program. | ||
func (m *Manager) Run(ctx context.Context) error { | ||
var wg sync.WaitGroup | ||
for { | ||
select { | ||
case w := <-m.startWorkerCh: | ||
wg.Add(1) | ||
go func(w *Worker) { | ||
w.Run(ctx) | ||
wg.Done() | ||
}(w) | ||
case <-ctx.Done(): | ||
close(m.managerStoppedCh) | ||
wg.Wait() | ||
return nil | ||
} | ||
} | ||
} | ||
|
||
// Module implements the di.Modular interface. | ||
func (m *Manager) Module() interface{} { | ||
return m | ||
} |
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,21 @@ | ||
package pool | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
"time" | ||
) | ||
|
||
func TestManager_Go(t *testing.T) { | ||
ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second) | ||
defer cancel() | ||
|
||
m := NewManager() | ||
go m.Run(ctx) | ||
|
||
var executed = make(chan struct{}) | ||
m.Go(ctx, func(ctx context.Context) { | ||
close(executed) | ||
}) | ||
<-executed | ||
} |
This file was deleted.
Oops, something went wrong.
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
Oops, something went wrong.