MCPcopy Create free account
hub / github.com/cloudwego/netpoll / Close

Method Close

netpoll_server.go:61–95  ·  view source on GitHub ↗

Close this server with deadline.

(ctx context.Context)

Source from the content-addressed store, hash-verified

59
60// Close this server with deadline.
61func (s *server) Close(ctx context.Context) error {
62 s.operator.Control(PollDetach)
63 s.ln.Close()
64
65 for {
66 activeConn := 0
67 s.connections.Range(func(key, value interface{}) bool {
68 conn, ok := value.(gracefulExit)
69 if !ok || conn.isIdle() {
70 value.(Connection).Close()
71 } else {
72 activeConn++
73 }
74 return true
75 })
76 if activeConn == 0 { // all connections have been closed
77 return nil
78 }
79
80 // smart control graceful shutdown check internal
81 // we should wait for more time if there are more active connections
82 waitTime := time.Millisecond * time.Duration(activeConn)
83 if waitTime > time.Second { // max wait time is 1000 ms
84 waitTime = time.Millisecond * 1000
85 } else if waitTime < time.Millisecond*50 { // min wait time is 50 ms
86 waitTime = time.Millisecond * 50
87 }
88 select {
89 case <-ctx.Done():
90 return ctx.Err()
91 case <-time.After(waitTime):
92 continue
93 }
94 }
95}
96
97// OnRead implements FDOperator.
98func (s *server) OnRead(p Poll) error {

Callers

nothing calls this directly

Calls 3

ControlMethod · 0.65
CloseMethod · 0.65
isIdleMethod · 0.65

Tested by

no test coverage detected