Skip to content

Commit 0207003

Browse files
authored
fix: unified runner config and hid gopool (#380)
1 parent 53e3080 commit 0207003

File tree

5 files changed

+96
-23
lines changed

5 files changed

+96
-23
lines changed

connection_onevent.go

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -21,22 +21,9 @@ import (
2121
"context"
2222
"sync/atomic"
2323

24-
"github.com/bytedance/gopkg/util/gopool"
24+
"github.com/cloudwego/netpoll/internal/runner"
2525
)
2626

27-
var runTask = gopool.CtxGo
28-
29-
func setRunner(runner func(ctx context.Context, f func())) {
30-
runTask = runner
31-
}
32-
33-
func disableGopool() error {
34-
runTask = func(ctx context.Context, f func()) {
35-
go f()
36-
}
37-
return nil
38-
}
39-
4027
// ------------------------------------ implement OnPrepare, OnRequest, CloseCallback ------------------------------------
4128

4229
type gracefulExit interface {
@@ -272,7 +259,7 @@ func (c *connection) onProcess(onConnect OnConnect, onRequest OnRequest) (proces
272259
} // end of task closure func
273260

274261
// add new task
275-
runTask(c.ctx, task)
262+
runner.RunTask(c.ctx, task)
276263
return true
277264
}
278265

internal/runner/runner.go

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/*
2+
* Copyright 2025 CloudWeGo Authors
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package runner
18+
19+
import (
20+
"context"
21+
22+
"github.com/bytedance/gopkg/util/gopool"
23+
)
24+
25+
// RunTask runs the `f` in background, and `ctx` is optional.
26+
// `ctx` is used to pass to underlying implementation
27+
var RunTask func(ctx context.Context, f func())
28+
29+
func goRunTask(ctx context.Context, f func()) {
30+
go f()
31+
}
32+
33+
func init() {
34+
// TODO(xiaost): Disable gopool by default in the future.
35+
// Once we move to use gopool of cloudwego/gopkg in other repos,
36+
// there should be no reason to continue using bytedance/gopkg version,
37+
// and for most users, using the 'go' keyword directly is more suitable.
38+
RunTask = gopool.CtxGo
39+
}
40+
41+
// UseGoRunTask updates RunTask with goRunTask which creates
42+
// a new goroutine for the given func, basically `go f()`
43+
func UseGoRunTask() {
44+
RunTask = goRunTask
45+
}

internal/runner/runner_test.go

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/*
2+
* Copyright 2025 CloudWeGo Authors
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package runner
18+
19+
import (
20+
"context"
21+
"sync"
22+
"testing"
23+
)
24+
25+
func TestRunTask(t *testing.T) {
26+
var wg sync.WaitGroup
27+
wg.Add(2)
28+
ctx := context.Background()
29+
RunTask(ctx, func() {
30+
wg.Done()
31+
})
32+
UseGoRunTask()
33+
RunTask(ctx, func() {
34+
wg.Done()
35+
})
36+
wg.Wait()
37+
}

mux/shard_queue.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,8 @@ import (
2020
"sync"
2121
"sync/atomic"
2222

23-
"github.com/bytedance/gopkg/util/gopool"
24-
2523
"github.com/cloudwego/netpoll"
24+
"github.com/cloudwego/netpoll/internal/runner"
2625
)
2726

2827
/* DOC:
@@ -137,7 +136,7 @@ func (q *ShardQueue) foreach() {
137136
if atomic.AddInt32(&q.runNum, 1) > 1 {
138137
return
139138
}
140-
gopool.CtxGo(nil, func() {
139+
runner.RunTask(nil, func() {
141140
var negNum int32 // is negative number of triggerNum
142141
for triggerNum := atomic.LoadInt32(&q.trigger); triggerNum > 0; {
143142
q.r = (q.r + 1) % q.size

netpoll_unix.go

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ import (
2525
"os"
2626
"runtime"
2727
"sync"
28+
29+
"github.com/cloudwego/netpoll/internal/runner"
2830
)
2931

3032
var (
@@ -52,7 +54,7 @@ func Configure(config Config) (err error) {
5254
}
5355

5456
if config.Runner != nil {
55-
setRunner(config.Runner)
57+
runner.RunTask = config.Runner
5658
}
5759
if config.LoggerOutput != nil {
5860
logger = log.New(config.LoggerOutput, "", log.LstdFlags)
@@ -99,19 +101,22 @@ func SetLoggerOutput(w io.Writer) {
99101
}
100102

101103
// SetRunner set the runner function for every OnRequest/OnConnect callback
102-
// Deprecated: use Configure instead.
104+
//
105+
// Deprecated: use Configure and specify config.Runner instead.
103106
func SetRunner(f func(ctx context.Context, f func())) {
104-
setRunner(f)
107+
runner.RunTask = f
105108
}
106109

107110
// DisableGopool will remove gopool(the goroutine pool used to run OnRequest),
108111
// which means that OnRequest will be run via `go OnRequest(...)`.
109112
// Usually, OnRequest will cause stack expansion, which can be solved by reusing goroutine.
110113
// But if you can confirm that the OnRequest will not cause stack expansion,
111114
// it is recommended to use DisableGopool to reduce redundancy and improve performance.
112-
// Deprecated: use Configure instead.
115+
//
116+
// Deprecated: use Configure() and specify config.Runner instead.
113117
func DisableGopool() error {
114-
return disableGopool()
118+
runner.UseGoRunTask()
119+
return nil
115120
}
116121

117122
// NewEventLoop .

0 commit comments

Comments
 (0)