This repository has been archived by the owner on May 11, 2023. It is now read-only.
/
file_store.go
320 lines (269 loc) · 7.08 KB
/
file_store.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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
package filestore
import (
"context"
"os/user"
"path"
"sync"
"time"
"github.com/battlesnakeio/engine/controller"
"github.com/battlesnakeio/engine/controller/pb"
"github.com/battlesnakeio/engine/rules"
"github.com/gogo/protobuf/proto"
uuid "github.com/satori/go.uuid"
log "github.com/sirupsen/logrus"
)
func defaultDir() string {
return path.Join(homeDir(), ".battlesnake/games")
}
func homeDir() string {
usr, err := user.Current()
if err != nil {
return "."
}
return usr.HomeDir
}
// NewFileStore returns a file based store implementation (1 file per game).
func NewFileStore(directory string) controller.Store {
if directory == "" {
directory = defaultDir()
}
return &fileStore{
games: map[string]*pb.Game{},
frames: map[string][]*pb.GameFrame{},
writers: map[string]writer{},
locks: map[string]*lock{},
directory: directory,
}
}
type lock struct {
token string
expires time.Time
}
type fileStore struct {
games map[string]*pb.Game
frames map[string][]*pb.GameFrame
writers map[string]writer
locks map[string]*lock
lock sync.Mutex
directory string
}
// closeGame removes the game from in-memory cache and closes the handle to its
// file. Should be called when game is complete.
func (fs *fileStore) closeGame(id string) {
if w, ok := fs.writers[id]; ok {
err := w.Close()
if err != nil {
log.WithError(err).Error("Error while closing file writer")
}
}
delete(fs.games, id)
delete(fs.frames, id)
delete(fs.writers, id)
}
func (fs *fileStore) Lock(ctx context.Context, key, token string) (string, error) {
fs.lock.Lock()
defer fs.lock.Unlock()
now := time.Now()
l, ok := fs.locks[key]
if ok {
// We have a lock token, if it's expired just delete it and continue as
// if nothing happened.
if l.expires.Before(now) {
delete(fs.locks, key)
} else {
// If the token is not expired and matched our active token, let's
// just bump the expiration.
if l.token == token {
l.expires = time.Now().Add(controller.LockExpiry)
return l.token, nil
}
// If it's not our token, we should throw an error.
return "", controller.ErrIsLocked
}
}
if token == "" {
token = uuid.NewV4().String()
}
// Lock was expired or non-existant, create a new token.
l = &lock{
token: token,
expires: now.Add(controller.LockExpiry),
}
fs.locks[key] = l
return l.token, nil
}
func (fs *fileStore) isLocked(key string) bool {
l, ok := fs.locks[key]
return ok && l.expires.After(time.Now())
}
func (fs *fileStore) Unlock(ctx context.Context, key, token string) error {
fs.lock.Lock()
defer fs.lock.Unlock()
now := time.Now()
l, ok := fs.locks[key]
// No lock? Don't care.
if !ok {
return nil
}
// We have a lock that matches our token, even if it's expired we are safe
// to remove it. If it's expired, remove it as well.
if l.expires.Before(now) || l.token == token {
delete(fs.locks, key)
return nil
}
// The token is valid and doesn't match our lock.
return controller.ErrIsLocked
}
// PopGameID gives the next running game. Since running games should always be
// cached in memory it is not necessary to scan file system.
func (fs *fileStore) PopGameID(ctx context.Context) (string, error) {
fs.lock.Lock()
defer fs.lock.Unlock()
// For every game we need to make sure it's active and is not locked before
// returning it. We get randomness due to go's built in random map.
for id, g := range fs.games {
if !fs.isLocked(id) && g.Status == string(rules.GameStatusRunning) {
return id, nil
}
}
return "", controller.ErrNotFound
}
func (fs *fileStore) CreateGame(ctx context.Context, g *pb.Game, frames []*pb.GameFrame) error {
fs.lock.Lock()
defer fs.lock.Unlock()
fs.games[g.ID] = g
if len(frames) == 0 {
fs.frames[g.ID] = []*pb.GameFrame{}
return nil
}
return fs.appendFrames(g.ID, frames)
}
func (fs *fileStore) SetGameStatus(ctx context.Context, id string, status rules.GameStatus) error {
fs.lock.Lock()
defer fs.lock.Unlock()
game, err := fs.requireGame(id)
if err != nil {
return err
}
game.Status = string(status)
if status != rules.GameStatusRunning {
fs.closeGame(id)
}
return nil
}
func (fs *fileStore) PushGameFrame(ctx context.Context, id string, g *pb.GameFrame) error {
fs.lock.Lock()
defer fs.lock.Unlock()
return fs.appendFrame(id, g)
}
func (fs *fileStore) ListGameFrames(ctx context.Context, id string, limit, offset int) ([]*pb.GameFrame, error) {
fs.lock.Lock()
defer fs.lock.Unlock()
if _, err := fs.requireGame(id); err != nil {
return nil, err
}
frames, err := fs.requireFrames(id)
if err != nil {
return nil, err
}
if offset < 0 {
offset = len(frames) + offset
}
if len(frames) == 0 || offset >= len(frames) {
return nil, nil
}
if offset+limit >= len(frames) {
limit = len(frames) - offset
}
return frames[offset : offset+limit], nil
}
func (fs *fileStore) GetGame(ctx context.Context, id string) (*pb.Game, error) {
fs.lock.Lock()
defer fs.lock.Unlock()
g, err := fs.requireGame(id)
if err != nil {
return nil, err
}
// Clone the game, since this could be modified after this is returned
// and upset internal state inside the store.
clone := proto.Clone(g).(*pb.Game)
return clone, nil
}
func (fs *fileStore) requireHandle(id string, mustBeNew bool) (writer, error) {
if w, ok := fs.writers[id]; ok {
return w, nil
}
handle, err := openFileWriter(fs.directory, id, mustBeNew)
if err != nil {
return nil, err
}
fs.writers[id] = handle
return handle, nil
}
func (fs *fileStore) requireGame(id string) (*pb.Game, error) {
// Do nothing if game already loaded.
if g, ok := fs.games[id]; ok {
return g, nil
}
// Load frames from file.
g, err := ReadGameInfo(fs.directory, id)
if err != nil {
return nil, err
}
fs.games[id] = g
return g, nil
}
func (fs *fileStore) requireFrames(id string) ([]*pb.GameFrame, error) {
// Do nothing if frames already loaded.
if frames, ok := fs.frames[id]; ok {
return frames, nil
}
// Load frames from file.
frames, err := ReadGameFrames(fs.directory, id)
if err != nil {
return nil, err
}
fs.frames[id] = frames
return frames, nil
}
func (fs *fileStore) appendFrame(id string, f *pb.GameFrame) error {
game, err := fs.requireGame(id)
if err != nil {
return err
}
alreadyHasFrames := fs.hasAnyFrames(id)
handle, err := fs.requireHandle(id, !alreadyHasFrames)
if err != nil {
return err
}
// If this is the first frame, then first write the game info header.
if !alreadyHasFrames {
err := writeGameInfo(handle, game, f.Snakes)
if err != nil {
return err
}
}
// Add frame to in-memory cache
fs.frames[id] = append(fs.frames[id], f)
// Add frame to archive file
return writeFrame(handle, f)
}
func (fs *fileStore) appendFrames(gameID string, frames []*pb.GameFrame) error {
for _, f := range frames {
if err := fs.appendFrame(gameID, f); err != nil {
return err
}
}
return nil
}
func (fs *fileStore) hasAnyFrames(gameID string) bool {
frames, ok := fs.frames[gameID]
return ok && len(frames) > 0
}
type gameArchive struct {
game *pb.Game
frames []*pb.GameFrame
}
func getFilePath(directory string, id string) string {
return path.Join(directory, id) + ".bs"
}