@@ -45,18 +45,20 @@ type datagramConn struct {
4545 conn QuicConnection
4646 index uint8
4747 sessionManager SessionManager
48+ metrics Metrics
4849 logger * zerolog.Logger
4950
5051 datagrams chan []byte
5152 readErrors chan error
5253}
5354
54- func NewDatagramConn (conn QuicConnection , sessionManager SessionManager , index uint8 , logger * zerolog.Logger ) DatagramConn {
55+ func NewDatagramConn (conn QuicConnection , sessionManager SessionManager , index uint8 , metrics Metrics , logger * zerolog.Logger ) DatagramConn {
5556 log := logger .With ().Uint8 ("datagramVersion" , 3 ).Logger ()
5657 return & datagramConn {
5758 conn : conn ,
5859 index : index ,
5960 sessionManager : sessionManager ,
61+ metrics : metrics ,
6062 logger : & log ,
6163 datagrams : make (chan []byte , demuxChanCapacity ),
6264 readErrors : make (chan error , 2 ),
@@ -143,19 +145,21 @@ func (c *datagramConn) Serve(ctx context.Context) error {
143145 c .logger .Err (err ).Msgf ("unable to unmarshal session registration datagram" )
144146 return
145147 }
148+ logger := c .logger .With ().Str (logFlowID , reg .RequestID .String ()).Logger ()
146149 // We bind the new session to the quic connection context instead of cloudflared context to allow for the
147150 // quic connection to close and close only the sessions bound to it. Closing of cloudflared will also
148151 // initiate the close of the quic connection, so we don't have to worry about the application context
149152 // in the scope of a session.
150- c .handleSessionRegistrationDatagram (connCtx , reg )
153+ c .handleSessionRegistrationDatagram (connCtx , reg , & logger )
151154 case UDPSessionPayloadType :
152155 payload := & UDPSessionPayloadDatagram {}
153156 err := payload .UnmarshalBinary (datagram )
154157 if err != nil {
155158 c .logger .Err (err ).Msgf ("unable to unmarshal session payload datagram" )
156159 return
157160 }
158- c .handleSessionPayloadDatagram (payload )
161+ logger := c .logger .With ().Str (logFlowID , payload .RequestID .String ()).Logger ()
162+ c .handleSessionPayloadDatagram (payload , & logger )
159163 case UDPSessionRegistrationResponseType :
160164 // cloudflared should never expect to receive UDP session responses as it will not initiate new
161165 // sessions towards the edge.
@@ -169,31 +173,33 @@ func (c *datagramConn) Serve(ctx context.Context) error {
169173}
170174
171175// This method handles new registrations of a session and the serve loop for the session.
172- func (c * datagramConn ) handleSessionRegistrationDatagram (ctx context.Context , datagram * UDPSessionRegistrationDatagram ) {
176+ func (c * datagramConn ) handleSessionRegistrationDatagram (ctx context.Context , datagram * UDPSessionRegistrationDatagram , logger * zerolog. Logger ) {
173177 session , err := c .sessionManager .RegisterSession (datagram , c )
174178 switch err {
175179 case nil :
176180 // Continue as normal
177181 case ErrSessionAlreadyRegistered :
178182 // Session is already registered and likely the response got lost
179- c .handleSessionAlreadyRegistered (datagram .RequestID )
183+ c .handleSessionAlreadyRegistered (datagram .RequestID , logger )
180184 return
181185 case ErrSessionBoundToOtherConn :
182186 // Session is already registered but to a different connection
183- c .handleSessionMigration (datagram .RequestID )
187+ c .handleSessionMigration (datagram .RequestID , logger )
184188 return
185189 default :
186- c . logger .Err (err ).Msgf ("session registration failure" )
187- c .handleSessionRegistrationFailure (datagram .RequestID )
190+ logger .Err (err ).Msgf ("flow registration failure" )
191+ c .handleSessionRegistrationFailure (datagram .RequestID , logger )
188192 return
189193 }
194+ c .metrics .IncrementFlows ()
190195 // Make sure to eventually remove the session from the session manager when the session is closed
191196 defer c .sessionManager .UnregisterSession (session .ID ())
197+ defer c .metrics .DecrementFlows ()
192198
193199 // Respond that we are able to process the new session
194200 err = c .SendUDPSessionResponse (datagram .RequestID , ResponseOk )
195201 if err != nil {
196- c . logger .Err (err ).Msgf ("session registration failure: unable to send session registration response" )
202+ logger .Err (err ).Msgf ("flow registration failure: unable to send session registration response" )
197203 return
198204 }
199205
@@ -203,24 +209,24 @@ func (c *datagramConn) handleSessionRegistrationDatagram(ctx context.Context, da
203209 if err == nil {
204210 // We typically don't expect a session to close without some error response. [SessionIdleErr] is the typical
205211 // expected error response.
206- c . logger .Warn ().Msg ("session was closed without explicit close or timeout" )
212+ logger .Warn ().Msg ("flow was closed without explicit close or timeout" )
207213 return
208214 }
209215 // SessionIdleErr and SessionCloseErr are valid and successful error responses to end a session.
210216 if errors .Is (err , SessionIdleErr {}) || errors .Is (err , SessionCloseErr ) {
211- c . logger .Debug ().Msg (err .Error ())
217+ logger .Debug ().Msg (err .Error ())
212218 return
213219 }
214220
215221 // All other errors should be reported as errors
216- c . logger .Err (err ).Msgf ("session was closed with an error" )
222+ logger .Err (err ).Msgf ("flow was closed with an error" )
217223}
218224
219- func (c * datagramConn ) handleSessionAlreadyRegistered (requestID RequestID ) {
225+ func (c * datagramConn ) handleSessionAlreadyRegistered (requestID RequestID , logger * zerolog. Logger ) {
220226 // Send another registration response since the session is already active
221227 err := c .SendUDPSessionResponse (requestID , ResponseOk )
222228 if err != nil {
223- c . logger .Err (err ).Msgf ("session registration failure: unable to send an additional session registration response" )
229+ logger .Err (err ).Msgf ("flow registration failure: unable to send an additional flow registration response" )
224230 return
225231 }
226232
@@ -233,9 +239,10 @@ func (c *datagramConn) handleSessionAlreadyRegistered(requestID RequestID) {
233239 // The session is already running in another routine so we want to restart the idle timeout since no proxied
234240 // packets have come down yet.
235241 session .ResetIdleTimer ()
242+ c .metrics .RetryFlowResponse ()
236243}
237244
238- func (c * datagramConn ) handleSessionMigration (requestID RequestID ) {
245+ func (c * datagramConn ) handleSessionMigration (requestID RequestID , logger * zerolog. Logger ) {
239246 // We need to migrate the currently running session to this edge connection.
240247 session , err := c .sessionManager .GetSession (requestID )
241248 if err != nil {
@@ -250,29 +257,29 @@ func (c *datagramConn) handleSessionMigration(requestID RequestID) {
250257 // Send another registration response since the session is already active
251258 err = c .SendUDPSessionResponse (requestID , ResponseOk )
252259 if err != nil {
253- c . logger .Err (err ).Msgf ("session registration failure: unable to send an additional session registration response" )
260+ logger .Err (err ).Msgf ("flow registration failure: unable to send an additional flow registration response" )
254261 return
255262 }
256263}
257264
258- func (c * datagramConn ) handleSessionRegistrationFailure (requestID RequestID ) {
265+ func (c * datagramConn ) handleSessionRegistrationFailure (requestID RequestID , logger * zerolog. Logger ) {
259266 err := c .SendUDPSessionResponse (requestID , ResponseUnableToBindSocket )
260267 if err != nil {
261- c . logger .Err (err ).Msgf ("unable to send session registration error response (%d)" , ResponseUnableToBindSocket )
268+ logger .Err (err ).Msgf ("unable to send flow registration error response (%d)" , ResponseUnableToBindSocket )
262269 }
263270}
264271
265272// Handles incoming datagrams that need to be sent to a registered session.
266- func (c * datagramConn ) handleSessionPayloadDatagram (datagram * UDPSessionPayloadDatagram ) {
273+ func (c * datagramConn ) handleSessionPayloadDatagram (datagram * UDPSessionPayloadDatagram , logger * zerolog. Logger ) {
267274 s , err := c .sessionManager .GetSession (datagram .RequestID )
268275 if err != nil {
269- c . logger .Err (err ).Msgf ("unable to find session " )
276+ logger .Err (err ).Msgf ("unable to find flow " )
270277 return
271278 }
272279 // We ignore the bytes written to the socket because any partial write must return an error.
273280 _ , err = s .Write (datagram .Payload )
274281 if err != nil {
275- c . logger .Err (err ).Msgf ("unable to write payload for unavailable session " )
282+ logger .Err (err ).Msgf ("unable to write payload for the flow " )
276283 return
277284 }
278285}
0 commit comments