To catch a crash loop the supervisor needs to know not just how many times a service restarted, but how many times recently. Today you record the timestamp of every restart and count how many fall inside a trailing time window.
Record each restart's time and count how many happened within the last window T.
A total restart count cannot distinguish a service that crashed three times over a
week from one that crashed three times in five seconds - but only the second is a
crash loop. What matters is the rate: how many restarts happened recently.
So the supervisor keeps a timestamp for each restart and, to judge health, counts
only those inside a trailing window - here the last 60 seconds. This is exactly
supervisord’s startretries measured over a period, and OTP’s “maximum restart
intensity within a period.”
The subtlety is the window edge. A restart is “recent” only if its timestamp is at
or after Now() - T; older ones have aged out and no longer count against the
service. Pin both sides: an old restart that is still just inside the window counts,
and the same restart one tick later, once it falls outside, does not. Getting that
cutoff exact is what makes the give-up rule in the next lesson trigger at the right
moment instead of one crash early or late.
// append s.clock.Now() to svc.Restarts each time Tick performs a restartfunc (s *Supervisor) RecentRestarts(svc *Service) int {cutoff := s.clock.Now() - 60*time.Secondn := 0for _, t := range svc.Restarts { if t >= cutoff { n++ } }return n}