-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathviewserver.go
More file actions
157 lines (138 loc) · 3.89 KB
/
viewserver.go
File metadata and controls
157 lines (138 loc) · 3.89 KB
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
package gobearmon
import "bufio"
import "encoding/json"
import "fmt"
import "log"
import "net"
import "strings"
import "sync"
import "time"
type ViewServer struct {
Addr string
Controllers []string
pingStatus map[string]int
activeController string
mu sync.Mutex
}
func (this *ViewServer) Start() {
this.pingStatus = make(map[string]int)
for _, controller := range this.Controllers {
this.pingStatus[controller] = 0
go this.ping(controller)
}
ln, err := net.Listen("tcp", this.Addr)
if err != nil {
panic(err)
}
go func() {
for {
conn, err := ln.Accept()
if err != nil {
log.Printf("viewserver: error while accepting connection: %s", err.Error())
continue
}
log.Printf("viewserver: new connection from %s", conn.RemoteAddr().String())
go this.handle(conn)
}
}()
}
func (this *ViewServer) getActiveController() string {
this.mu.Lock()
defer this.mu.Unlock()
return this.activeController
}
func (this *ViewServer) updatePing(controller string, result bool) {
this.mu.Lock()
defer this.mu.Unlock()
if result {
this.pingStatus[controller]++
if this.activeController == "" {
log.Printf("viewserver: initializing controller to %s", controller)
this.activeController = controller
}
} else {
if this.pingStatus[controller] != 0 {
log.Printf("viewserver: marking controller %s as down", controller)
this.pingStatus[controller] = 0
}
if controller == this.activeController {
this.activeController = ""
for testController, uptime := range this.pingStatus {
if uptime > 0 && (this.activeController == "" || uptime > this.pingStatus[this.activeController]) {
this.activeController = testController
}
}
if this.activeController == "" {
log.Printf("viewserver: warning: controller %s failed, but no one found to replace", controller)
mailAdmin("gobearmon: controller failed without replacement", fmt.Sprintf("controller %s failed without replacement", controller))
} else {
log.Printf("viewserver: failover from %s to %s", controller, this.activeController)
}
}
}
}
func (this *ViewServer) handle(conn net.Conn) {
defer conn.Close()
in := bufio.NewReader(conn)
password, err := in.ReadString('\n')
if err != nil || len(password) == 0 || password[:len(password) - 1] != cfg.Default.Password {
log.Printf("viewserver: terminating connection from %s due to incorrect password", conn.RemoteAddr().String())
return
}
for {
line, err := in.ReadString('\n')
if err != nil {
log.Printf("viewserver: client at %s disconnected: %s", conn.RemoteAddr().String(), err.Error())
break
}
line = strings.TrimSpace(line)
if line != "request" {
log.Printf("viewserver: received invalid request [%s] from %s", line, conn.RemoteAddr().String())
break
}
conn.Write([]byte(this.getActiveController() + "\n"))
}
}
func (this *ViewServer) ping(controller string) {
var conn net.Conn
var in *bufio.Reader
for {
if conn == nil {
var err error
conn, err = net.DialTimeout("tcp", controller, 10 * time.Second)
if err != nil {
this.updatePing(controller, false)
time.Sleep(30 * time.Second)
continue
}
conn.Write([]byte(cfg.Default.Password + "\n"))
in = bufio.NewReader(conn)
}
// send empty request and expect empty but valid response
requestBytes, err := json.Marshal(MakeControllerRequest())
if err != nil {
panic(err)
}
conn.Write([]byte(string(requestBytes) + "\n"))
conn.SetReadDeadline(time.Now().Add(5 * time.Second))
line, err := in.ReadString('\n')
if err != nil {
this.updatePing(controller, false)
conn.Close()
conn = nil
time.Sleep(30 * time.Second)
continue
}
var response ControllerResponse
err = json.Unmarshal([]byte(strings.TrimSpace(line)), &response)
if err != nil {
this.updatePing(controller, false)
conn.Close()
conn = nil
time.Sleep(30 * time.Second)
continue
}
this.updatePing(controller, true)
time.Sleep(5 * time.Second)
}
}