-
Notifications
You must be signed in to change notification settings - Fork 0
/
upstream.go
56 lines (44 loc) · 1.18 KB
/
upstream.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
package main
// Manages information about upstream servers
import (
"fmt"
"time"
)
type UpstreamName string
type UpstreamWeight float64
type Upstream struct {
// The hostname of the upstream
Name UpstreamName
// The port to connect to
Port int
// The current weight score of this upstream
weight UpstreamWeight
// if set and in the future, wait for this time before making connections
wakeupTime time.Time
}
func (u *Upstream) GetAddress() string {
port := u.Port
if port == 0 {
port = 853
}
return fmt.Sprintf("%s:%d", u.Name, port)
}
func (u *Upstream) GetWeight() UpstreamWeight {
return u.weight
}
func (u *Upstream) SetWeight(w UpstreamWeight) {
u.weight = w
}
// Sets a wakeup time until which no connections should be made
func (u *Upstream) Cooldown(t time.Duration) {
u.wakeupTime = time.Now().Add(t)
}
// returns whether or not the wakeupTime is after the current time
// if so, this upstream shouldn't get more connections
func (u *Upstream) IsCooling() (ret bool) {
return u.wakeupTime.After(time.Now())
}
// returns the actual time when this upstream will be ready for connections
func (u *Upstream) WakeupTime() (wakeupTime time.Time) {
return u.wakeupTime
}