feat: status api

This commit is contained in:
Ilia Denisov
2026-01-07 18:38:06 +02:00
parent 1b0ab7a079
commit 204d3df8cf
20 changed files with 188 additions and 40 deletions
+34 -5
View File
@@ -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")
}