-
Notifications
You must be signed in to change notification settings - Fork 0
/
writer.go
260 lines (232 loc) · 4.95 KB
/
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
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
// Copyright (c) 2020 Meng Huang ([email protected])
// This package is licensed under a MIT license that can be found in the LICENSE file.
// Package writer implements buffering for an io.Writer object.
package writer
import (
"errors"
"github.com/hslam/atomic"
"github.com/hslam/buffer"
"io"
"sync"
"syscall"
)
const (
maximumSegmentSize = 65536
)
// ErrWriterClosed is returned by the Writer's Write methods after a call to Close.
var ErrWriterClosed = errors.New("Writer closed")
// Flusher is the interface that wraps the basic Flush method.
//
// Flush writes any buffered data to the underlying io.Writer.
type Flusher interface {
Flush() (err error)
}
// BufWriter is the interface that wraps the Writev method.
type BufWriter interface {
Writev(p [][]byte) (n int, err error)
Write(p []byte) (n int, err error)
}
type bufWriter struct {
writer io.Writer
}
func (bw *bufWriter) Writev(c [][]byte) (n int, err error) {
for _, b := range c {
var written int
written, err = bw.writer.Write(b)
n += written
}
return
}
func (bw *bufWriter) Write(p []byte) (n int, err error) {
return bw.writer.Write(p)
}
// NewFdWriter returns a BufWriter.
func NewFdWriter(fd int) BufWriter {
return &fdWriter{
fd: fd,
}
}
type fdWriter struct {
fd int
}
func (bw *fdWriter) Writev(c [][]byte) (n int, err error) {
return Writev(bw.fd, c)
}
func (bw *fdWriter) Write(p []byte) (n int, err error) {
return Writev(bw.fd, [][]byte{p})
}
// ErrSyscallConn will be returned when the net.Conn do not implement the syscall.Conn interface.
var ErrSyscallConn = errors.New("The net.Conn do not implement the syscall.Conn interface")
func syscallFd(w io.Writer) (int, error) {
c, ok := w.(syscall.Conn)
if !ok {
return 0, ErrSyscallConn
}
return fileDescriptor(c)
}
func fileDescriptor(c syscall.Conn) (int, error) {
var descriptor int
raw, err := c.SyscallConn()
if err != nil {
return 0, err
}
raw.Control(func(fd uintptr) {
descriptor = int(fd)
})
return descriptor, nil
}
// Writer implements buffering for an io.Writer object.
type Writer struct {
lock sync.Mutex
writing *atomic.Bool
bw BufWriter
mss int
pool *buffer.Pool
buffer []byte
size int
buffers [][]byte
running bool
closed *atomic.Bool
}
// NewWriter returns a new buffered writer whose buffer has at least the specified size.
func NewWriter(writer io.Writer, size int) *Writer {
if size < 1 {
size = maximumSegmentSize
}
w := &Writer{
mss: size,
pool: buffer.AssignPool(size),
writing: atomic.NewBool(false),
closed: atomic.NewBool(false),
}
if bw, ok := writer.(BufWriter); ok {
w.bw = bw
} else {
fd, err := syscallFd(writer)
if err == nil {
w.bw = NewFdWriter(fd)
} else {
w.bw = &bufWriter{
writer: writer,
}
}
}
return w
}
// Write writes the contents of p into the buffer or the underlying io.Writer.
// It returns the number of bytes written.
func (w *Writer) Write(p []byte) (n int, err error) {
if w.closed.Load() {
return 0, ErrWriterClosed
}
w.lock.Lock()
if !w.running {
w.running = true
w.append(p)
go w.run()
} else {
w.append(p)
}
w.lock.Unlock()
if err == nil {
n = len(p)
}
return n, err
}
func (w *Writer) append(p []byte) {
length := len(p)
w.checkBuffer()
retain := length
for retain > w.mss {
n := copy(w.buffer[w.size:w.mss], p[length-retain:])
w.size = w.mss
w.push()
w.checkBuffer()
retain -= n
}
if retain > 0 {
if w.size+retain > w.mss {
w.push()
w.checkBuffer()
}
copy(w.buffer[w.size:], p[length-retain:])
w.size += retain
}
}
func (w *Writer) push() {
buffer := w.buffer[:w.size]
w.size = 0
w.buffers = append(w.buffers, buffer)
w.buffer = nil
}
func (w *Writer) checkBuffer() {
if cap(w.buffer) == 0 {
w.buffer = w.pool.GetBuffer(w.mss)
}
}
// Flush writes any buffered data to the underlying io.Writer.
func (w *Writer) Flush() error {
w.lock.Lock()
for !w.writing.CompareAndSwap(false, true) {
w.lock.Unlock()
w.lock.Lock()
}
var c [][]byte
if w.buffered() {
c = w.cache()
}
w.lock.Unlock()
err := w.flush(c)
w.writing.Store(false)
return err
}
func (w *Writer) buffered() bool {
return len(w.buffers) > 0 || w.size > 0
}
func (w *Writer) cache() (c [][]byte) {
if w.size > 0 {
w.push()
}
w.size = 0
c = w.buffers
w.buffers = nil
return
}
func (w *Writer) flush(c [][]byte) (err error) {
if len(c) == 1 {
b := c[0]
_, err = w.bw.Write(b)
w.pool.PutBuffer(b)
} else if len(c) > 1 {
_, err = w.bw.Writev(c)
for _, b := range c {
w.pool.PutBuffer(b)
}
}
return
}
func (w *Writer) run() {
for {
w.lock.Lock()
if !w.buffered() {
w.running = false
w.lock.Unlock()
return
}
if w.writing.CompareAndSwap(false, true) {
c := w.cache()
w.lock.Unlock()
w.flush(c)
w.writing.Store(false)
} else {
w.lock.Unlock()
}
}
}
// Close closes the writer, but do not close the underlying io.Writer
func (w *Writer) Close() (err error) {
if !w.closed.CompareAndSwap(false, true) {
return nil
}
return w.Flush()
}