This repository has been archived by the owner on Feb 12, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbufwriter.go
119 lines (111 loc) · 3.23 KB
/
bufwriter.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
// like bufio.Writer (Copyright 2009 The Go Authors. All rights reserved.)
// but with instrumentation around flushing
// because this codebase is the only user:
// * doesn't implement the entire bufio.Writer api because it doesn't need to
// * and some simplifications can be made, less edgecases etc
package main
import (
"bytes"
"github.com/graphite-ng/carbon-relay-ng/_third_party/github.com/Dieterbe/go-metrics"
"github.com/graphite-ng/carbon-relay-ng/_third_party/github.com/op/go-logging"
"io"
"time"
)
// Writer implements buffering for an io.Writer object.
// If an error occurs writing to a Writer, no more data will be
// accepted and all subsequent writes will return the error.
// After all data has been written, the client should call the
// Flush method to guarantee all data has been forwarded to
// the underlying io.Writer.
type Writer struct {
key string
err error
buf []byte
n int
wr io.Writer
durationOverflowFlush metrics.Timer
}
// NewWriterSize returns a new Writer whose buffer has at least the specified
// size. If the argument io.Writer is already a Writer with large enough
// size, it returns the underlying Writer.
func NewWriter(w io.Writer, size int, key string) *Writer {
if size <= 0 {
panic("invalid size requested")
}
return &Writer{
key: key,
buf: make([]byte, size),
wr: w,
durationOverflowFlush: Timer("dest=" + key + ".what=durationFlush.type=overflow"),
}
}
// Flush writes any buffered data to the underlying io.Writer.
func (b *Writer) Flush() error {
err := b.flush()
return err
}
func (b *Writer) flush() error {
if b.err != nil {
return b.err
}
if b.n == 0 {
return nil
}
if log.IsEnabledFor(logging.INFO) {
bufs := bytes.Split(b.buf[0:b.n], []byte{'\n'})
for _, buf := range bufs {
log.Info("bufWriter %s flush-writing to tcp %s\n", b.key, buf)
}
}
n, err := b.wr.Write(b.buf[0:b.n])
if n < b.n && err == nil {
err = io.ErrShortWrite
}
if err != nil {
if n > 0 && n < b.n {
copy(b.buf[0:b.n-n], b.buf[n:b.n])
}
b.n -= n
b.err = err
return err
}
b.n = 0
return nil
}
// Available returns how many bytes are unused in the buffer.
func (b *Writer) Available() int { return len(b.buf) - b.n }
// Buffered returns the number of bytes that have been written into the current buffer.
func (b *Writer) Buffered() int { return b.n }
// Write writes the contents of p into the buffer.
// It returns the number of bytes written.
// If nn < len(p), it also returns an error explaining
// why the write is short.
func (b *Writer) Write(p []byte) (nn int, err error) {
for len(p) > b.Available() && b.err == nil {
var n int
if b.Buffered() == 0 {
// Large write, empty buffer.
// Write directly from p to avoid copy.
// we should measure this duration because it's equivalent to a flush
start := time.Now()
log.Info("bufWriter %s writing to tcp %s\n", b.key, p)
n, b.err = b.wr.Write(p)
b.durationOverflowFlush.UpdateSince(start)
} else {
n = copy(b.buf[b.n:], p)
b.n += n
b.durationOverflowFlush.Time(func() {
b.flush()
})
}
nn += n
p = p[n:]
}
if b.err != nil {
return nn, b.err
}
n := copy(b.buf[b.n:], p)
b.n += n
nn += n
return nn, nil
}