code: func (s *remoteSite) handleHeartbeat(conn *remoteConn, ch ssh.Channel, reqC <-chan *ssh.Request) {
  	defer func() {
  		s.Infof("Cluster connection closed.")
  		conn.Close()
  	}()
  	for {
  		select {
  		case <-s.ctx.Done():
  			s.Infof("closing")
  			return
  		case req := <-reqC:
  			if req == nil {
  				s.Infof("Cluster agent disconnected.")
  				conn.markInvalid(trace.ConnectionProblem(nil, "agent disconnected"))
  				if !s.hasValidConnections() {
  					s.Debugf("Deleting connection record.")
  					s.deleteConnectionRecord()
  				}
  				return
  			}
  			var timeSent time.Time
  			var roundtrip time.Duration
  			if req.Payload != nil {
  				if err := timeSent.UnmarshalText(req.Payload); err == nil {
  					roundtrip = s.srv.Clock.Now().Sub(timeSent)
  				}
  			}
  			if roundtrip != 0 {
  				s.WithFields(log.Fields{"latency": roundtrip}).Debugf("ping <- %v", conn.conn.RemoteAddr())
  			} else {
  				s.Debugf("Ping <- %v.", conn.conn.RemoteAddr())
  			}
  			tm := time.Now().UTC()
  			conn.setLastHeartbeat(tm)
  			go s.registerHeartbeat(tm)
  		// since we block on select, time.After is re-created everytime we process a request.
  		case <-time.After(defaults.ReverseTunnelOfflineThreshold):
  			conn.markInvalid(trace.ConnectionProblem(nil, "no heartbeats for %v", defaults.ReverseTunnelOfflineThreshold))
  		}
  	}
  }
docs: // handleHearbeat receives heartbeat messages from the connected agent
  // if the agent has missed several heartbeats in a row, Proxy marks
  // the connection as invalid.