@@ -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+
3654var 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
5071type 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
0 commit comments