Skip to content

Commit

Permalink
Fix: default not to start gr pool limit (#72)
Browse files Browse the repository at this point in the history
* fix: default not start gr pool

* fix: ut
  • Loading branch information
LaurenceLiZhixin authored Oct 17, 2021
1 parent b640a57 commit 99c2ec7
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 0 deletions.
7 changes: 7 additions & 0 deletions sync/base_worker_pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ type WorkerPoolConfig struct {
NumQueues int
QueueSize int
Logger gxlog.Logger
Enable bool
}

// baseWorkerPool is a worker pool with multiple queues.
Expand Down Expand Up @@ -69,6 +70,7 @@ type baseWorkerPool struct {
taskQueues []chan task

numWorkers *atomic.Int32
enable bool

wg *sync.WaitGroup
}
Expand All @@ -94,6 +96,11 @@ func newBaseWorkerPool(config WorkerPoolConfig) *baseWorkerPool {
taskQueues: taskQueues,
numWorkers: new(atomic.Int32),
wg: new(sync.WaitGroup),
enable: config.Enable,
}

if !config.Enable {
return p
}

initWg := new(sync.WaitGroup)
Expand Down
5 changes: 5 additions & 0 deletions sync/connection_pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,11 @@ func (p *ConnectionPool) Submit(t task) error {
return perrors.New("task shouldn't be nil")
}

if !p.enable {
go t()
return nil
}

// put the task to a queue using Round Robin algorithm
taskId := atomic.AddUint32(&p.taskId, 1)
select {
Expand Down
10 changes: 10 additions & 0 deletions sync/connection_pool_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ func TestConnectionPool(t *testing.T) {
NumQueues: runtime.NumCPU(),
QueueSize: 10,
Logger: nil,
Enable: true,
})
var count int64
wg := new(sync.WaitGroup)
Expand All @@ -60,6 +61,7 @@ func TestConnectionPool(t *testing.T) {
NumQueues: 1,
QueueSize: 0,
Logger: nil,
Enable: true,
})

wg := new(sync.WaitGroup)
Expand All @@ -85,6 +87,7 @@ func TestConnectionPool(t *testing.T) {
NumWorkers: runtime.NumCPU(),
NumQueues: runtime.NumCPU(),
QueueSize: 100,
Enable: true,
Logger: nil,
})

Expand All @@ -102,6 +105,7 @@ func TestConnectionPool(t *testing.T) {
p := NewConnectionPool(WorkerPoolConfig{
NumWorkers: 0,
NumQueues: runtime.NumCPU(),
Enable: true,
QueueSize: 100,
Logger: nil,
})
Expand All @@ -111,6 +115,7 @@ func TestConnectionPool(t *testing.T) {
p = NewConnectionPool(WorkerPoolConfig{
NumWorkers: 1,
NumQueues: 0,
Enable: true,
QueueSize: 0,
Logger: nil,
})
Expand All @@ -123,6 +128,7 @@ func TestConnectionPool(t *testing.T) {
NumQueues: 1,
QueueSize: -1,
Logger: nil,
Enable: true,
})

err = p.Submit(func() {})
Expand All @@ -134,6 +140,7 @@ func TestConnectionPool(t *testing.T) {
p := NewConnectionPool(WorkerPoolConfig{
NumWorkers: 1,
NumQueues: 1,
Enable: true,
QueueSize: 0,
Logger: nil,
})
Expand All @@ -149,6 +156,7 @@ func TestConnectionPool(t *testing.T) {
NumQueues: runtime.NumCPU(),
QueueSize: 10,
Logger: nil,
Enable: true,
})

task, v := newCountTask()
Expand All @@ -174,6 +182,7 @@ func TestConnectionPool(t *testing.T) {
NumQueues: runtime.NumCPU(),
QueueSize: 10,
Logger: nil,
Enable: true,
})

task, v := newCountTask()
Expand All @@ -192,6 +201,7 @@ func BenchmarkConnectionPool(b *testing.B) {
NumWorkers: 100,
NumQueues: runtime.NumCPU(),
QueueSize: 100,
Enable: true,
Logger: nil,
})

Expand Down

0 comments on commit 99c2ec7

Please sign in to comment.