]> git.giacomolli.uk - onechat/blob - syncer.go
Separate multiline Go statements
[onechat] / syncer.go
1 package main
2
3 import (
4 "context"
5 "errors"
6 "fmt"
7 "os"
8 "sync"
9
10 "golang.org/x/sys/unix"
11 )
12
13 // Syncer makes the writes of a chat durable by group commit. Each write goes to
14 // the operating system at once, so a crash of the process loses nothing. After
15 // each write, Written starts a background sync of the file system of the chat,
16 // unless one runs already. Then the next sync starts when the running one ends.
17 // One sync makes every write before its start durable, so a sync covers many
18 // writes, and there is never more than one sync at a time. A power loss loses
19 // at most the writes after the start of the last finished sync.
20 //
21 // A failed sync can mean that the kernel dropped written data, so the error
22 // stays, and each later Flush returns it.
23 type Syncer struct {
24 mutex sync.Mutex
25 sync func() error
26 file *os.File
27 written uint64
28 durable uint64
29 running bool
30 done chan struct{}
31 failure error
32 workers sync.WaitGroup
33 }
34
35 // openSyncer returns the syncer of the file system that holds dir. It syncs
36 // with syncfs, which writes back only that file system.
37 func openSyncer(dir string) (*Syncer, error) {
38 file, err := os.Open(dir)
39 if err != nil {
40 return nil, fmt.Errorf("open chat directory: %w", err)
41 }
42
43 s := newSyncer(func() error {
44 return unix.Syncfs(int(file.Fd()))
45 })
46
47 s.file = file
48
49 return s, nil
50 }
51
52 // newSyncer returns a syncer that makes the writes durable with sync.
53 func newSyncer(sync func() error) *Syncer {
54 return &Syncer{
55 sync: sync,
56 done: make(chan struct{}),
57 }
58 }
59
60 // Written tells the syncer that a write went to the operating system, and
61 // starts a sync when none runs.
62 func (s *Syncer) Written() {
63 s.mutex.Lock()
64 defer s.mutex.Unlock()
65
66 s.written++
67 s.start()
68 }
69
70 // Flush returns when every write before the call is durable.
71 func (s *Syncer) Flush(ctx context.Context) error {
72 s.mutex.Lock()
73
74 target := s.written
75 s.mutex.Unlock()
76
77 for {
78 s.mutex.Lock()
79
80 reached := s.durable >= target
81 failure := s.failure
82 done := s.done
83 s.mutex.Unlock()
84
85 if reached {
86 return nil
87 }
88
89 if failure != nil {
90 return failure
91 }
92
93 select {
94 case <-done:
95 case <-ctx.Done():
96 return ctx.Err()
97 }
98 }
99 }
100
101 // Close makes every write durable, waits for the syncs, and closes the
102 // directory.
103 func (s *Syncer) Close() error {
104 err := s.Flush(context.Background())
105 s.workers.Wait()
106
107 if s.file != nil {
108 err = errors.Join(err, s.file.Close())
109 s.file = nil
110 }
111
112 return err
113 }
114
115 // start starts a sync of the writes so far when none runs and a write is not
116 // durable. The caller holds the mutex.
117 func (s *Syncer) start() {
118 if s.running || s.durable == s.written || s.failure != nil {
119 return
120 }
121
122 s.running = true
123 s.workers.Add(1)
124
125 go s.run(s.written)
126 }
127
128 // run syncs once, so the writes up to target become durable, and starts the
129 // next sync when writes came during this one.
130 func (s *Syncer) run(target uint64) {
131 defer s.workers.Done()
132
133 err := s.sync()
134
135 s.mutex.Lock()
136 defer s.mutex.Unlock()
137
138 s.running = false
139 if err != nil {
140 s.failure = fmt.Errorf("sync chat files: %w", err)
141 } else {
142 s.durable = target
143 }
144
145 close(s.done)
146
147 s.done = make(chan struct{})
148 s.start()
149 }