-
Notifications
You must be signed in to change notification settings - Fork 1
/
udp.go
86 lines (75 loc) · 1.53 KB
/
udp.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
package main
import (
"bufio"
"net"
"sync"
"time"
)
func SendToAll(msg string) {
var wg sync.WaitGroup
start := time.Now()
for _, c := range AllCells {
wg.Add(1)
go func(c *Cell) {
SendUDP(msg, c)
wg.Done()
}(c)
}
wg.Wait()
end := time.Now()
logger.Info("SendToAll",
"msg", msg,
"duration", end.Sub(start))
}
func SendUDP(daters string, cell *Cell) (err error) {
for i := 0; i < 5; i++ {
err = SendUDPOnce(daters, cell)
if err != nil {
cell.addr = "" // trigger cache refresh, probably should be handled elsewhere, but eh.
} else {
break
}
}
return
}
func SendUDPOnce(daters string, cell *Cell) (err error) {
addr, err := cell.Address()
if err != nil {
return
}
start := time.Now()
conn, err := net.Dial("udp", addr)
if err != nil {
logger.Error("getting UDP port",
"name", cell.Name(),
"err", err)
return
}
defer conn.Close()
// remember: this fixed my "jobs dont die becuause frozen" bug
now := time.Now()
err = conn.SetDeadline(now.Add(150 * time.Millisecond)) // TODO: longer when run on a cluster?
if err != nil {
logger.Error("setting deadline", "err", err)
}
_, err = conn.Write([]byte(daters))
if err != nil {
logger.Error("sending",
"daters", daters,
"name", cell.Name(),
"err", err)
}
_, _, err = bufio.NewReader(conn).ReadLine()
if err != nil {
logger.Error("reading response",
"name", cell.Name(),
"err", err)
return
}
end := time.Now()
logger.Debug("SendUDP",
"daters", daters,
"target", cell.Name(),
"duration", end.Sub(start))
return
}