From 602baf9c5b843ce481d20de07c7a7b532c131884 Mon Sep 17 00:00:00 2001 From: lesismal <40462947+lesismal@users.noreply.github.com> Date: Fri, 16 Jul 2021 20:37:41 +0800 Subject: [PATCH] + config: MaxReadTimesPerEventLoop --- gopher.go | 25 ++++++++++++++++--------- gopher_unix.go | 30 +++++++++++++++++------------- nbhttp/server.go | 43 ++++++++++++++++++++++++------------------- poller_epoll.go | 2 +- 4 files changed, 58 insertions(+), 42 deletions(-) diff --git a/gopher.go b/gopher.go index 1aac5d69..a08360b3 100644 --- a/gopher.go +++ b/gopher.go @@ -21,6 +21,9 @@ const ( // DefaultMaxWriteBufferSize . DefaultMaxWriteBufferSize = 1024 * 1024 + // DefaultMaxReadTimesPerEventLoop . + DefaultMaxReadTimesPerEventLoop = 3 + // DefaultMinConnCacheSize . DefaultMinConnCacheSize = 1024 * 2 ) @@ -64,6 +67,9 @@ type Config struct { // more than MaxWriteBufferSize, the connection would be closed by nbio. MaxWriteBufferSize int + // MaxReadTimesPerEventLoop represents max read times in one poller loop for one fd + MaxReadTimesPerEventLoop int + // LockListener represents listener's goroutine to lock thread or not, it's set to false by default. LockListener bool @@ -79,15 +85,16 @@ type Gopher struct { Name string - network string - addrs []string - pollerNum int - backlogSize int - readBufferSize int - maxWriteBufferSize int - minConnCacheSize int - lockListener bool - lockPoller bool + network string + addrs []string + pollerNum int + backlogSize int + readBufferSize int + maxWriteBufferSize int + maxReadTimesPerEventLoop int + minConnCacheSize int + lockListener bool + lockPoller bool lfds []int diff --git a/gopher_unix.go b/gopher_unix.go index e4b42b9a..ad97bd23 100644 --- a/gopher_unix.go +++ b/gopher_unix.go @@ -89,21 +89,25 @@ func NewGopher(conf Config) *Gopher { if conf.MinConnCacheSize == 0 { conf.MinConnCacheSize = DefaultMinConnCacheSize } + if conf.MaxReadTimesPerEventLoop <= 0 { + conf.MaxReadTimesPerEventLoop = DefaultMaxReadTimesPerEventLoop + } g := &Gopher{ - Name: conf.Name, - network: conf.Network, - addrs: conf.Addrs, - pollerNum: conf.NPoller, - backlogSize: conf.Backlog, - readBufferSize: conf.ReadBufferSize, - maxWriteBufferSize: conf.MaxWriteBufferSize, - minConnCacheSize: conf.MinConnCacheSize, - lockListener: conf.LockListener, - lockPoller: conf.LockPoller, - listeners: make([]*poller, len(conf.Addrs)), - pollers: make([]*poller, conf.NPoller), - connsUnix: make([]*Conn, MaxOpenFiles), + Name: conf.Name, + network: conf.Network, + addrs: conf.Addrs, + pollerNum: conf.NPoller, + backlogSize: conf.Backlog, + readBufferSize: conf.ReadBufferSize, + maxWriteBufferSize: conf.MaxWriteBufferSize, + maxReadTimesPerEventLoop: conf.MaxReadTimesPerEventLoop, + minConnCacheSize: conf.MinConnCacheSize, + lockListener: conf.LockListener, + lockPoller: conf.LockPoller, + listeners: make([]*poller, len(conf.Addrs)), + pollers: make([]*poller, conf.NPoller), + connsUnix: make([]*Conn, MaxOpenFiles), trigger: time.NewTimer(timeForever), chTimer: make(chan struct{}), diff --git a/nbhttp/server.go b/nbhttp/server.go index 46057e40..479e8131 100644 --- a/nbhttp/server.go +++ b/nbhttp/server.go @@ -23,7 +23,7 @@ import ( var ( // DefaultMaxLoad . - DefaultMaxLoad = 1024 * 500 + DefaultMaxLoad = 1024 * 1024 // DefaultHTTPReadLimit . DefaultHTTPReadLimit = 1024 * 1024 * 64 @@ -113,6 +113,9 @@ type Config struct { // ReleaseWebsocketPayload . ReleaseWebsocketPayload bool + + // MaxReadTimesPerEventLoop represents max read times in one poller loop for one fd + MaxReadTimesPerEventLoop int } // Server . @@ -276,15 +279,16 @@ func NewServer(conf Config, handler http.Handler, messageHandlerExecutor func(in } gopherConf := nbio.Config{ - Name: conf.Name, - Network: conf.Network, - Addrs: conf.Addrs, - NPoller: conf.NPoller, - NListener: conf.NListener, - ReadBufferSize: conf.ReadBufferSize, - MaxWriteBufferSize: conf.MaxWriteBufferSize, - LockPoller: conf.LockPoller, - LockListener: conf.LockListener, + Name: conf.Name, + Network: conf.Network, + Addrs: conf.Addrs, + NPoller: conf.NPoller, + NListener: conf.NListener, + ReadBufferSize: conf.ReadBufferSize, + MaxWriteBufferSize: conf.MaxWriteBufferSize, + MaxReadTimesPerEventLoop: conf.MaxReadTimesPerEventLoop, + LockPoller: conf.LockPoller, + LockListener: conf.LockListener, } g := nbio.NewGopher(gopherConf) @@ -438,15 +442,16 @@ func NewServerTLS(conf Config, handler http.Handler, messageHandlerExecutor func } gopherConf := nbio.Config{ - Name: conf.Name, - Network: conf.Network, - Addrs: conf.Addrs, - NPoller: conf.NPoller, - NListener: conf.NListener, - ReadBufferSize: conf.ReadBufferSize, - MaxWriteBufferSize: conf.MaxWriteBufferSize, - LockPoller: conf.LockPoller, - LockListener: conf.LockListener, + Name: conf.Name, + Network: conf.Network, + Addrs: conf.Addrs, + NPoller: conf.NPoller, + NListener: conf.NListener, + ReadBufferSize: conf.ReadBufferSize, + MaxWriteBufferSize: conf.MaxWriteBufferSize, + MaxReadTimesPerEventLoop: conf.MaxReadTimesPerEventLoop, + LockPoller: conf.LockPoller, + LockListener: conf.LockListener, } g := nbio.NewGopher(gopherConf) diff --git a/poller_epoll.go b/poller_epoll.go index 20fff885..1d5e118a 100644 --- a/poller_epoll.go +++ b/poller_epoll.go @@ -195,7 +195,7 @@ func (p *poller) readWrite(ev *syscall.EpollEvent) { } if ev.Events&epoollEventsRead != 0 { - for i := 0; i < 3; i++ { + for i := 0; i < p.g.maxReadTimesPerEventLoop; i++ { buffer := p.g.borrow(c) n, err := c.Read(buffer) if n > 0 {