Skip to content

Commit

Permalink
opt: make channel buffered if necessary
Browse files Browse the repository at this point in the history
  • Loading branch information
panjf2000 committed Aug 21, 2020
1 parent 7832a4f commit 43f93ca
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 4 deletions.
2 changes: 1 addition & 1 deletion connection_unix.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ type conn struct {
sa unix.Sockaddr // remote socket address
ctx interface{} // user-defined context
loop *eventloop // connected event-loop
buffer []byte // reuse memory of inbound data as a temporary buffer
codec ICodec // codec for TCP
buffer []byte // reuse memory of inbound data as a temporary buffer
opened bool // connection opened event fired
localAddr net.Addr // local addr
remoteAddr net.Addr // remote addr
Expand Down
15 changes: 15 additions & 0 deletions gnet.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ package gnet

import (
"net"
"runtime"
"strings"
"sync/atomic"
"time"
Expand Down Expand Up @@ -264,3 +265,17 @@ func sniffErrorAndLog(err error) {
logging.DefaultLogger.Errorf(err.Error())
}
}

// channelBuffer determines whether the channel should be a buffered channel to get the best performance.
func channelBuffer(preset int) int {
// Use blocking channel if GOMAXPROCS=1.
// This switches context from sender to receiver immediately,
// which results in higher performance (under go1.5 at least).
if runtime.GOMAXPROCS(0) == 1 {
return 0
}

// Use non-blocking workerChan if GOMAXPROCS>1,
// since otherwise the sender might be dragged down if the receiver is CPU-bound.
return preset
}
2 changes: 1 addition & 1 deletion server_unix.go
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@ func serve(eventHandler EventHandler, listener *listener, options *Options) erro
}

svr.cond = sync.NewCond(&sync.Mutex{})
svr.ticktock = make(chan time.Duration, 1)
svr.ticktock = make(chan time.Duration, channelBuffer(1))
svr.logger = logging.DefaultLogger
svr.codec = func() ICodec {
if options.Codec == nil {
Expand Down
4 changes: 2 additions & 2 deletions server_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ import (

// commandBufferSize represents the buffer size of event-loop command channel on Windows.
const (
commandBufferSize = 512
commandBufferSize = 128
)

var errCloseAllConns = errors.New("close all connections in event-loop")
Expand Down Expand Up @@ -86,7 +86,7 @@ func (svr *server) startListener() {
func (svr *server) startEventLoops(numEventLoop int) {
for i := 0; i < numEventLoop; i++ {
el := &eventloop{
ch: make(chan interface{}, commandBufferSize),
ch: make(chan interface{}, channelBuffer(commandBufferSize)),
svr: svr,
connections: make(map[*stdConn]struct{}),
eventHandler: svr.eventHandler,
Expand Down

0 comments on commit 43f93ca

Please sign in to comment.