feat: status api
This commit is contained in:
+34
-5
@@ -161,15 +161,44 @@ func (f *fs) Write(path string, v encoding.BinaryMarshaler) error {
|
||||
}
|
||||
|
||||
func (f *fs) Read(path string, v encoding.BinaryUnmarshaler) error {
|
||||
if v == nil {
|
||||
return errors.New("can't unmarshal to a nil object")
|
||||
}
|
||||
|
||||
if f.lock == nil {
|
||||
return errors.New("lock must be acquired before read")
|
||||
}
|
||||
|
||||
targetFilePath := filepath.Join(f.root, path)
|
||||
return f.readUnsafe(path, v)
|
||||
}
|
||||
|
||||
func (f *fs) ReadSafe(path string, v encoding.BinaryUnmarshaler) error {
|
||||
if f.lock != nil {
|
||||
timeout := time.NewTimer(time.Millisecond * 100)
|
||||
checker := time.NewTicker(time.Millisecond)
|
||||
out:
|
||||
for {
|
||||
select {
|
||||
case <-checker.C:
|
||||
if f.lock == nil {
|
||||
checker.Stop()
|
||||
timeout.Stop()
|
||||
break out
|
||||
}
|
||||
case <-timeout.C:
|
||||
checker.Stop()
|
||||
return errors.New("lock acquired, timeout waiting for release")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return f.readUnsafe(path, v)
|
||||
}
|
||||
|
||||
// readUnsafe reads the file contents without locking mechanism in mind.
|
||||
// Using readUnsafe directly may cause errors if file being written at the moment.
|
||||
func (f *fs) readUnsafe(file string, v encoding.BinaryUnmarshaler) error {
|
||||
if v == nil {
|
||||
return errors.New("can't unmarshal to a nil object")
|
||||
}
|
||||
|
||||
targetFilePath := filepath.Join(f.root, file)
|
||||
if targetFilePath == f.lockFilePath() {
|
||||
return errors.New("can't read from the lock file")
|
||||
}
|
||||
|
||||
+15
-5
@@ -58,10 +58,14 @@ func saveState(s Storage, g game.Game) error {
|
||||
}
|
||||
|
||||
func (r *repo) LoadState() (game.Game, error) {
|
||||
return loadState(r.s)
|
||||
return loadState(r.s, true)
|
||||
}
|
||||
|
||||
func loadState(s Storage) (game.Game, error) {
|
||||
func (r *repo) LoadStateSafe() (game.Game, error) {
|
||||
return loadState(r.s, false)
|
||||
}
|
||||
|
||||
func loadState(s Storage, locked bool) (game.Game, error) {
|
||||
var g game.Game
|
||||
path := statePath
|
||||
exist, err := s.Exists(path)
|
||||
@@ -69,10 +73,16 @@ func loadState(s Storage) (game.Game, error) {
|
||||
return g, NewStorageError(err)
|
||||
}
|
||||
if !exist {
|
||||
return g, NewStateError("latest state was never stored")
|
||||
return g, NewGameNotInitializedError()
|
||||
}
|
||||
if err := s.Read(path, &g); err != nil {
|
||||
return g, NewStorageError(err)
|
||||
if locked {
|
||||
if err := s.Read(path, &g); err != nil {
|
||||
return g, NewStorageError(err)
|
||||
}
|
||||
} else {
|
||||
if err := s.ReadSafe(path, &g); err != nil {
|
||||
return g, NewStorageError(err)
|
||||
}
|
||||
}
|
||||
return g, nil
|
||||
}
|
||||
|
||||
@@ -12,6 +12,10 @@ func NewStorageError(err error) error {
|
||||
return e.NewRepoError(err)
|
||||
}
|
||||
|
||||
func NewGameNotInitializedError() error {
|
||||
return e.NewGameNotInitializedError()
|
||||
}
|
||||
|
||||
func NewStateError(msg string) error {
|
||||
return e.NewGameStateError(msg)
|
||||
}
|
||||
@@ -21,6 +25,7 @@ type Storage interface {
|
||||
Exists(string) (bool, error)
|
||||
Write(string, encoding.BinaryMarshaler) error
|
||||
Read(string, encoding.BinaryUnmarshaler) error
|
||||
ReadSafe(string, encoding.BinaryUnmarshaler) error
|
||||
}
|
||||
|
||||
type repo struct {
|
||||
|
||||
Reference in New Issue
Block a user