-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathstream_encoder.go
162 lines (125 loc) · 2.76 KB
/
stream_encoder.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
package cypress
import (
"io"
"os"
"sync"
"time"
"github.com/andrew-d/go-termutil"
"gopkg.in/tomb.v2"
)
type MessageEncoder interface {
Encode(m *Message) (uint64, error)
}
type Flusher interface {
Flush() error
}
// A type that encodes Messages to a stream with optional compression
type StreamEncoder struct {
w io.WriteCloser
ew io.WriteCloser
flush Flusher
enc MessageEncoder
encoded uint64
lock sync.Mutex
t tomb.Tomb
}
// Create a new StreamEncoder sending data to w. The format is either the
// native format or kv format if w is stdout and a tty.
func NewStreamEncoder(w io.WriteCloser) *StreamEncoder {
if w == os.Stdout {
if termutil.Isatty(os.Stdout.Fd()) {
return &StreamEncoder{w: w, enc: NewKVEncoder(w)}
}
}
return &StreamEncoder{w: w, enc: NewEncoder(w)}
}
var StreamNotifyByte = []byte{'-'}
// Initialize the StreamEncoder to a particular compression level and
// write the header
func (s *StreamEncoder) Init(comp StreamHeader_Compression) error {
hdr := &StreamHeader{Compression: comp.Enum()}
return s.WriteCustomHeader(hdr)
}
func (s *StreamEncoder) flushTimer() error {
tick := time.NewTicker(5 * time.Second)
for {
select {
case <-tick.C:
s.lock.Lock()
s.flush.Flush()
s.lock.Unlock()
case <-s.t.Dying():
return nil
}
}
}
// Write a StreamHeader
func (s *StreamEncoder) WriteCustomHeader(hdr *StreamHeader) error {
_, err := s.w.Write(StreamNotifyByte)
if err != nil {
return err
}
data, err := hdr.Marshal()
if err != nil {
return err
}
_, err = WriteUvarint(s.w, uint64(len(data)))
if err != nil {
return err
}
_, err = s.w.Write(data)
if err != nil {
return err
}
s.ew = WriteCompressed(s.w, hdr.GetCompression())
if f, ok := s.ew.(Flusher); ok {
s.flush = f
s.t.Go(s.flushTimer)
}
s.enc = NewEncoder(s.ew)
return nil
}
// Probe the file and setup the encoder to match the probe's
// settings.
func (s *StreamEncoder) OpenFile(f *os.File) error {
probe := NewProbe(f)
err := probe.Probe()
if err != nil {
return err
}
s.ew = WriteCompressed(s.w, probe.Compression())
if f, ok := s.ew.(Flusher); ok {
s.flush = f
s.t.Go(s.flushTimer)
}
s.enc = NewEncoder(s.ew)
_, err = f.Seek(0, os.SEEK_END)
return err
}
// Take a Message and encode it
func (s *StreamEncoder) Receive(m *Message) error {
s.lock.Lock()
cnt, err := s.enc.Encode(m)
s.encoded += cnt
s.lock.Unlock()
return err
}
func (s *StreamEncoder) Close() error {
if s.flush != nil {
s.t.Kill(nil)
s.t.Wait()
}
return s.ew.Close()
}
func (s *StreamEncoder) Flush() error {
if s.flush == nil {
return nil
}
s.lock.Lock()
defer s.lock.Unlock()
return s.flush.Flush()
}
// Indicate how many bytes have been sent
func (s *StreamEncoder) EncodedBytes() uint64 {
return s.encoded
}