@@ -139,10 +139,12 @@ func (s *session) newStream(
139139
140140 var client Ydb_Coordination_V1.CoordinationService_SessionClient
141141 if lastChance {
142+ timer := time .NewTimer (s .options .SessionKeepAliveTimeout )
142143 select {
143- case <- time . After ( s . options . SessionKeepAliveTimeout ) :
144+ case <- timer . C :
144145 case client = <- result :
145146 }
147+ timer .Stop ()
146148
147149 if client != nil {
148150 return client , nil
@@ -175,10 +177,12 @@ func (s *session) newStream(
175177 }
176178
177179 // Waiting for some time before trying to reconnect.
180+ sessionReconnectDelay := time .NewTimer (s .options .SessionReconnectDelay )
178181 select {
179- case <- time . After ( s . options . SessionReconnectDelay ) :
182+ case <- sessionReconnectDelay . C :
180183 case <- s .ctx .Done ():
181184 }
185+ sessionReconnectDelay .Stop ()
182186
183187 if s .ctx .Err () != nil {
184188 // Give this session the last chance to stop gracefully if the session is canceled in the reconnect cycle.
@@ -247,6 +251,7 @@ func (s *session) mainLoop(path string, sessionStartedChan chan struct{}) {
247251
248252 // Wait for the session started response unless the stream context is done. We intentionally do not take into
249253 // account stream context cancellation in order to proceed with the graceful shutdown if it requires reconnect.
254+ sessionStartTimer := time .NewTimer (s .options .SessionStartTimeout )
250255 select {
251256 case start := <- sessionStarted :
252257 trace .CoordinationOnSessionStarted (s .client .config .Trace (), start .GetSessionId (), s .sessionID )
@@ -258,13 +263,14 @@ func (s *session) mainLoop(path string, sessionStartedChan chan struct{}) {
258263 cancelStream ()
259264 }
260265 close (startSending )
261- case <- time . After ( s . options . SessionStartTimeout ) :
266+ case <- sessionStartTimer . C :
262267 // Reconnect if no response was received before the timeout occurred.
263268 trace .CoordinationOnSessionStartTimeout (s .client .config .Trace (), s .options .SessionStartTimeout )
264269 cancelStream ()
265270 case <- streamCtx .Done ():
266271 case <- s .ctx .Done ():
267272 }
273+ sessionStartTimer .Stop ()
268274
269275 for {
270276 // Respect the failure reason priority: if the session context is done, we must stop the session, even
@@ -280,8 +286,9 @@ func (s *session) mainLoop(path string, sessionStartedChan chan struct{}) {
280286 }
281287
282288 keepAliveTime := time .Until (s .getLastGoodResponseTime ().Add (s .options .SessionKeepAliveTimeout ))
289+ keepAliveTimeTimer := time .NewTimer (keepAliveTime )
283290 select {
284- case <- time . After ( keepAliveTime ) :
291+ case <- keepAliveTimeTimer . C :
285292 last := s .getLastGoodResponseTime ()
286293 if time .Since (last ) > s .options .SessionKeepAliveTimeout {
287294 // Reconnect if the underlying stream is likely to be dead.
@@ -295,6 +302,7 @@ func (s *session) mainLoop(path string, sessionStartedChan chan struct{}) {
295302 case <- streamCtx .Done ():
296303 case <- s .ctx .Done ():
297304 }
305+ keepAliveTimeTimer .Stop ()
298306 }
299307
300308 if closing {
@@ -318,8 +326,10 @@ func (s *session) mainLoop(path string, sessionStartedChan chan struct{}) {
318326 )
319327
320328 // Wait for the session stopped response unless the stream context is done.
329+ sessionStopTimeout := time .NewTimer (s .options .SessionStopTimeout )
321330 select {
322331 case stop := <- sessionStopped :
332+ sessionStopTimeout .Stop ()
323333 trace .CoordinationOnSessionStopped (s .client .config .Trace (), stop .GetSessionId (), s .sessionID )
324334 if stop .GetSessionId () == s .sessionID {
325335 cancelStream ()
@@ -329,15 +339,19 @@ func (s *session) mainLoop(path string, sessionStartedChan chan struct{}) {
329339
330340 // Reconnect if the server response is invalid.
331341 cancelStream ()
332- case <- time .After (s .options .SessionStopTimeout ):
342+ case <- sessionStopTimeout .C :
343+ sessionStopTimeout .Stop () // no really need, call stop for common style only
344+
333345 // Reconnect if no response was received before the timeout occurred.
334346 trace .CoordinationOnSessionStopTimeout (s .client .config .Trace (), s .options .SessionStopTimeout )
335347 cancelStream ()
336348 case <- s .ctx .Done ():
349+ sessionStopTimeout .Stop ()
337350 cancelStream ()
338351
339352 return
340353 case <- streamCtx .Done ():
354+ sessionStopTimeout .Stop ()
341355 }
342356 }
343357
0 commit comments