-
Notifications
You must be signed in to change notification settings - Fork 3
/
edge_writer.go
59 lines (46 loc) · 1.08 KB
/
edge_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
package main
import (
"fmt"
"net"
"sync"
)
const bufSize = 1000
type edgeWriter struct {
buffers map[string]chan []byte
mutex sync.RWMutex
quit bool
}
func NewEdgeWriter() *edgeWriter {
return &edgeWriter{
buffers: make(map[string]chan []byte),
mutex: sync.RWMutex{},
quit: false,
}
}
func (edge *edgeWriter) new(host string, conn *net.UDPConn) {
edge.buffers[host] = make(chan []byte, bufSize)
go func() {
for {
// Since this is UDP, _it cannot fail_
conn.Write(<-edge.buffers[host])
}
}()
}
func (edge *edgeWriter) write(buf []byte) {
for host := range edge.buffers {
// if edge.quit {
// return
// }
// Bug is the writes continue even after the edge is closed, likely filling up the buffer
// fmt.Println("Writing to buffer for ", host)
if len(edge.buffers[host]) >= bufSize {
fmt.Printf("BUFFER FULL FOR %s, PURGING...\n", host)
// If the queue is full, read the first message to start clearing it
<-edge.buffers[host]
}
edge.buffers[host] <- buf
}
}
func (edge *edgeWriter) remove(host string) {
delete(edge.buffers, host)
}