Skip to content

Commit

Permalink
optimise: smart control graceful shutdown wait time
Browse files Browse the repository at this point in the history
  • Loading branch information
joway committed Aug 22, 2024
1 parent f64c76c commit 7ff1f46
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 21 deletions.
6 changes: 2 additions & 4 deletions netpoll_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,12 @@ package netpoll
import (
"context"
"io"
"time"
)

// global config
var (
defaultLinkBufferSize = pagesize
defaultGracefulShutdownCheckInterval = time.Second
featureAlwaysNoCopyRead = false
defaultLinkBufferSize = pagesize
featureAlwaysNoCopyRead = false
)

// Config expose some tuning parameters to control the internal behaviors of netpoll.
Expand Down
20 changes: 13 additions & 7 deletions netpoll_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,27 +63,33 @@ func (s *server) Close(ctx context.Context) error {
s.operator.Control(PollDetach)
s.ln.Close()

var ticker = time.NewTicker(defaultGracefulShutdownCheckInterval)
defer ticker.Stop()
var hasConn bool
for {
hasConn = false
activeConn := 0
s.connections.Range(func(key, value interface{}) bool {
var conn, ok = value.(gracefulExit)
if !ok || conn.isIdle() {
value.(Connection).Close()
} else {
activeConn++
}
hasConn = true
return true
})
if !hasConn { // all connections have been closed
if activeConn == 0 { // all connections have been closed
return nil
}

// smart control graceful shutdown check internal
// we should wait for more time if there are more active connections
waitTime := time.Millisecond * time.Duration(activeConn)
if waitTime > time.Second { // max wait time is 1000 ms
waitTime = time.Millisecond * 1000
} else if waitTime < time.Millisecond*10 { // min wait time is 50 ms
waitTime = time.Millisecond * 50
}
select {
case <-ctx.Done():
return ctx.Err()
case <-ticker.C:
case <-time.After(waitTime):
continue
}
}
Expand Down
9 changes: 0 additions & 9 deletions netpoll_unix_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,15 +64,6 @@ func Assert(t *testing.T, cond bool, val ...interface{}) {
}
}

func TestMain(m *testing.M) {
// defaultGracefulShutdownCheckInterval will affect shutdown function running time,
// so for speed up tests, we change it to 10ms here
oldGracefulShutdownCheckInterval := defaultGracefulShutdownCheckInterval
defaultGracefulShutdownCheckInterval = time.Millisecond * 10
m.Run()
defaultGracefulShutdownCheckInterval = oldGracefulShutdownCheckInterval
}

var testPort int32 = 10000

// getTestAddress return a unique port for every tests, so all tests will not share a same listerner
Expand Down
2 changes: 1 addition & 1 deletion poll_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ func TestPollMod(t *testing.T) {
runtime.Gosched()
}
r, w, h = atomic.LoadInt32(&rn), atomic.LoadInt32(&wn), atomic.LoadInt32(&hn)
Assert(t, r == 0 && w == 1 && h == 0, r, w, h)
Assert(t, r == 0 && w >= 1 && h == 0, r, w, h)

err = p.Control(rop, PollR2RW) // trigger write
MustNil(t, err)
Expand Down

0 comments on commit 7ff1f46

Please sign in to comment.