aboutsummaryrefslogtreecommitdiff
path: root/weed/util/buffered_writer/max_latency_writer.go
blob: d95d26ffdc35ef8b9da50dd28eaf621e592b5619 (plain)
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
package buffered_writer

import (
	"io"
	"sync"
	"time"
)

type TimedWriteBuffer struct {
	maxLatencyWriterAt *maxLatencyWriterAt
	bufWriterAt        *bufferedWriterAt
}

func (t *TimedWriteBuffer) ReadAt(p []byte, off int64) (n int, err error) {
	bufStart := t.bufWriterAt.nextOffset - int64(t.bufWriterAt.dataSize)
	start := max(bufStart, off)
	stop := min(t.bufWriterAt.nextOffset, off+int64(len(p)))
	if start <= stop {
		n = copy(p, t.bufWriterAt.data[start-bufStart:stop-bufStart])
	}
	return
}
func (t *TimedWriteBuffer) WriteAt(p []byte, offset int64) (n int, err error) {
	return t.maxLatencyWriterAt.WriteAt(p, offset)
}
func (t *TimedWriteBuffer) Flush() {
	t.maxLatencyWriterAt.Flush()
}
func (t *TimedWriteBuffer) Close() {
	t.maxLatencyWriterAt.Close()
}

func NewTimedWriteBuffer(writerAt io.WriterAt, size int, latency time.Duration, currentOffset int64) *TimedWriteBuffer {
	bufWriterAt := newBufferedWriterAt(writerAt, size, currentOffset)
	maxLatencyWriterAt := newMaxLatencyWriterAt(bufWriterAt, latency)
	return &TimedWriteBuffer{
		bufWriterAt:        bufWriterAt,
		maxLatencyWriterAt: maxLatencyWriterAt,
	}
}

type bufferedWriterAt struct {
	data       []byte
	dataSize   int
	nextOffset int64
	writerAt   io.WriterAt
	counter    int
}

func newBufferedWriterAt(writerAt io.WriterAt, bufferSize int, currentOffset int64) *bufferedWriterAt {
	return &bufferedWriterAt{
		data:       make([]byte, bufferSize),
		nextOffset: currentOffset,
		dataSize:   0,
		writerAt:   writerAt,
	}
}

func (b *bufferedWriterAt) WriteAt(p []byte, offset int64) (n int, err error) {
	if b.nextOffset != offset {
		println("nextOffset", b.nextOffset, "bufSize", b.dataSize, "offset", offset, "data", len(p))
	}
	if b.nextOffset != offset || len(p)+b.dataSize > len(b.data) {
		if err = b.Flush(); err != nil {
			return 0, err
		}
	}
	if len(p)+b.dataSize > len(b.data) {
		n, err = b.writerAt.WriteAt(p, offset)
		if err == nil {
			b.nextOffset = offset + int64(n)
		}
	} else {
		n = copy(b.data[b.dataSize:len(p)+b.dataSize], p)
		b.dataSize += n
		b.nextOffset += int64(n)
		b.counter++
	}
	return
}

func (b *bufferedWriterAt) Flush() (err error) {
	if b.dataSize == 0 {
		return nil
	}
	// println("flush", b.counter)
	b.counter = 0
	_, err = b.writerAt.WriteAt(b.data[0:b.dataSize], b.nextOffset-int64(b.dataSize))
	if err == nil {
		b.dataSize = 0
	}
	return
}

// adapted from https://golang.org/src/net/http/httputil/reverseproxy.go

type writeFlusher interface {
	io.WriterAt
	Flush() error
}

type maxLatencyWriterAt struct {
	dst          writeFlusher
	latency      time.Duration // non-zero; negative means to flush immediately
	mu           sync.Mutex    // protects t, flushPending, and dst.Flush
	t            *time.Timer
	flushPending bool
}

func newMaxLatencyWriterAt(dst writeFlusher, latency time.Duration) *maxLatencyWriterAt {
	return &maxLatencyWriterAt{
		dst:     dst,
		latency: latency,
	}
}

func (m *maxLatencyWriterAt) WriteAt(p []byte, offset int64) (n int, err error) {
	m.mu.Lock()
	defer m.mu.Unlock()
	n, err = m.dst.WriteAt(p, offset)
	if m.latency < 0 {
		m.dst.Flush()
		return
	}
	if m.flushPending {
		return
	}
	if m.t == nil {
		m.t = time.AfterFunc(m.latency, m.Flush)
	} else {
		m.t.Reset(m.latency)
	}
	m.flushPending = true
	return
}

func (m *maxLatencyWriterAt) Flush() {
	m.mu.Lock()
	defer m.mu.Unlock()
	if !m.flushPending { // if stop was called but AfterFunc already started this goroutine
		return
	}
	m.dst.Flush()
	m.flushPending = false
}

func (m *maxLatencyWriterAt) Close() {
	m.mu.Lock()
	defer m.mu.Unlock()
	m.flushPending = false
	if m.t != nil {
		m.t.Stop()
	}
}

func min(x, y int64) int64 {
	if x <= y {
		return x
	}
	return y
}
func max(x, y int64) int64 {
	if x <= y {
		return y
	}
	return x
}