You are an expert in writing code in many different languages. Your goal is to perform code completion for the following code, keeping in mind the rest of the code and the file meta data.
    Metadata details:
    filename: slinstance.go
    path of file: ./repos/cockroach/pkg/sql/sqlliveness/slinstance
    
    the code of the file until where you have to start completion: // Copyright 2020 The Cockroach Authors.
//
// Use of this software is governed by the Business Source License
// included in the file licenses/BSL.txt.
//
// As of the Change Date specified in that file, in accordance with
// the Business Source License, use of this software will be govern
    
func (l *Instance) heartbeatLoopInner(ctx context.Context) error {
	defer func() {
		log.Warning(ctx, "exiting heartbeat loop")
	}()
	// Operations below retry endlessly after the stopper started quiescing if we
	// don't cancel their ctx.
	ctx, cancel := l.stopper.WithCancelOnQuiesce(ctx)
	defer cancel()
	var t timeutil.Timer
	defer t.Stop()

	t.Reset(0)
	for {
		select {
		case <-l.drain:
			return stop.ErrUnavailable
		case <-ctx.Done():
			return stop.ErrUnavailable
		case <-t.C:
			t.Read = true

			var s *session
			l.mu.Lock()
			s = l.mu.s
			l.mu.Unlock()

			// If we don't currently have a session, create one.
			if s == nil {
				newSession, err := l.createSession(ctx)
				if err != nil {
					return err
				}
				l.setSession(newSession)
				t.Reset(l.hb())
				continue
			}

			// Extend the session.
			found, err := l.extendSession(ctx, s)
			if err != nil {
				// extendSession only ever returns an error on a canceled ctx.
				return err
			}
			if !found {
				// Someone deleted our session record. Let's clear our state and proceed
				// to create a new session, unless the event handler wants to shut down
				// instead.
				if !l.clearSession(ctx) {
					return errors.New("session record deleted")
				}
				t.Reset(0)
				continue
			}
			log.VEventf(ctx, 2, "extended SQL liveness session %s", s.ID())
			t.Reset(l.hb())
		}
	}
}