Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: expose config and features control #349

Merged
merged 3 commits into from
Jul 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions connection_impl.go
Original file line number Diff line number Diff line change
Expand Up @@ -330,8 +330,8 @@ func (c *connection) init(conn Conn, opts *options) (err error) {
// init buffer, barrier, finalizer
c.readTrigger = make(chan error, 1)
c.writeTrigger = make(chan error, 1)
c.bookSize, c.maxSize = pagesize, pagesize
c.inputBuffer, c.outputBuffer = NewLinkBuffer(pagesize), NewLinkBuffer()
c.bookSize, c.maxSize = defaultLinkBufferSize, defaultLinkBufferSize
c.inputBuffer, c.outputBuffer = NewLinkBuffer(defaultLinkBufferSize), NewLinkBuffer()
c.outputBarrier = barrierPool.Get().(*barrier)
c.state = 0

Expand Down
7 changes: 7 additions & 0 deletions connection_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,13 @@ func TestConnectionRead(t *testing.T) {
}

func TestConnectionNoCopyReadString(t *testing.T) {
err := Configure(Config{Feature: Feature{AlwaysNoCopyRead: true}})
MustNil(t, err)
defer func() {
err = Configure(Config{Feature: Feature{AlwaysNoCopyRead: false}})
MustNil(t, err)
}()

r, w := GetSysFdPairs()
var rconn, wconn = &connection{}, &connection{}
rconn.init(&netFD{fd: r}, nil)
Expand Down
89 changes: 0 additions & 89 deletions netpoll.go

This file was deleted.

45 changes: 45 additions & 0 deletions netpoll_config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
// Copyright 2024 CloudWeGo Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package netpoll

import (
"context"
"io"
)

// global config
var (
defaultLinkBufferSize = pagesize
featureAlwaysNoCopyRead = false
)

// Config expose some tuning parameters to control the internal behaviors of netpoll.
// Every parameter with the default zero value should keep the default behavior of netpoll.
type Config struct {
PollerNum int // number of pollers
BufferSize int // default size of a new connection's LinkBuffer
Runner func(ctx context.Context, f func()) // runner for event handler, most of the time use a goroutine pool.
LoggerOutput io.Writer // logger output
LoadBalance LoadBalance // load balance for poller picker
Feature // define all features that not enable by default
}

// Feature expose some new features maybe promoted as a default behavior but not yet.
type Feature struct {
// AlwaysNoCopyRead allows some copy Read functions like ReadBinary/ReadString
// will use NoCopy read and will not reuse the underlying buffer.
// It gains more performance benefits when need read much big string/bytes in codec.
AlwaysNoCopyRead bool
}
79 changes: 13 additions & 66 deletions netpoll_options.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2022 CloudWeGo Authors
// Copyright 2024 CloudWeGo Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand All @@ -12,61 +12,23 @@
// See the License for the specific language governing permissions and
// limitations under the License.

//go:build !windows
// +build !windows

package netpoll

import (
"context"
"io"
"time"
)

// SetNumLoops is used to set the number of pollers, generally do not need to actively set.
// By default, the number of pollers is equal to runtime.GOMAXPROCS(0)/20+1.
// If the number of cores in your service process is less than 20c, theoretically only one poller is needed.
// Otherwise you may need to adjust the number of pollers to achieve the best results.
// Experience recommends assigning a poller every 20c.
//
// You can only use SetNumLoops before any connection is created. An example usage:
//
// func init() {
// netpoll.SetNumLoops(...)
// }
func SetNumLoops(numLoops int) error {
return setNumLoops(numLoops)
}

// SetLoadBalance sets the load balancing method. Load balancing is always a best effort to attempt
// to distribute the incoming connections between multiple polls.
// This option only works when numLoops is set.
func SetLoadBalance(lb LoadBalance) error {
return setLoadBalance(lb)
}
import "time"

// Initialize the pollers actively. By default, it's lazy initialized.
// It's safe to call it multi times.
func Initialize() {
initialize()
}

func SetLoggerOutput(w io.Writer) {
setLoggerOutput(w)
}

// SetRunner set the runner function for every OnRequest/OnConnect callback
func SetRunner(f func(ctx context.Context, f func())) {
setRunner(f)
// Option .
type Option struct {
f func(*options)
}

// 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()
type options struct {
onPrepare OnPrepare
onConnect OnConnect
onDisconnect OnDisconnect
onRequest OnRequest
readTimeout time.Duration
writeTimeout time.Duration
idleTimeout time.Duration
}

// WithOnPrepare registers the OnPrepare method to EventLoop.
Expand Down Expand Up @@ -110,18 +72,3 @@ func WithIdleTimeout(timeout time.Duration) Option {
op.idleTimeout = timeout
}}
}

// Option .
type Option struct {
f func(*options)
}

type options struct {
onPrepare OnPrepare
onConnect OnConnect
onDisconnect OnDisconnect
onRequest OnRequest
readTimeout time.Duration
writeTimeout time.Duration
idleTimeout time.Duration
}
Loading
Loading