]> git.giacomolli.uk - onechat/blob - syncer.go
Order model and chat declarations
[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 s.file = file
47
48 return s, nil
49 }
50
51 // newSyncer returns a syncer that makes the writes durable with sync.
52 func newSyncer(sync func() error) *Syncer {
53 return &Syncer{
54 sync: sync,
55 done: make(chan struct{}),
56 }
57 }
58
59 // Written tells the syncer that a write went to the operating system, and
60 // starts a sync when none runs.
61 func (s *Syncer) Written() {
62 s.mutex.Lock()
63 defer s.mutex.Unlock()
64
65 s.written++
66 s.start()
67 }
68
69 // Flush returns when every write before the call is durable.
70 func (s *Syncer) Flush(ctx context.Context) error {
71 s.mutex.Lock()
72
73 target := s.written
74 s.mutex.Unlock()
75
76 for {
77 s.mutex.Lock()
78
79 reached := s.durable >= target
80 failure := s.failure
81 done := s.done
82 s.mutex.Unlock()
83
84 if reached {
85 return nil
86 }
87
88 if failure != nil {
89 return failure
90 }
91
92 select {
93 case <-done:
94 case <-ctx.Done():
95 return ctx.Err()
96 }
97 }
98 }
99
100 // Close makes every write durable, waits for the syncs, and closes the
101 // directory.
102 func (s *Syncer) Close() error {
103 err := s.Flush(context.Background())
104 s.workers.Wait()
105
106 if s.file != nil {
107 err = errors.Join(err, s.file.Close())
108 s.file = nil
109 }
110
111 return err
112 }
113
114 // start starts a sync of the writes so far when none runs and a write is not
115 // durable. The caller holds the mutex.
116 func (s *Syncer) start() {
117 if s.running || s.durable == s.written || s.failure != nil {
118 return
119 }
120
121 s.running = true
122 s.workers.Add(1)
123
124 go s.run(s.written)
125 }
126
127 // run syncs once, so the writes up to target become durable, and starts the
128 // next sync when writes came during this one.
129 func (s *Syncer) run(target uint64) {
130 defer s.workers.Done()
131
132 err := s.sync()
133
134 s.mutex.Lock()
135 defer s.mutex.Unlock()
136
137 s.running = false
138 if err != nil {
139 s.failure = fmt.Errorf("sync chat files: %w", err)
140 } else {
141 s.durable = target
142 }
143
144 close(s.done)
145
146 s.done = make(chan struct{})
147 s.start()
148 }