-
Notifications
You must be signed in to change notification settings - Fork 0
/
backends.go
60 lines (52 loc) · 1.05 KB
/
backends.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
package main
import (
"fmt"
"log"
"net"
"time"
)
type Backend interface {
IsDown() bool
Verify()
}
type BackendGroup interface {
SelectBackend() Backend
VerifyBackends()
AddBackend(Backend) error
RemoveBackend() error
}
type BaseBackend struct {
Ip net.IP
Port int16
Down bool
LastValidCheck time.Time
}
type BaseBackendGroup struct {
Group []BaseBackend
Method string
}
func NewBaseBackend(ip_string string, port int16) (*BaseBackend, error) {
ip := net.ParseIP(ip_string)
if port < 0 || ip == nil {
return nil, fmt.Errorf("IP and Port can't have negative values!")
}
return &BaseBackend{Ip: ip, Port: port}, nil
}
func (bb *BaseBackend) IsDown() bool {
return bb.Down
}
func (bb *BaseBackend) Verify() {
_, err := net.DialTimeout("tcp", fmt.Sprintf("%s:%d", bb.Ip, bb.Port), time.Second*10)
// Timeout... Set it as down!
if err != nil {
log.Println(err)
if bb.Down == false {
bb.LastValidCheck = time.Now()
}
bb.Down = true
return
}
// No error
bb.LastValidCheck = time.Now()
return
}