Skip to content

Commit 4b11ece

Browse files
committed
Extract DefaultWorkerSettings.
1 parent 506417d commit 4b11ece

File tree

2 files changed

+35
-32
lines changed

2 files changed

+35
-32
lines changed

worker/pool.go

Lines changed: 34 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -32,19 +32,40 @@ type WorkerPool interface {
3232
ScheduleWithTimeout(func(), time.Duration) error
3333
}
3434

35-
//
35+
// DefaultWorkerSettings Settings for DefaultWorkerPool
36+
type DefaultWorkerSettings struct {
37+
// JobQueue
38+
39+
isJobQueueClosedWhenClose bool
40+
workerBatchSize int
41+
42+
// Worker
43+
44+
workerSizeStandBy int
45+
workerSizeMaximum int
46+
spawnWorkerDuration time.Duration
47+
workerExpiryDuration time.Duration
48+
49+
// Panic Handler
50+
51+
panicHandler func(interface{})
52+
}
53+
3654
var defaultPanicHandler = func(panic interface{}) {
3755
log.Printf("panic from worker: %v\n", panic)
3856
buf := make([]byte, 4096)
3957
log.Printf("panic from worker: %s\n", string(buf[:runtime.Stack(buf, false)]))
4058
}
4159

42-
type (
43-
workerRecord struct {
44-
LastAccessTime time.Time
45-
TerminatedCh fpgo.ChannelQueue[int]
46-
}
47-
)
60+
var defaultDefaultWorkerSettings = &DefaultWorkerSettings{
61+
isJobQueueClosedWhenClose: true,
62+
workerBatchSize: 5,
63+
workerSizeStandBy: 5,
64+
workerSizeMaximum: 1000,
65+
spawnWorkerDuration: 100 * time.Millisecond,
66+
workerExpiryDuration: 5000 * time.Millisecond,
67+
panicHandler: defaultPanicHandler,
68+
}
4869

4970
// DefaultWorkerPool DefaultWorkerPool inspired by Java ExecutorService
5071
type DefaultWorkerPool struct {
@@ -58,39 +79,21 @@ type DefaultWorkerPool struct {
5879
lastAccessTime time.Time
5980

6081
// Settings
61-
62-
// JobQueue
63-
64-
isJobQueueClosedWhenClose bool
65-
workerBatchSize int
66-
67-
// Worker
68-
69-
workerSizeStandBy int
70-
workerSizeMaximum int
71-
spawnWorkerDuration time.Duration
72-
workerExpiryDuration time.Duration
73-
74-
// Panic Handler
75-
76-
panicHandler func(interface{})
82+
DefaultWorkerSettings
7783
}
7884

7985
// NewDefaultWorkerPool New a DefaultWorkerPool
80-
func NewDefaultWorkerPool(jobQueue *fpgo.BufferedChannelQueue[func()]) *DefaultWorkerPool {
86+
func NewDefaultWorkerPool(jobQueue *fpgo.BufferedChannelQueue[func()], settings *DefaultWorkerSettings) *DefaultWorkerPool {
87+
if settings == nil {
88+
settings = defaultDefaultWorkerSettings
89+
}
8190
workerPool := &DefaultWorkerPool{
8291
jobQueue: jobQueue,
8392

8493
spawnWorkerCh: fpgo.NewChannelQueue[int](1),
8594

8695
// Settings
87-
isJobQueueClosedWhenClose: true,
88-
workerBatchSize: 5,
89-
workerSizeStandBy: 5,
90-
workerSizeMaximum: 1000,
91-
spawnWorkerDuration: 100 * time.Millisecond,
92-
workerExpiryDuration: 5000 * time.Millisecond,
93-
panicHandler: defaultPanicHandler,
96+
DefaultWorkerSettings: *settings,
9497
}
9598
go workerPool.spawnLoop()
9699

worker/pool_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import (
1313
func TestWorkerPool(t *testing.T) {
1414
var workerPool WorkerPool
1515
var err error
16-
defaultWorkerPool := NewDefaultWorkerPool(fpgo.NewBufferedChannelQueue[func()](3, 10000, 100)).
16+
defaultWorkerPool := NewDefaultWorkerPool(fpgo.NewBufferedChannelQueue[func()](3, 10000, 100), nil).
1717
SetSpawnWorkerDuration(1 * time.Millisecond / 10).
1818
SetWorkerExpiryDuration(2 * time.Millisecond).
1919
SetWorkerSizeMaximum(5).

0 commit comments

Comments
 (0)