This repository was archived by the owner on Aug 7, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcursor.go
More file actions
95 lines (85 loc) · 2.69 KB
/
Copy pathcursor.go
File metadata and controls
95 lines (85 loc) · 2.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
package spool
import (
"encoding/binary"
"hash/fnv"
"os"
"path/filepath"
)
const (
cursorName = "cursor"
cursorLen = 24 // seq(8) + offset(8) + fnv64a checksum(8)
)
// cursor is the persisted read position: which segment, and how far into it.
type cursor struct{ seq, off int64 }
func cursorSum(b []byte) uint64 {
h := fnv.New64a()
_, _ = h.Write(b)
return h.Sum64()
}
// encode lays the cursor out for its in-place rewrite. The record carries a
// checksum precisely because it is rewritten in place: a partial write on power
// loss must fail its check and fall back to redelivering, rather than seek to a
// mixed old/new position that silently skips undelivered frames.
func (c cursor) encode() [cursorLen]byte {
var buf [cursorLen]byte
binary.BigEndian.PutUint64(buf[:8], uint64(c.seq))
binary.BigEndian.PutUint64(buf[8:16], uint64(c.off))
binary.BigEndian.PutUint64(buf[16:], cursorSum(buf[:16]))
return buf
}
// decodeCursor reports ok=false for a short or checksum-failing record — a torn
// in-place rewrite, or a fresh spool. The caller redelivers from the oldest
// segment.
func decodeCursor(b []byte) (cursor, bool) {
if len(b) < cursorLen || binary.BigEndian.Uint64(b[16:cursorLen]) != cursorSum(b[:16]) {
return cursor{}, false
}
return cursor{
seq: int64(binary.BigEndian.Uint64(b[:8])),
off: int64(binary.BigEndian.Uint64(b[8:16])),
}, true
}
// cursorFile is the open handle to dir/cursor.
type cursorFile struct {
f *os.File
// sync makes every store durable. The cursor is an optimisation, not a
// durability record — see Options.CommitSync for why this is a choice and
// what it costs.
sync bool
}
// openCursor opens or creates dir/cursor and returns the position it holds.
// found is false for a fresh, short, or torn record.
func openCursor(dir string, sync bool) (*cursorFile, cursor, bool, error) {
f, err := os.OpenFile(filepath.Join(dir, cursorName), os.O_RDWR|os.O_CREATE, 0o644)
if err != nil {
return nil, cursor{}, false, err
}
var buf [cursorLen]byte
n, _ := f.ReadAt(buf[:], 0)
cur, ok := decodeCursor(buf[:n])
return &cursorFile{f: f, sync: sync}, cur, ok, nil
}
// store rewrites the cursor in place. A torn rewrite fails its checksum on the
// next load and redelivers. It tolerates a nil receiver: three of its four call
// sites are error-recovery paths where the file may legitimately be closed.
func (c *cursorFile) store(cur cursor) error {
if c == nil || c.f == nil {
return nil
}
buf := cur.encode()
if _, err := c.f.WriteAt(buf[:], 0); err != nil {
return err
}
if !c.sync {
return nil
}
return c.f.Sync()
}
func (c *cursorFile) Close() error {
if c == nil || c.f == nil {
return nil
}
err := c.f.Close()
c.f = nil
return err
}