github.com/asynkron/protoactor-go@v0.0.0-20240308120642-ef91a6abee75/actor/child_restart_stats.go (about) 1 package actor 2 3 import ( 4 "time" 5 ) 6 7 // RestartStatistics keeps track of how many times an actor have restarted and when 8 type RestartStatistics struct { 9 failureTimes []time.Time 10 } 11 12 // NewRestartStatistics construct a RestartStatistics 13 func NewRestartStatistics() *RestartStatistics { 14 return &RestartStatistics{[]time.Time{}} 15 } 16 17 // FailureCount returns failure count 18 func (rs *RestartStatistics) FailureCount() int { 19 return len(rs.failureTimes) 20 } 21 22 // Fail increases the associated actors' failure count 23 func (rs *RestartStatistics) Fail() { 24 rs.failureTimes = append(rs.failureTimes, time.Now()) 25 } 26 27 // Reset the associated actors' failure count 28 func (rs *RestartStatistics) Reset() { 29 rs.failureTimes = []time.Time{} 30 } 31 32 // NumberOfFailures returns number of failures within a given duration 33 func (rs *RestartStatistics) NumberOfFailures(withinDuration time.Duration) int { 34 if withinDuration == 0 { 35 return len(rs.failureTimes) 36 } 37 38 num := 0 39 currTime := time.Now() 40 41 for _, t := range rs.failureTimes { 42 if currTime.Sub(t) < withinDuration { 43 num++ 44 } 45 } 46 47 return num 48 }