-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstate.go
122 lines (101 loc) · 2.3 KB
/
state.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
package main
import (
"encoding/gob"
"errors"
"log/slog"
"os"
"sync"
"time"
)
// State represents the state of the application
type State struct {
LastRequest time.Time
ImageRedraws int
ImageRequests int
BotRequests int
}
func (s State) LogValue() slog.Value {
return slog.GroupValue(
slog.Time("last_request", s.LastRequest),
slog.Int("image_redraws", s.ImageRedraws),
slog.Int("image_requests", s.ImageRequests),
)
}
// StateManager handles synchronization and persistence of application state.
type StateManager struct {
sync.Mutex
state *State
filename string
}
func NewStateManager(filename string, interval time.Duration) (*StateManager, error) {
sm := StateManager{
state: &State{},
filename: filename,
}
go sm.autoSave(interval)
if err := sm.Load(); err != nil {
return &sm, err
}
return &sm, nil
}
// autoSave periodically saves the current state to a file at the given interval.
func (sm *StateManager) autoSave(interval time.Duration) {
ticker := time.NewTicker(interval)
for range ticker.C {
err := sm.Save()
if err != nil {
slog.Error("failed to save state", "error", err)
}
slog.Debug("automatically saved state", "state", sm.state, "interval", interval)
}
}
func (sm *StateManager) Load() error {
sm.Lock()
defer sm.Unlock()
f, err := os.Open(sm.filename)
// If the state file doesn't exist, save empty state
if errors.Is(err, os.ErrNotExist) {
return sm.save()
}
if err = gob.NewDecoder(f).Decode(&sm.state); err != nil {
slog.Error("failed to decode state", "error", err)
err = os.Remove(sm.filename)
if err != nil {
return err
}
return sm.save()
}
return nil
}
func (sm *StateManager) save() error {
f, err := os.Create(sm.filename)
if err != nil {
return err
}
return gob.NewEncoder(f).Encode(sm.state)
}
func (sm *StateManager) Save() error {
sm.Lock()
defer sm.Unlock()
return sm.save()
}
func (sm *StateManager) SetLastRequest(dt time.Time) {
sm.Lock()
defer sm.Unlock()
sm.state.LastRequest = dt
}
func (sm *StateManager) IncrementImageRedraws() {
sm.Lock()
defer sm.Unlock()
sm.state.ImageRedraws++
}
func (sm *StateManager) IncrementImageRequests() {
sm.Lock()
defer sm.Unlock()
sm.state.ImageRequests++
}
func (sm *StateManager) IncrementBotRequests() {
sm.Lock()
defer sm.Unlock()
sm.state.BotRequests++
}