@@ -60,7 +60,7 @@ func (s *server) Run(ctx context.Context) error {
60
60
MaxHeaderBytes : mhbz ,
61
61
BaseContext : func (net.Listener ) context.Context { return ctx },
62
62
ErrorLog : errLogger (ctx ),
63
- ConnState : diagConn ,
63
+ ConnState : getDiagConnFunc ( ctx ) ,
64
64
}
65
65
66
66
var listenCfg net.ListenConfig
@@ -117,7 +117,7 @@ func (s *server) Run(ctx context.Context) error {
117
117
}
118
118
// Do a clean shutdown if the context is cancelled
119
119
case <- ctx .Done ():
120
- sCtx , cancel := context .WithTimeout (context .Background (), s .cfg .Timeouts .Drain )
120
+ sCtx , cancel := context .WithTimeout (context .Background (), s .cfg .Timeouts .Drain ) // Background context to allow connections to drain when server context is cancelled.
121
121
defer cancel ()
122
122
if err := srv .Shutdown (sCtx ); err != nil {
123
123
cErr := srv .Close () // force it closed
@@ -128,24 +128,26 @@ func (s *server) Run(ctx context.Context) error {
128
128
return nil
129
129
}
130
130
131
- func diagConn (c net.Conn , s http.ConnState ) {
132
- if c == nil {
133
- return
134
- }
131
+ func getDiagConnFunc (ctx context.Context ) func (c net.Conn , s http.ConnState ) {
132
+ return func (c net.Conn , s http.ConnState ) {
133
+ if c == nil {
134
+ return
135
+ }
135
136
136
- zerolog .Ctx (context .TODO ()).Trace ().
137
- Str ("local" , c .LocalAddr ().String ()).
138
- Str ("remote" , c .RemoteAddr ().String ()).
139
- Str ("state" , s .String ()).
140
- Msg ("connection state change" )
141
-
142
- switch s {
143
- case http .StateNew :
144
- cntHTTPNew .Inc ()
145
- cntHTTPActive .Inc ()
146
- case http .StateClosed :
147
- cntHTTPClose .Inc ()
148
- cntHTTPActive .Dec ()
137
+ zerolog .Ctx (ctx ).Trace ().
138
+ Str ("local" , c .LocalAddr ().String ()).
139
+ Str ("remote" , c .RemoteAddr ().String ()).
140
+ Str ("state" , s .String ()).
141
+ Msg ("connection state change" )
142
+
143
+ switch s {
144
+ case http .StateNew :
145
+ cntHTTPNew .Inc ()
146
+ cntHTTPActive .Inc ()
147
+ case http .StateClosed :
148
+ cntHTTPClose .Inc ()
149
+ cntHTTPActive .Dec ()
150
+ }
149
151
}
150
152
}
151
153
@@ -157,7 +159,7 @@ func wrapConnLimitter(ctx context.Context, ln net.Listener, cfg *config.Server)
157
159
Int ("hardConnLimit" , hardLimit ).
158
160
Msg ("server hard connection limiter installed" )
159
161
160
- ln = limit .Listener (ln , hardLimit )
162
+ ln = limit .Listener (ln , hardLimit , zerolog . Ctx ( ctx ) )
161
163
} else {
162
164
zerolog .Ctx (ctx ).Info ().Msg ("server hard connection limiter disabled" )
163
165
}
0 commit comments