File tree 3 files changed +38
-2
lines changed
3 files changed +38
-2
lines changed Original file line number Diff line number Diff line change 1
1
package wal
2
2
3
- import "os"
3
+ import (
4
+ "os"
5
+ "time"
6
+ )
4
7
5
8
// Options represents the configuration options for a Write-Ahead Log (WAL).
6
9
type Options struct {
@@ -29,6 +32,10 @@ type Options struct {
29
32
30
33
// BytesPerSync specifies the number of bytes to write before calling fsync.
31
34
BytesPerSync uint32
35
+
36
+ // SyncInterval is the time duration in which explicit synchronization is performed.
37
+ // If SyncInterval is zero, no periodic synchronization is performed.
38
+ SyncInterval time.Duration
32
39
}
33
40
34
41
const (
@@ -44,4 +51,5 @@ var DefaultOptions = Options{
44
51
SegmentFileExt : ".SEG" ,
45
52
Sync : false ,
46
53
BytesPerSync : 0 ,
54
+ SyncInterval : 0 ,
47
55
}
Original file line number Diff line number Diff line change 9
9
"sort"
10
10
"strings"
11
11
"sync"
12
+ "time"
12
13
)
13
14
14
15
const (
@@ -40,6 +41,8 @@ type WAL struct {
40
41
pendingWrites [][]byte
41
42
pendingSize int64
42
43
pendingWritesLock sync.Mutex
44
+ closeC chan struct {}
45
+ syncTicker * time.Ticker
43
46
}
44
47
45
48
// Reader represents a reader for the WAL.
@@ -64,6 +67,7 @@ func Open(options Options) (*WAL, error) {
64
67
options : options ,
65
68
olderSegments : make (map [SegmentID ]* segment ),
66
69
pendingWrites : make ([][]byte , 0 ),
70
+ closeC : make (chan struct {}),
67
71
}
68
72
69
73
// create the directory if not exists.
@@ -117,6 +121,22 @@ func Open(options Options) (*WAL, error) {
117
121
}
118
122
}
119
123
124
+ // only start the sync operation if the SyncInterval is greater than 0.
125
+ if wal .options .SyncInterval > 0 {
126
+ wal .syncTicker = time .NewTicker (wal .options .SyncInterval )
127
+ go func () {
128
+ for {
129
+ select {
130
+ case <- wal .syncTicker .C :
131
+ _ = wal .Sync ()
132
+ case <- wal .closeC :
133
+ wal .syncTicker .Stop ()
134
+ return
135
+ }
136
+ }
137
+ }()
138
+ }
139
+
120
140
return wal , nil
121
141
}
122
142
@@ -428,6 +448,13 @@ func (wal *WAL) Close() error {
428
448
wal .mu .Lock ()
429
449
defer wal .mu .Unlock ()
430
450
451
+ select {
452
+ case <- wal .closeC :
453
+ // channel is already closed
454
+ default :
455
+ close (wal .closeC )
456
+ }
457
+
431
458
// close all segment files.
432
459
for _ , segment := range wal .olderSegments {
433
460
if err := segment .Close (); err != nil {
Original file line number Diff line number Diff line change 1
1
package wal
2
2
3
3
import (
4
- "github.com/stretchr/testify/assert"
5
4
"io"
6
5
"os"
7
6
"strings"
8
7
"testing"
8
+
9
+ "github.com/stretchr/testify/assert"
9
10
)
10
11
11
12
func destroyWAL (wal * WAL ) {
You can’t perform that action at this time.
0 commit comments