forked from One-com/ozone
/
vtransport.go
230 lines (198 loc) · 5.54 KB
/
vtransport.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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
package rproxy
import (
"context"
"encoding/binary"
"fmt"
"net/http"
"net/url"
"time"
"sync"
"github.com/One-com/gone/http/vtransport"
"github.com/One-com/gone/http/vtransport/upstream/rr"
"github.com/One-com/gone/log"
"github.com/One-com/gone/jconf"
"github.com/One-com/ozone/rproxymod"
)
// VirtualTransport defines JSON for configuring a virtual backend transport.
// Multiple named virtual upstreams can be defined as an array of "scheme://host:port/" URIs
// Requests are send to the hosts in a virtual upstream in a RR manner, but if the request
// contains a RoutingKey Header a specific backend host will be used for all req. with the same
// Header for "BackendPin" duration. This can be used to ensure backend caches are used optimally.
// If a host has MaxFails errors it will be put in Quarantine and request will be routes to a different
// host.
type VirtualTransportConfig struct {
Type string
Retries int
Upstreams map[string][]string
Config *jconf.OptionalSubConfig
// Legacy
MaxFails int
Quarantine jconf.Duration
BackendPin jconf.Duration
RoutingKeyHeader string
}
type HealthCheckConfig struct {
Interval jconf.Duration
URIPath string
Timeout jconf.Duration
Expect int
}
type RRUpstreamConfig struct {
MaxFails int
Quarantine jconf.Duration
BackendPin jconf.Duration
BurstFailGrace jconf.Duration
RoutingKeyHeader string
HealthCheck *HealthCheckConfig
}
type rrPinCache struct {
cc rproxymod.Cache
}
func (c *rrPinCache) Set(key string, value int, ttl time.Duration) {
var buf [8]byte
binary.PutUvarint(buf[:], uint64(value))
err := c.cc.Set([]byte(key), buf[:], ttl)
if err != nil {
}
}
func (c *rrPinCache) Get(key string) (value int) {
bval, err := c.cc.Get([]byte(key))
if err != nil {
return -1
}
ival, _ := binary.Uvarint(bval)
value = int(ival)
return
}
func (c *rrPinCache) Delete(key string) {
c.cc.Delete([]byte(key))
}
func initVirtualTransport(cc rproxymod.Cache, wrapped *http.Transport, js jconf.SubConfig) (vt *vtransport.VirtualTransport, servicefunc func(context.Context) error, err error) {
cfg := new(VirtualTransportConfig)
err = js.ParseInto(&cfg)
if err != nil {
return
}
if cfg.Type != "RoundRobin" {
err = fmt.Errorf("Unknown upstream type: %s", cfg.Type)
return
}
// Check RR specific config
var rrcfg *RRUpstreamConfig
var healthuri *url.URL
if cfg.Config != nil {
err = cfg.Config.ParseInto(&rrcfg)
if err != nil {
return
}
}
// If not defined, fall back to create it from legacy
if rrcfg == nil {
rrcfg = new(RRUpstreamConfig)
rrcfg.BackendPin = cfg.BackendPin
rrcfg.MaxFails = cfg.MaxFails
rrcfg.Quarantine = cfg.Quarantine
rrcfg.RoutingKeyHeader = cfg.RoutingKeyHeader
} else {
if rrcfg.HealthCheck != nil {
// parse health check URI once and for all
healthuri, err = url.ParseRequestURI(rrcfg.HealthCheck.URIPath)
if err != nil {
return
}
}
}
rrcache := &rrPinCache{cc: cc}
logger := log.Default()
var services []func(context.Context) error
upstreams := make(map[string]vtransport.VirtualUpstream)
for k, v := range cfg.Upstreams {
var urls = make([]*url.URL, 0)
for _, urlStr := range v {
url, e := url.Parse(urlStr)
if e != nil {
err = e
return
}
urls = append(urls, url)
}
upname := k
logfunc := func(e rr.Event) {
target := ""
if e.Target != nil {
target = e.Target.String()
}
if e.Err == nil {
logger.Printf("Virtual Transport: %s, %s %s", upname, e.Name, target)
} else {
logger.Printf("Virtual Transport: %s, %s %s: %s", upname, e.Name, target, e.Err.Error())
}
}
RROptions := []rr.RROption{
rr.Targets(urls...),
rr.MaxFails(rrcfg.MaxFails),
rr.Quarantine(rrcfg.Quarantine.Duration),
rr.BurstFailGrace(rrcfg.BurstFailGrace.Duration),
rr.EventCallback(logfunc),
}
if rrcfg.BackendPin.Duration != 0 {
pinoption := rr.PinRequestsWith(rrcache, rrcfg.BackendPin.Duration,
rr.PinKeyFunc(func(req *http.Request) string {
key := req.Header.Get(rrcfg.RoutingKeyHeader)
return key
}))
RROptions = append(RROptions, pinoption)
}
if rrcfg.HealthCheck != nil &&
rrcfg.HealthCheck.Timeout.Duration != 0 {
client := &http.Client{Timeout: rrcfg.HealthCheck.Timeout.Duration }
checkfunc := func(u *url.URL) error {
u2 := *healthuri
u2.Scheme = u.Scheme
u2.Host = u.Host
res, err := client.Get(u2.String())
if err != nil {
return err
}
res.Body.Close()
if res.StatusCode != rrcfg.HealthCheck.Expect {
return fmt.Errorf("Expected %d, got %d", rrcfg.HealthCheck.Expect, res.StatusCode )
}
return nil
}
healthcheckoption, service := rr.HealthCheck(
rrcfg.HealthCheck.Interval.Duration,
checkfunc)
if healthcheckoption != nil {
services = append(services, service)
RROptions = append(RROptions, healthcheckoption)
}
}
upstreams[k], err = rr.NewRoundRobinUpstream(RROptions...)
if err != nil {
return
}
}
// Make an overall service function for all upstream monitoring health checks
servicefunc = func(ctx context.Context) error {
var wg sync.WaitGroup
for s := range services {
i := s
wg.Add(1)
// All these go-routines will exit when ctx is canceled.
go func() {
services[i](ctx)
wg.Done()
}()
}
// ... We just wait for them. We're allowed to exit without context being canceled.
wg.Wait()
return nil
}
vt = &vtransport.VirtualTransport{
Transport: wrapped,
Upstreams: upstreams,
RetryPolicy: vtransport.Retries(cfg.Retries, 0, false),
}
return
}