This repository has been archived by the owner on Dec 19, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 30
/
broadcast.go
186 lines (163 loc) · 3.22 KB
/
broadcast.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
// broadcast package
// some code from dotcloud/docker
package main
import (
"bytes"
"errors"
"io"
"sync"
"time"
)
var ErrTimeout = errors.New("timeout")
func GoTimeout(f func() error, timeout time.Duration) (err error) {
done := make(chan bool)
go func() {
err = f()
done <- true
}()
select {
case <-time.After(timeout):
return ErrTimeout
case <-done:
return
}
}
type StreamWriter struct {
wc io.WriteCloser
stream string
}
type WriteBroadcaster struct {
sync.Mutex
buf *bytes.Buffer
writers map[StreamWriter]bool
closed bool
}
func NewWriteBroadcaster() *WriteBroadcaster {
bc := &WriteBroadcaster{
writers: make(map[StreamWriter]bool),
buf: bytes.NewBuffer(nil),
closed: false,
}
return bc
}
func (w *WriteBroadcaster) AddWriter(writer io.WriteCloser, stream string) {
w.Lock()
defer w.Unlock()
if w.closed {
writer.Close()
return
}
sw := StreamWriter{wc: writer, stream: stream}
w.writers[sw] = true
}
func (wb *WriteBroadcaster) Closed() bool {
return wb.closed
}
func (wb *WriteBroadcaster) NewReader(name string) ([]byte, *io.PipeReader) {
r, w := io.Pipe()
wb.AddWriter(w, name)
return wb.buf.Bytes(), r
}
func (wb *WriteBroadcaster) NewBufReader(name string) ([]byte, io.ReadCloser) {
data, rd := wb.NewReader(name)
return data, NewBufReader(rd)
}
func (wb *WriteBroadcaster) Bytes() []byte {
return wb.buf.Bytes()
}
func (w *WriteBroadcaster) Write(p []byte) (n int, err error) {
w.Lock()
defer w.Unlock()
w.buf.Write(p)
for sw := range w.writers {
// set write timeout
err = GoTimeout(func() error {
if n, err := sw.wc.Write(p); err != nil || n != len(p) {
return errors.New("broadcast to " + sw.stream + " error")
}
return nil
}, time.Second*1)
if err != nil {
// On error, evict the writer
// Debugf("broadcase write error: %s, %s", sw.stream, err)
delete(w.writers, sw)
}
}
return len(p), nil
}
func (w *WriteBroadcaster) CloseWriters() error {
w.Lock()
defer w.Unlock()
for sw := range w.writers {
sw.wc.Close()
}
w.writers = make(map[StreamWriter]bool)
w.closed = true
return nil
}
// nop writer
type NopWriter struct{}
func (*NopWriter) Write(buf []byte) (int, error) {
return len(buf), nil
}
type nopWriteCloser struct {
io.Writer
}
func (w *nopWriteCloser) Close() error { return nil }
func NopWriteCloser(w io.Writer) io.WriteCloser {
return &nopWriteCloser{w}
}
type bufReader struct {
sync.Mutex
buf *bytes.Buffer
reader io.Reader
err error
wait sync.Cond
}
func NewBufReader(r io.Reader) *bufReader {
reader := &bufReader{
buf: &bytes.Buffer{},
reader: r,
}
reader.wait.L = &reader.Mutex
go reader.drain()
return reader
}
func (r *bufReader) drain() {
buf := make([]byte, 1024)
for {
n, err := r.reader.Read(buf)
r.Lock()
if err != nil {
r.err = err
} else {
r.buf.Write(buf[0:n])
}
r.wait.Signal()
r.Unlock()
if err != nil {
break
}
}
}
func (r *bufReader) Read(p []byte) (n int, err error) {
r.Lock()
defer r.Unlock()
for {
n, err = r.buf.Read(p)
if n > 0 {
return n, err
}
if r.err != nil {
return 0, r.err
}
r.wait.Wait()
}
}
func (r *bufReader) Close() error {
closer, ok := r.reader.(io.ReadCloser)
if !ok {
return nil
}
return closer.Close()
}