Skip to content

Commit

Permalink
Merge pull request #65 from lesismal/poolopt
Browse files Browse the repository at this point in the history
+ config: MaxReadTimesPerEventLoop
  • Loading branch information
lesismal authored Jul 16, 2021
2 parents 901808a + 602baf9 commit e54367e
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 42 deletions.
25 changes: 16 additions & 9 deletions gopher.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ const (
// DefaultMaxWriteBufferSize .
DefaultMaxWriteBufferSize = 1024 * 1024

// DefaultMaxReadTimesPerEventLoop .
DefaultMaxReadTimesPerEventLoop = 3

// DefaultMinConnCacheSize .
DefaultMinConnCacheSize = 1024 * 2
)
Expand Down Expand Up @@ -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

Expand All @@ -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

Expand Down
30 changes: 17 additions & 13 deletions gopher_unix.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{}),
Expand Down
43 changes: 24 additions & 19 deletions nbhttp/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import (

var (
// DefaultMaxLoad .
DefaultMaxLoad = 1024 * 500
DefaultMaxLoad = 1024 * 1024

// DefaultHTTPReadLimit .
DefaultHTTPReadLimit = 1024 * 1024 * 64
Expand Down Expand Up @@ -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 .
Expand Down Expand Up @@ -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)

Expand Down Expand Up @@ -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)

Expand Down
2 changes: 1 addition & 1 deletion poller_epoll.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit e54367e

Please sign in to comment.