-
Notifications
You must be signed in to change notification settings - Fork 1
/
statereaper.go
62 lines (50 loc) · 1.5 KB
/
statereaper.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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
package common
import (
"context"
"time"
"github.com/na4ma4/config"
"github.com/na4ma4/rsca/api"
"github.com/na4ma4/rsca/internal/state"
"go.uber.org/zap"
)
// StateReaper periodically checks the state store and deactivates old entries.
func StateReaper(
ctx context.Context,
cfg config.Conf,
logger *zap.Logger,
st state.State,
) func() error {
logger.Info("starting state reaper")
ticker := time.NewTicker(cfg.GetDuration("server.state-tick"))
return func() error {
for {
select {
case ts := <-ticker.C:
logger.Debug("state reaper tick received")
expireState := []string{}
expireTime := ts.Add(-1 * cfg.GetDuration("server.state-timeout")).UTC()
_ = st.Walk(func(in *api.Member) error {
if in != nil && in.GetLastSeen() != nil && in.GetActive() && !in.GetLastSeen().AsTime().After(expireTime) {
logger.Debug("adding host to inactive list",
zap.String("rsca.client.name", in.GetName()),
zap.Time("expireTime", expireTime),
zap.Time("lastseen", in.GetLastSeen().AsTime()),
)
expireState = append(expireState, in.GetName())
}
return nil
})
for k := range expireState {
logger.Info("deactivating host for inactivity", zap.String("rsca.client.name", expireState[k]))
if err := st.DeactivateByHostname(expireState[k]); err != nil {
logger.Error("unable to deactive member", zap.Error(err))
}
}
case <-ctx.Done():
logger.Debug("StateReaper Done()")
ticker.Stop()
return nil
}
}
}
}