-
Notifications
You must be signed in to change notification settings - Fork 0
/
pipelined_flush_writer.go
88 lines (73 loc) · 1.97 KB
/
pipelined_flush_writer.go
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
package parallelwriter
import (
"sync"
)
type pipelinedWriteAdapter struct {
w *PipelinedFlushWriter
}
func (a *pipelinedWriteAdapter) Write(buf []byte) (int, error) {
n, err := a.w.wf.Write(buf)
if err != nil {
return n, err
}
// Ensure that only one flush is running at a time, and block writes
// from completing unless it can start a flush.
a.w.flushLock.Lock()
flushCh := make(chan struct{})
a.w.lock.Lock()
a.w.flushDone = flushCh
a.w.lock.Unlock()
go func() {
defer a.w.flushLock.Unlock()
err := a.w.wf.Flush()
if err != nil {
a.w.lock.Lock()
a.w.flushErr = err
a.w.lock.Unlock()
}
close(flushCh)
}()
return n, err
}
// PipelinedFlushWriter is a writer that serialises and batches parallel
// writes, similar to Writer, but with two additional properties:
// 1. Flush is performed after every Write.
// 2. Flush and Write are pipelined, so that a Flush may occur in parallel
// with a Write, but Writes are serialised, and Flushes are serialised.
//
// This behaviour is particularly useful for write-ahead logs, where the caller
// wants to ensure writes are flushed to disk, but also wants to do many writes
// in parallel for high total throughput.
type PipelinedFlushWriter struct {
wf WriteFlusher
w *Writer
flushLock sync.Mutex
flushDone chan struct{}
flushErr error
lock sync.RWMutex
}
func NewPipelinedFlushWriter(wf WriteFlusher) *PipelinedFlushWriter {
pfw := &PipelinedFlushWriter{wf: wf}
pfw.w = NewWriter(&pipelinedWriteAdapter{w: pfw})
return pfw
}
// Write serialises, batches, and flushes writes to the underlying Writer.
// Parallel writes are atomic, and buffers are never interleaved or broken to
// the underlying Writer.
func (w *PipelinedFlushWriter) Write(buf []byte) (int, error) {
n, err := w.w.Write(buf)
if err != nil {
return n, err
}
w.lock.RLock()
defer w.lock.RUnlock()
ch := w.flushDone
w.lock.RUnlock()
<-ch
w.lock.RLock()
err = w.flushErr
if err != nil {
return 0, err
}
return n, nil
}