@@ -202,33 +202,41 @@ const (
202202func (s * Storage ) isAlive (
203203 ctx context.Context , sid sqlliveness.SessionID , syncOrAsync readType ,
204204) (alive bool , _ error ) {
205- s .mu .Lock ()
206- if ! s .mu .started {
207- s .mu .Unlock ()
208- return false , sqlliveness .NotStartedError
209- }
210- if _ , ok := s .mu .deadSessions .Get (sid ); ok {
211- s .mu .Unlock ()
212- s .metrics .IsAliveCacheHits .Inc (1 )
213- return false , nil
214- }
215- if expiration , ok := s .mu .liveSessions .Get (sid ); ok {
216- expiration := expiration .(hlc.Timestamp )
217- // The record exists and is valid.
218- if s .clock .Now ().Less (expiration ) {
219- s .mu .Unlock ()
205+
206+ // If wait is false, alive is set and future is unset.
207+ // If wait is true, alive is unset and future is set.
208+ alive , wait , future , err := func () (bool , bool , singleflight.Future , error ) {
209+ s .mu .Lock ()
210+ defer s .mu .Unlock ()
211+
212+ if ! s .mu .started {
213+ return false , false , singleflight.Future {}, sqlliveness .NotStartedError
214+ }
215+ if _ , ok := s .mu .deadSessions .Get (sid ); ok {
220216 s .metrics .IsAliveCacheHits .Inc (1 )
221- return true , nil
217+ return false , false , singleflight. Future {} , nil
222218 }
223- }
219+ if expiration , ok := s .mu .liveSessions .Get (sid ); ok {
220+ expiration := expiration .(hlc.Timestamp )
221+ // The record exists and is valid.
222+ if s .clock .Now ().Less (expiration ) {
223+ s .metrics .IsAliveCacheHits .Inc (1 )
224+ return true , false , singleflight.Future {}, nil
225+ }
226+ }
227+
228+ // We think that the session is expired; check, and maybe delete it.
229+ future := s .deleteOrFetchSessionSingleFlightLocked (ctx , sid )
224230
225- // We think that the session is expired; check, and maybe delete it.
226- future := s .deleteOrFetchSessionSingleFlightLocked (ctx , sid )
231+ // At this point, we know that the singleflight goroutine has been launched.
232+ // Releasing the lock when we return ensures that callers will either join
233+ // the singleflight or see the result.
234+ return false , true , future , nil
235+ }()
236+ if err != nil || ! wait {
237+ return alive , err
238+ }
227239
228- // At this point, we know that the singleflight goroutine has been launched.
229- // Releasing the lock here ensures that callers will either join the single-
230- // flight or see the result.
231- s .mu .Unlock ()
232240 s .metrics .IsAliveCacheMisses .Inc (1 )
233241
234242 // If we do not want to wait for the result, assume that the session is
0 commit comments