Skip to content

Commit

Permalink
feat: add API to DisableGopool (cloudwego#28)
Browse files Browse the repository at this point in the history
  • Loading branch information
Hchenn committed Sep 16, 2021
1 parent 276559f commit e188cf1
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
11 changes: 10 additions & 1 deletion connection_onevent.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,15 @@ import (
"github.com/bytedance/gopkg/util/gopool"
)

var runTask = gopool.CtxGo

func disableGopool() error {
runTask = func(ctx context.Context, f func()) {
go f()
}
return nil
}

// ------------------------------------ implement OnPrepare, OnRequest, CloseCallback ------------------------------------

type gracefulExit interface {
Expand Down Expand Up @@ -116,7 +125,7 @@ func (c *connection) onRequest() (err error) {
goto START
}
}
gopool.CtxGo(c.ctx, task)
runTask(c.ctx, task)
return nil
}

Expand Down
9 changes: 9 additions & 0 deletions netpoll_options.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,15 @@ func SetLoadBalance(lb LoadBalance) error {
return setLoadBalance(lb)
}

// DisableGopool will remove gopool(the goroutine pool used to run OnRequest),
// which means that OnRequest will be run via `go OnRequest(...)`.
// Usually, OnRequest will cause stack expansion, which can be solved by reusing goroutine.
// But if you can confirm that the OnRequest will not cause stack expansion,
// it is recommended to use DisableGopool to reduce redundancy and improve performance.
func DisableGopool() error {
return disableGopool()
}

// WithOnPrepare registers the OnPrepare method to EventLoop.
func WithOnPrepare(onPrepare OnPrepare) Option {
return Option{func(op *options) {
Expand Down

0 comments on commit e188cf1

Please sign in to comment.