-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathupstream.go
192 lines (154 loc) · 3.78 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
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
package main
import (
"errors"
"fmt"
"os"
"time"
"sync"
"encoding/binary"
"net"
"github.com/technofy/udp-loadbalancer-go/config"
"github.com/golang/glog"
)
const (
HT_REMOTE_IP = iota + 1
HT_REMOTE_PORT
HT_NONE
)
const (
TT_STATIC = iota + 1
TT_AWS_ASG
)
type IDynamicUpstreamSource interface {
UpdatePeers() ([]string, error)
}
type Upstream struct {
Config *config.Upstream
Targets []string
TargetType uint8
RRcounter uint
IsDynamic bool
HashType uint8
HashCache *CacheManager
DynamicSource IDynamicUpstreamSource
DynamicSourceLock sync.Mutex
}
// GetRRPeer will get a peer from the peers list with a round-robin behavior
func (m *Upstream) GetRRPeer() (*string, error) {
numTargets := len(m.Targets)
if numTargets == 0 {
return nil, errors.New("No target present in upstream")
}
peer := &m.Targets[m.RRcounter % uint(numTargets)]
// in case the number of targets got reduced
m.RRcounter++
return peer, nil
}
// GetPeer will fetch a peer either from the cache or the peers list upon request
func (m *Upstream) GetPeer(hash uint32) (*string, error) {
if m.HashType == HT_NONE {
return m.GetRRPeer()
}
m.DynamicSourceLock.Lock()
ipeer := m.HashCache.Get(hash)
if ipeer == nil {
peer, err := m.GetRRPeer()
if err != nil {
return nil, err
}
m.HashCache.Add(hash, peer)
ipeer = peer
}
m.DynamicSourceLock.Unlock()
return ipeer.(*string), nil
}
// UpdateDynamicPeers will update the peers of a dynamic upstream
func (m *Upstream) UpdateDynamicPeers() {
if m.IsDynamic {
targets, err := m.DynamicSource.UpdatePeers()
if err != nil {
glog.Errorf("Can't update upstream (%s): %s", m.Config.Name, err)
return
}
m.DynamicSourceLock.Lock()
m.Targets = targets
// Check if an old peer entry is stored in the cache, if yes, delete it
storedKeys := m.HashCache.GetKeys()
for _, sk := range storedKeys {
found := false
for _, t := range targets {
if sk == binary.BigEndian.Uint32(net.ParseIP(t)) {
found = true
}
}
if !found {
m.HashCache.DeleteEntry(sk)
}
}
m.DynamicSourceLock.Unlock()
} else {
fmt.Println()
}
}
// AutoUpdatePeer is a helper function that will update the list of dynamic upstream peers at a regular rate defined by
// the parameter seconds.
func AutoUpdatePeer(us *Upstream, seconds int) {
ticker := time.NewTicker(time.Second * time.Duration(seconds))
us.UpdateDynamicPeers()
for range ticker.C {
us.UpdateDynamicPeers()
}
}
// NewUpstream parses an upstream configuration block and creates an Upstream object
func NewUpstream(cfg *config.Upstream) (*Upstream, error) {
var hashType uint8
switch cfg.Hash {
case "remote_ip":
hashType = HT_REMOTE_IP
break
case "remote_port":
hashType = HT_REMOTE_PORT
break
default:
glog.Warning("Incorrect upstream hash. Defaulting to none.")
case "":
case "none":
hashType = HT_NONE
break
}
us := &Upstream{
Config: cfg,
RRcounter: 0,
HashType: hashType,
IsDynamic: false,
}
if us.HashType != HT_NONE {
us.HashCache = MustNewCacheManager(60, 5)
}
switch cfg.Type {
case "aws_autoscaling_group":
us.TargetType = TT_AWS_ASG
us.IsDynamic = true
region := os.Getenv("AWS_REGION")
if region == "" || len(cfg.Targets) == 0 {
return nil, errors.New("AWS_REGION is not set or no targets are configured for autoscaling target")
}
us.DynamicSource = *MustNewAutoScalingGroupUpstreamSource(region, cfg.Targets[0])
break
default:
glog.Warning("Incorrect upstream type. Defaulting to none.")
case "":
case "static":
us.TargetType = TT_STATIC
us.Targets = cfg.Targets
}
return us, nil
}
// MustNewUpstream does the same thing as NewUpstream and will panic if the creation fails
func MustNewUpstream(cfg *config.Upstream) *Upstream {
us, err := NewUpstream(cfg)
if err != nil {
panic(err)
}
return us
}