11package promise
22
33import (
4- "context"
54 "runtime"
65 "sync"
76 "sync/atomic"
@@ -19,7 +18,7 @@ func DefaultPromiseMgrConfig() *PromiseMgrConfig {
1918 executorWorkers := cpuCount * 2
2019 microtaskWorkers := cpuCount
2120
22- microtaskQueueSize := 2000 + microtaskWorkers * 200
21+ microtaskQueueSize := 5000 + microtaskWorkers * 500
2322
2423 return & PromiseMgrConfig {
2524 ExecutorWorkers : executorWorkers ,
@@ -30,15 +29,11 @@ func DefaultPromiseMgrConfig() *PromiseMgrConfig {
3029}
3130
3231type PromiseMgr struct {
33- executorPool * taskPool
34- microtaskPool * taskPool
35- mu sync.RWMutex
36- shutdown int32
37- config * PromiseMgrConfig
38- microtaskSched chan func ()
39- schedulerWg sync.WaitGroup
40- schedulerCtx context.Context
41- schedulerCancel context.CancelFunc
32+ executorPool * taskPool
33+ microtaskPool * taskPool
34+ mu sync.RWMutex
35+ shutdown int32
36+ config * PromiseMgrConfig
4237}
4338
4439func NewPromiseMgr () * PromiseMgr {
@@ -53,7 +48,6 @@ func NewPromiseMgrWithConfig(config *PromiseMgrConfig) *PromiseMgr {
5348 defaultConfig := DefaultPromiseMgrConfig ()
5449 normalizeConfig (config , defaultConfig )
5550
56- ctx , cancel := context .WithCancel (context .Background ())
5751 mgr := & PromiseMgr {
5852 executorPool : newTaskPool (& taskPoolConfig {
5953 Workers : config .ExecutorWorkers ,
@@ -63,14 +57,9 @@ func NewPromiseMgrWithConfig(config *PromiseMgrConfig) *PromiseMgr {
6357 Workers : config .MicrotaskWorkers ,
6458 QueueSize : config .MicrotaskQueueSize ,
6559 }),
66- config : config ,
67- microtaskSched : make (chan func (), 10000 ),
68- schedulerCtx : ctx ,
69- schedulerCancel : cancel ,
60+ config : config ,
7061 }
7162
72- mgr .startMicrotaskScheduler ()
73-
7463 return mgr
7564}
7665
@@ -99,45 +88,12 @@ func (m *PromiseMgr) scheduleExecutor(executor func()) error {
9988 return m .executorPool .Submit (executor )
10089}
10190
102- func (m * PromiseMgr ) startMicrotaskScheduler () {
103- m .schedulerWg .Add (1 )
104- go func () {
105- defer m .schedulerWg .Done ()
106- for {
107- select {
108- case fn := <- m .microtaskSched :
109- if fn == nil {
110- return
111- }
112- _ = m .microtaskPool .Submit (fn )
113- case <- m .schedulerCtx .Done ():
114- return
115- }
116- }
117- }()
118- }
119-
12091func (m * PromiseMgr ) scheduleMicrotask (fn func ()) error {
12192 if atomic .LoadInt32 (& m .shutdown ) == 1 {
12293 return ErrManagerStopped
12394 }
12495
125- select {
126- case m .microtaskSched <- fn :
127- return nil
128- case <- m .schedulerCtx .Done ():
129- return ErrManagerStopped
130- default :
131- go func () {
132- defer func () {
133- if r := recover (); r != nil {
134- // Prevent goroutine crash
135- }
136- }()
137- fn ()
138- }()
139- return nil
140- }
96+ return m .microtaskPool .Submit (fn )
14197}
14298
14399// SetMicrotaskConfig updates microtask configuration
@@ -211,10 +167,6 @@ func (m *PromiseMgr) Close() {
211167 m .mu .Lock ()
212168 defer m .mu .Unlock ()
213169
214- m .schedulerCancel ()
215- close (m .microtaskSched )
216- m .schedulerWg .Wait ()
217-
218170 m .executorPool .Close ()
219171 m .microtaskPool .Close ()
220172}
@@ -235,7 +187,6 @@ func (m *PromiseMgr) IsShutdown() bool {
235187
236188// WaitForShutdown waits for shutdown completion
237189func (m * PromiseMgr ) WaitForShutdown () {
238- m .schedulerWg .Wait ()
239190 m .executorPool .WaitForShutdown ()
240191 m .microtaskPool .WaitForShutdown ()
241192}
0 commit comments