Pass the state-file flush to a subscription in its options
A subscription flushed its state file through a package variable, which the ntfy tests replaced with a no-op in their TestMain. Tests in other packages could not reach it, so the daemon's own tests still flushed for real. Options.Sync now takes the flush; nil means (*os.File).Sync, as before. The ntfy tests pass a no-op through their options and to saveState. Behaviour is unchanged. docs/plans/ntfy.md names the option, and its line references follow the code. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
c548833656
commit
62e5ec9c66
9 files changed
+49
-49
No files matched your search
+10
-15
@@ -75,16 +75,11 @@ func loadState(path string) (state, error) {
|
||||
return st, nil
|
||||
}
|
||||
|
||||
// syncFile flushes a file, or a directory, to disk. It is a variable so
|
||||
// that tests, which write state files hundreds of times, can skip the
|
||||
// flush; on a disk that is slow to sync, each one costs tens of
|
||||
// milliseconds.
|
||||
var syncFile = (*os.File).Sync
|
||||
|
||||
// saveState writes st to path atomically: to a temporary file in the same
|
||||
// directory, synced to disk, then renamed over path. A crash leaves the
|
||||
// old file or the new one, never part of either.
|
||||
func saveState(path string, st state) error {
|
||||
// directory, flushed to disk with sync, then renamed over path, and the
|
||||
// directory flushed after the rename. A crash leaves the old file or the
|
||||
// new one, never part of either.
|
||||
func saveState(path string, st state, sync func(*os.File) error) error {
|
||||
st.Version = stateVersion
|
||||
data, err := json.Marshal(st)
|
||||
if err != nil {
|
||||
@@ -100,7 +95,7 @@ func saveState(path string, st state) error {
|
||||
tmp := f.Name()
|
||||
_, err = f.Write(data)
|
||||
if err == nil {
|
||||
err = syncFile(f)
|
||||
err = sync(f)
|
||||
}
|
||||
if closeErr := f.Close(); err == nil {
|
||||
err = closeErr
|
||||
@@ -112,13 +107,13 @@ func saveState(path string, st state) error {
|
||||
_ = os.Remove(tmp)
|
||||
return err
|
||||
}
|
||||
syncDir(dir)
|
||||
syncDir(dir, sync)
|
||||
return nil
|
||||
}
|
||||
|
||||
// syncDir makes a rename in dir durable. Windows cannot open a directory
|
||||
// to sync it, and its renames need no such step.
|
||||
func syncDir(dir string) {
|
||||
// syncDir makes a rename in dir durable by flushing dir with sync. Windows
|
||||
// cannot open a directory to sync it, and its renames need no such step.
|
||||
func syncDir(dir string, sync func(*os.File) error) {
|
||||
if runtime.GOOS == "windows" {
|
||||
return
|
||||
}
|
||||
@@ -126,7 +121,7 @@ func syncDir(dir string) {
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
_ = syncFile(d)
|
||||
_ = sync(d)
|
||||
_ = d.Close()
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user