-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathexample.go
More file actions
48 lines (41 loc) · 945 Bytes
/
example.go
File metadata and controls
48 lines (41 loc) · 945 Bytes
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
package main
import (
"context"
"log/slog"
"os"
"time"
"github.com/stym06/rebuf/rebuf"
)
func writeToStdout(data []byte) error {
slog.Info(string(data))
return nil
}
func main() {
logger := slog.New(slog.NewTextHandler(os.Stdout, nil))
r, err := rebuf.New(
context.Background(),
"./data",
rebuf.WithMaxLogSize(50),
rebuf.WithMaxSegments(5),
rebuf.WithFsyncTime(5*time.Second),
rebuf.WithSyncStrategy(rebuf.SyncEveryWrite),
rebuf.WithLogger(logger),
)
if err != nil {
logger.Error("failed to create rebuf", "error", err)
os.Exit(1)
}
defer r.Close()
// Write entries.
for i := 0; i < 30; i++ {
logger.Info("writing data", "iter", i)
if err := r.Write([]byte("Hello world")); err != nil {
logger.Error("write failed", "error", err)
}
time.Sleep(300 * time.Millisecond)
}
// Replay all entries.
if err := r.Replay(writeToStdout); err != nil {
logger.Error("replay failed", "error", err)
}
}