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 181
/
broadcast.go
193 lines (165 loc) · 3.5 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
187
188
189
190
191
192
193
package main
import (
"errors"
"io"
"sync"
"time"
"github.com/glycerine/rbuf"
"github.com/qiniu/log"
)
// The new broadcast
type StreamWriter struct {
wc io.WriteCloser
stream string
}
type WriteBroadcaster struct {
sync.Mutex
buf *rbuf.FixedSizeRingBuf
writers map[StreamWriter]bool
closed bool
}
func NewWriteBroadcaster(size int) *WriteBroadcaster {
if size <= 0 {
size = 4 * 1024
}
bc := &WriteBroadcaster{
writers: make(map[StreamWriter]bool),
buf: rbuf.NewFixedSizeRingBuf(size),
closed: false,
}
return bc
}
func (wb *WriteBroadcaster) Closed() bool {
return wb.closed
}
// this is main func
func (wb *WriteBroadcaster) NewChanString(name string) chan string {
wb.Lock()
defer wb.Unlock()
wr := NewChanStrWriter()
if wb.closed {
wr.Close()
return nil
}
sw := StreamWriter{wc: wr, stream: name}
wb.writers[sw] = true
wr.Write(wb.buf.Bytes())
return wr.C
}
func (wb *WriteBroadcaster) Bytes() []byte {
return wb.buf.Bytes()
}
func (w *WriteBroadcaster) Write(p []byte) (n int, err error) {
w.Lock()
defer w.Unlock()
// write with advance
w.buf.WriteAndMaybeOverwriteOldestData(p)
for sw := range w.writers {
// set write timeout
err = GoTimeout(func() error {
if _, 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
log.Warnf("broadcase write error: %s, %s", sw.stream, err)
sw.wc.Close()
delete(w.writers, sw)
}
}
return len(p), nil
}
func (w *WriteBroadcaster) CloseWriter(name string) {
for sw := range w.writers {
if sw.stream == name {
sw.wc.Close()
}
}
}
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}
// }
// chan string writer
type chanStrWriter struct {
C chan string
closed bool
mu sync.Mutex
}
func (c *chanStrWriter) Write(data []byte) (n int, err error) {
c.mu.Lock()
defer c.mu.Unlock()
if c.closed {
return 0, errors.New("chan writer closed")
}
c.C <- string(data) // write timeout
return len(data), nil
}
func (c *chanStrWriter) Close() error {
c.mu.Lock()
defer c.mu.Unlock()
if !c.closed {
c.closed = true
close(c.C)
}
return nil
}
func NewChanStrWriter() *chanStrWriter {
return &chanStrWriter{
C: make(chan string, 10),
}
}
// quick loss writer
type QuickLossBroadcastWriter struct {
*WriteBroadcaster
bufC chan string
closed bool
}
func (w *QuickLossBroadcastWriter) Write(buf []byte) (int, error) {
select {
case w.bufC <- string(buf):
default:
}
return len(buf), nil
}
func (w *QuickLossBroadcastWriter) Close() error {
if !w.closed {
w.closed = true
close(w.bufC)
w.WriteBroadcaster.CloseWriters()
}
return nil
}
func (w *QuickLossBroadcastWriter) drain() {
for data := range w.bufC {
w.WriteBroadcaster.Write([]byte(data))
}
}
func NewQuickLossBroadcastWriter(size int) *QuickLossBroadcastWriter {
qlw := &QuickLossBroadcastWriter{
WriteBroadcaster: NewWriteBroadcaster(size),
bufC: make(chan string, 20),
}
go qlw.drain()
return qlw
}