]> git.giacomolli.uk - onechat/blobdiff - syncer.go
Order model and chat declarations
[onechat] / syncer.go
index 3da6d7a6dec0daa7ad2d6a9ae4e004445f694e91..0784393fcd4df9bbfc44e8d99877eaca9f3bac71 100644 (file)
--- a/syncer.go
+++ b/syncer.go
@@ -10,22 +10,6 @@ import (
        "golang.org/x/sys/unix"
 )
 
-// openSyncer returns the syncer of the file system that holds dir. It syncs
-// with syncfs, which writes back only that file system.
-func openSyncer(dir string) (*Syncer, error) {
-       file, err := os.Open(dir)
-       if err != nil {
-               return nil, fmt.Errorf("open chat directory: %w", err)
-       }
-
-       s := newSyncer(func() error {
-               return unix.Syncfs(int(file.Fd()))
-       })
-       s.file = file
-
-       return s, nil
-}
-
 // Syncer makes the writes of a chat durable by group commit. Each write goes to
 // the operating system at once, so a crash of the process loses nothing. After
 // each write, Written starts a background sync of the file system of the chat,
@@ -48,6 +32,30 @@ type Syncer struct {
        workers sync.WaitGroup
 }
 
+// openSyncer returns the syncer of the file system that holds dir. It syncs
+// with syncfs, which writes back only that file system.
+func openSyncer(dir string) (*Syncer, error) {
+       file, err := os.Open(dir)
+       if err != nil {
+               return nil, fmt.Errorf("open chat directory: %w", err)
+       }
+
+       s := newSyncer(func() error {
+               return unix.Syncfs(int(file.Fd()))
+       })
+       s.file = file
+
+       return s, nil
+}
+
+// newSyncer returns a syncer that makes the writes durable with sync.
+func newSyncer(sync func() error) *Syncer {
+       return &Syncer{
+               sync: sync,
+               done: make(chan struct{}),
+       }
+}
+
 // Written tells the syncer that a write went to the operating system, and
 // starts a sync when none runs.
 func (s *Syncer) Written() {
@@ -138,11 +146,3 @@ func (s *Syncer) run(target uint64) {
        s.done = make(chan struct{})
        s.start()
 }
-
-// newSyncer returns a syncer that makes the writes durable with sync.
-func newSyncer(sync func() error) *Syncer {
-       return &Syncer{
-               sync: sync,
-               done: make(chan struct{}),
-       }
-}