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