forked from ContentSquare/chproxy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
scope.go
892 lines (760 loc) · 21.5 KB
/
scope.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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
package main
import (
"context"
"fmt"
"hash/fnv"
"io/ioutil"
"net"
"net/http"
"net/url"
"regexp"
"strings"
"sync/atomic"
"time"
"github.com/Vertamedia/chproxy/cache"
"github.com/Vertamedia/chproxy/config"
"github.com/Vertamedia/chproxy/log"
"github.com/prometheus/client_golang/prometheus"
)
type scopeID uint64
func (sid scopeID) String() string {
return fmt.Sprintf("%08X", uint64(sid))
}
func newScopeID() scopeID {
sid := atomic.AddUint64(&nextScopeID, 1)
return scopeID(sid)
}
var nextScopeID = uint64(time.Now().UnixNano())
type scope struct {
startTime time.Time
id scopeID
host *host
cluster *cluster
user *user
clusterUser *clusterUser
remoteAddr string
localAddr string
// is true when KillQuery has been called
canceled bool
labels prometheus.Labels
}
func newScope(req *http.Request, u *user, c *cluster, cu *clusterUser) *scope {
h := c.getHost()
var localAddr string
if addr, ok := req.Context().Value(http.LocalAddrContextKey).(net.Addr); ok {
localAddr = addr.String()
}
s := &scope{
startTime: time.Now(),
id: newScopeID(),
host: h,
cluster: c,
user: u,
clusterUser: cu,
remoteAddr: req.RemoteAddr,
localAddr: localAddr,
labels: prometheus.Labels{
"user": u.name,
"cluster": c.name,
"cluster_user": cu.name,
"replica": h.replica.name,
"cluster_node": h.addr.Host,
},
}
return s
}
func (s *scope) String() string {
return fmt.Sprintf("[ Id: %s; User %q(%d) proxying as %q(%d) to %q(%d); RemoteAddr: %q; LocalAddr: %q; Duration: %d μs]",
s.id,
s.user.name, s.user.queryCounter.load(),
s.clusterUser.name, s.clusterUser.queryCounter.load(),
s.host.addr.Host, s.host.load(),
s.remoteAddr, s.localAddr, time.Since(s.startTime).Nanoseconds()/1000.0)
}
func (s *scope) incQueued() error {
if s.user.queueCh == nil && s.clusterUser.queueCh == nil {
// Request queues in the current scope are disabled.
return s.inc()
}
// Do not store `replica` and `cluster_node` in labels, since they have
// no sense for queue metrics.
labels := prometheus.Labels{
"user": s.labels["user"],
"cluster": s.labels["cluster"],
"cluster_user": s.labels["cluster_user"],
}
if s.user.queueCh != nil {
select {
case s.user.queueCh <- struct{}{}:
defer func() {
<-s.user.queueCh
}()
default:
// Per-user request queue is full.
// Give the request the last chance to run.
err := s.inc()
if err != nil {
userQueueOverflow.With(labels).Inc()
}
return err
}
}
if s.clusterUser.queueCh != nil {
select {
case s.clusterUser.queueCh <- struct{}{}:
defer func() {
<-s.clusterUser.queueCh
}()
default:
// Per-clusterUser request queue is full.
// Give the request the last chance to run.
err := s.inc()
if err != nil {
clusterUserQueueOverflow.With(labels).Inc()
}
return err
}
}
// The request has been successfully queued.
queueSize := requestQueueSize.With(labels)
queueSize.Inc()
defer queueSize.Dec()
// Try starting the request during the given duration.
d := s.maxQueueTime()
dSleep := d / 10
if dSleep > time.Second {
dSleep = time.Second
}
if dSleep < time.Millisecond {
dSleep = time.Millisecond
}
deadline := time.Now().Add(d)
for {
err := s.inc()
if err == nil {
// The request is allowed to start.
return nil
}
dLeft := time.Until(deadline)
if dLeft <= 0 {
// Give up: the request exceeded its wait time
// in the queue :(
return err
}
// The request has dLeft remaining time to wait in the queue.
// Sleep for a bit and try starting it again.
if dSleep > dLeft {
time.Sleep(dLeft)
} else {
time.Sleep(dSleep)
}
// Choose new host, since the previous one may become obsolete
// after sleeping.
h := s.cluster.getHost()
s.host = h
s.labels["replica"] = h.replica.name
s.labels["cluster_node"] = h.addr.Host
}
}
func (s *scope) inc() error {
uQueries := s.user.queryCounter.inc()
cQueries := s.clusterUser.queryCounter.inc()
var err error
if s.user.maxConcurrentQueries > 0 && uQueries > s.user.maxConcurrentQueries {
err = fmt.Errorf("limits for user %q are exceeded: max_concurrent_queries limit: %d",
s.user.name, s.user.maxConcurrentQueries)
}
if s.clusterUser.maxConcurrentQueries > 0 && cQueries > s.clusterUser.maxConcurrentQueries {
err = fmt.Errorf("limits for cluster user %q are exceeded: max_concurrent_queries limit: %d",
s.clusterUser.name, s.clusterUser.maxConcurrentQueries)
}
uRPM := s.user.rateLimiter.inc()
cRPM := s.clusterUser.rateLimiter.inc()
// int32(xRPM) > 0 check is required to detect races when RPM
// is decremented on error below after per-minute zeroing
// in rateLimiter.run.
// These races become innocent with the given check.
if s.user.reqPerMin > 0 && int32(uRPM) > 0 && uRPM > s.user.reqPerMin {
err = fmt.Errorf("rate limit for user %q is exceeded: requests_per_minute limit: %d",
s.user.name, s.user.reqPerMin)
}
if s.clusterUser.reqPerMin > 0 && int32(cRPM) > 0 && cRPM > s.clusterUser.reqPerMin {
err = fmt.Errorf("rate limit for cluster user %q is exceeded: requests_per_minute limit: %d",
s.clusterUser.name, s.clusterUser.reqPerMin)
}
if err != nil {
s.user.queryCounter.dec()
s.clusterUser.queryCounter.dec()
// Decrement rate limiter here, so it doesn't count requests
// that didn't start due to limits overflow.
s.user.rateLimiter.dec()
s.clusterUser.rateLimiter.dec()
return err
}
s.host.inc()
concurrentQueries.With(s.labels).Inc()
return nil
}
func (s *scope) dec() {
// There is no need in ratelimiter.dec here, since the rate limiter
// is automatically zeroed every minute in rateLimiter.run.
s.user.queryCounter.dec()
s.clusterUser.queryCounter.dec()
s.host.dec()
concurrentQueries.With(s.labels).Dec()
}
const killQueryTimeout = time.Second * 30
func (s *scope) killQuery() error {
log.Debugf("killing the query with query_id=%s", s.id)
killedRequests.With(s.labels).Inc()
s.canceled = true
query := fmt.Sprintf("KILL QUERY WHERE query_id = '%s'", s.id)
r := strings.NewReader(query)
addr := s.host.addr.String()
req, err := http.NewRequest("POST", addr, r)
if err != nil {
return fmt.Errorf("error while creating kill query request to %s: %s", addr, err)
}
ctx, cancel := context.WithTimeout(context.Background(), killQueryTimeout)
defer cancel()
req = req.WithContext(ctx)
// send request as kill_query_user
userName := s.cluster.killQueryUserName
if len(userName) == 0 {
userName = "default"
}
req.SetBasicAuth(userName, s.cluster.killQueryUserPassword)
resp, err := http.DefaultClient.Do(req)
if err != nil {
return fmt.Errorf("error while executing clickhouse query %q at %q: %s", query, addr, err)
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
responseBody, _ := ioutil.ReadAll(resp.Body)
return fmt.Errorf("unexpected status code returned from query %q at %q: %d. Response body: %q",
query, addr, resp.StatusCode, responseBody)
}
respBody, err := ioutil.ReadAll(resp.Body)
if err != nil {
return fmt.Errorf("cannot read response body for the query %q: %s", query, err)
}
log.Debugf("killed the query with query_id=%s; respBody: %q", s.id, respBody)
return nil
}
// allowedParams contains query args allowed to be proxied.
// See http://clickhouse-docs.readthedocs.io/en/latest/settings/
//
// All the other params passed via query args are stripped before
// proxying the request. This is for the sake of security.
var allowedParams = []string{
"query",
"database",
"default_format",
// if `compress=1`, CH will compress the data it sends you
"compress",
// if `decompress=1` , CH will decompress the same data that you pass in the POST method
"decompress",
// compress the result if the client over HTTP said that it understands data compressed by gzip or deflate.
"enable_http_compression",
// limit on the number of rows in the result
"max_result_rows",
// whether to count extreme values
"extremes",
// what to do if the volume of the result exceeds one of the limits
"result_overflow_mode",
}
// This regexp must match params needed to describe a way to use external data
// @see https://clickhouse.yandex/docs/en/table_engines/external_data/
var externalDataParams = regexp.MustCompile(`(_types|_structure|_format)$`)
func (s *scope) decorateRequest(req *http.Request) (*http.Request, url.Values) {
// Make new params to purify URL.
params := make(url.Values)
// Set user params
if s.user.params != nil {
for _, param := range s.user.params.params {
params.Set(param.Key, param.Value)
}
}
// Keep allowed params.
origParams := req.URL.Query()
for _, param := range allowedParams {
val := origParams.Get(param)
if len(val) > 0 {
params.Set(param, val)
}
}
// Keep external_data params
if req.Method == "POST" {
ct := req.Header.Get("Content-Type")
if strings.Contains(ct, "multipart/form-data") {
for key := range origParams {
if externalDataParams.MatchString(key) {
params.Set(key, origParams.Get(key))
}
}
// disable cache for external_data queries
origParams.Set("no_cache", "1")
log.Debugf("external data params detected - cache will be disabled")
}
}
// Set query_id as scope_id to have possibility to kill query if needed.
params.Set("query_id", s.id.String())
req.URL.RawQuery = params.Encode()
// Rewrite possible previous Basic Auth and send request
// as cluster user.
req.SetBasicAuth(s.clusterUser.name, s.clusterUser.password)
// Delete possible X-ClickHouse headers,
// it is not allowed to use X-ClickHouse HTTP headers and other authentication methods simultaneously
req.Header.Del("X-ClickHouse-User")
req.Header.Del("X-ClickHouse-Key")
// Send request to the chosen host from cluster.
req.URL.Scheme = s.host.addr.Scheme
req.URL.Host = s.host.addr.Host
// Extend ua with additional info, so it may be queried
// via system.query_log.http_user_agent.
ua := fmt.Sprintf("RemoteAddr: %s; LocalAddr: %s; CHProxy-User: %s; CHProxy-ClusterUser: %s; %s",
s.remoteAddr, s.localAddr, s.user.name, s.clusterUser.name, req.UserAgent())
req.Header.Set("User-Agent", ua)
return req, origParams
}
func (s *scope) getTimeoutWithErrMsg() (time.Duration, error) {
var (
timeout time.Duration
timeoutErrMsg error
)
if s.user.maxExecutionTime > 0 {
timeout = s.user.maxExecutionTime
timeoutErrMsg = fmt.Errorf("timeout for user %q exceeded: %v", s.user.name, timeout)
}
if timeout == 0 || (s.clusterUser.maxExecutionTime > 0 && s.clusterUser.maxExecutionTime < timeout) {
timeout = s.clusterUser.maxExecutionTime
timeoutErrMsg = fmt.Errorf("timeout for cluster user %q exceeded: %v", s.clusterUser.name, timeout)
}
return timeout, timeoutErrMsg
}
func (s *scope) maxQueueTime() time.Duration {
d := s.user.maxQueueTime
if d <= 0 || s.clusterUser.maxQueueTime > 0 && s.clusterUser.maxQueueTime < d {
d = s.clusterUser.maxQueueTime
}
if d <= 0 {
// Default queue time.
d = 10 * time.Second
}
return d
}
type paramsRegistry struct {
// key is a hashed concatenation of the params list
key uint32
params []config.Param
}
func newParamsRegistry(params []config.Param) (*paramsRegistry, error) {
if len(params) == 0 {
return nil, fmt.Errorf("params can't be empty")
}
h := fnv.New32a()
for _, p := range params {
str := fmt.Sprintf("%s=%s&", p.Key, p.Value)
h.Write([]byte(str))
}
return ¶msRegistry{
key: h.Sum32(),
params: params,
}, nil
}
type user struct {
name string
password string
toCluster string
toUser string
maxConcurrentQueries uint32
queryCounter counter
maxExecutionTime time.Duration
reqPerMin uint32
rateLimiter rateLimiter
queueCh chan struct{}
maxQueueTime time.Duration
allowedNetworks config.Networks
denyHTTP bool
denyHTTPS bool
allowCORS bool
cache *cache.Cache
params *paramsRegistry
}
type usersProfile struct {
cfg []config.User
clusters map[string]*cluster
caches map[string]*cache.Cache
params map[string]*paramsRegistry
}
func (up usersProfile) newUsers() (map[string]*user, error) {
users := make(map[string]*user, len(up.cfg))
for _, u := range up.cfg {
if _, ok := users[u.Name]; ok {
return nil, fmt.Errorf("duplicate config for user %q", u.Name)
}
tmpU, err := up.newUser(u)
if err != nil {
return nil, fmt.Errorf("cannot initialize user %q: %s", u.Name, err)
}
users[u.Name] = tmpU
}
return users, nil
}
func (up usersProfile) newUser(u config.User) (*user, error) {
c, ok := up.clusters[u.ToCluster]
if !ok {
return nil, fmt.Errorf("unknown `to_cluster` %q", u.ToCluster)
}
if _, ok := c.users[u.ToUser]; !ok {
return nil, fmt.Errorf("unknown `to_user` %q in cluster %q", u.ToUser, u.ToCluster)
}
var queueCh chan struct{}
if u.MaxQueueSize > 0 {
queueCh = make(chan struct{}, u.MaxQueueSize)
}
var cc *cache.Cache
if len(u.Cache) > 0 {
cc = up.caches[u.Cache]
if cc == nil {
return nil, fmt.Errorf("unknown `cache` %q", u.Cache)
}
}
var params *paramsRegistry
if len(u.Params) > 0 {
params = up.params[u.Params]
if params == nil {
return nil, fmt.Errorf("unknown `params` %q", u.Params)
}
}
return &user{
name: u.Name,
password: u.Password,
toCluster: u.ToCluster,
toUser: u.ToUser,
maxConcurrentQueries: u.MaxConcurrentQueries,
maxExecutionTime: time.Duration(u.MaxExecutionTime),
reqPerMin: u.ReqPerMin,
queueCh: queueCh,
maxQueueTime: time.Duration(u.MaxQueueTime),
allowedNetworks: u.AllowedNetworks,
denyHTTP: u.DenyHTTP,
denyHTTPS: u.DenyHTTPS,
allowCORS: u.AllowCORS,
cache: cc,
params: params,
}, nil
}
type clusterUser struct {
name string
password string
maxConcurrentQueries uint32
queryCounter counter
maxExecutionTime time.Duration
reqPerMin uint32
rateLimiter rateLimiter
queueCh chan struct{}
maxQueueTime time.Duration
allowedNetworks config.Networks
}
func newClusterUser(cu config.ClusterUser) *clusterUser {
var queueCh chan struct{}
if cu.MaxQueueSize > 0 {
queueCh = make(chan struct{}, cu.MaxQueueSize)
}
return &clusterUser{
name: cu.Name,
password: cu.Password,
maxConcurrentQueries: cu.MaxConcurrentQueries,
maxExecutionTime: time.Duration(cu.MaxExecutionTime),
reqPerMin: cu.ReqPerMin,
queueCh: queueCh,
maxQueueTime: time.Duration(cu.MaxQueueTime),
allowedNetworks: cu.AllowedNetworks,
}
}
type host struct {
replica *replica
// Counter of unsuccessful requests to decrease host priority.
penalty uint32
// Either the current host is alive.
active uint32
// Host address.
addr *url.URL
counter
}
type replica struct {
cluster *cluster
name string
hosts []*host
nextHostIdx uint32
}
func newReplicas(replicasCfg []config.Replica, nodes []string, scheme string, c *cluster) ([]*replica, error) {
if len(nodes) > 0 {
// No replicas, just flat nodes. Create default replica
// containing all the nodes.
r := &replica{
cluster: c,
name: "default",
}
hosts, err := newNodes(nodes, scheme, r)
if err != nil {
return nil, err
}
r.hosts = hosts
return []*replica{r}, nil
}
replicas := make([]*replica, len(replicasCfg))
for i, rCfg := range replicasCfg {
r := &replica{
cluster: c,
name: rCfg.Name,
}
hosts, err := newNodes(rCfg.Nodes, scheme, r)
if err != nil {
return nil, fmt.Errorf("cannot initialize replica %q: %s", rCfg.Name, err)
}
r.hosts = hosts
replicas[i] = r
}
return replicas, nil
}
func newNodes(nodes []string, scheme string, r *replica) ([]*host, error) {
hosts := make([]*host, len(nodes))
for i, node := range nodes {
addr, err := url.Parse(fmt.Sprintf("%s://%s", scheme, node))
if err != nil {
return nil, fmt.Errorf("cannot parse `node` %q with `scheme` %q: %s", node, scheme, err)
}
hosts[i] = &host{
replica: r,
addr: addr,
}
}
return hosts, nil
}
func (h *host) runHeartbeat(done <-chan struct{}) {
label := prometheus.Labels{
"cluster": h.replica.cluster.name,
"replica": h.replica.name,
"cluster_node": h.addr.Host,
}
hb := h.replica.cluster.heartBeat
heartbeat := func() {
if err := hb.isHealthy(h.addr.String()); err == nil {
atomic.StoreUint32(&h.active, uint32(1))
hostHealth.With(label).Set(1)
} else {
log.Errorf("error while health-checking %q host: %s", h.addr.Host, err)
atomic.StoreUint32(&h.active, uint32(0))
hostHealth.With(label).Set(0)
}
}
heartbeat()
for {
select {
case <-done:
return
case <-time.After(hb.interval):
heartbeat()
}
}
}
func (h *host) isActive() bool { return atomic.LoadUint32(&h.active) == 1 }
func (r *replica) isActive() bool {
// The replica is active if at least a single host is active.
for _, h := range r.hosts {
if h.isActive() {
return true
}
}
return false
}
const (
// prevents excess goroutine creating while penalizing overloaded host
penaltySize = 5
penaltyMaxSize = 300
penaltyDuration = time.Second * 10
)
// decrease host priority for next requests
func (h *host) penalize() {
p := atomic.LoadUint32(&h.penalty)
if p >= penaltyMaxSize {
return
}
hostPenalties.With(prometheus.Labels{
"cluster": h.replica.cluster.name,
"replica": h.replica.name,
"cluster_node": h.addr.Host,
}).Inc()
atomic.AddUint32(&h.penalty, penaltySize)
time.AfterFunc(penaltyDuration, func() {
atomic.AddUint32(&h.penalty, ^uint32(penaltySize-1))
})
}
// overload runningQueries to take penalty into consideration
func (h *host) load() uint32 {
c := h.counter.load()
p := atomic.LoadUint32(&h.penalty)
return c + p
}
func (r *replica) load() uint32 {
var reqs uint32
for _, h := range r.hosts {
reqs += h.load()
}
return reqs
}
type cluster struct {
name string
replicas []*replica
nextReplicaIdx uint32
users map[string]*clusterUser
killQueryUserName string
killQueryUserPassword string
heartBeat *heartBeat
}
func newCluster(c config.Cluster) (*cluster, error) {
clusterUsers := make(map[string]*clusterUser, len(c.ClusterUsers))
for _, cu := range c.ClusterUsers {
if _, ok := clusterUsers[cu.Name]; ok {
return nil, fmt.Errorf("duplicate config for cluster user %q", cu.Name)
}
clusterUsers[cu.Name] = newClusterUser(cu)
}
heartBeat := newHeartBeat(c.HeartBeat, c.ClusterUsers[0])
newC := &cluster{
name: c.Name,
users: clusterUsers,
killQueryUserName: c.KillQueryUser.Name,
killQueryUserPassword: c.KillQueryUser.Password,
heartBeat: heartBeat,
}
replicas, err := newReplicas(c.Replicas, c.Nodes, c.Scheme, newC)
if err != nil {
return nil, fmt.Errorf("cannot initialize replicas: %s", err)
}
newC.replicas = replicas
return newC, nil
}
func newClusters(cfg []config.Cluster) (map[string]*cluster, error) {
clusters := make(map[string]*cluster, len(cfg))
for _, c := range cfg {
if _, ok := clusters[c.Name]; ok {
return nil, fmt.Errorf("duplicate config for cluster %q", c.Name)
}
tmpC, err := newCluster(c)
if err != nil {
return nil, fmt.Errorf("cannot initialize cluster %q: %s", c.Name, err)
}
clusters[c.Name] = tmpC
}
return clusters, nil
}
// getReplica returns least loaded + round-robin replica from the cluster.
//
// Always returns non-nil.
func (c *cluster) getReplica() *replica {
idx := atomic.AddUint32(&c.nextReplicaIdx, 1)
n := uint32(len(c.replicas))
if n == 1 {
return c.replicas[0]
}
idx %= n
r := c.replicas[idx]
reqs := r.load()
// Set least priority to inactive replica.
if !r.isActive() {
reqs = ^uint32(0)
}
if reqs == 0 {
return r
}
// Scan all the replicas for the least loaded replica.
for i := uint32(1); i < n; i++ {
tmpIdx := (idx + i) % n
tmpR := c.replicas[tmpIdx]
if !tmpR.isActive() {
continue
}
tmpReqs := tmpR.load()
if tmpReqs == 0 {
return tmpR
}
if tmpReqs < reqs {
r = tmpR
reqs = tmpReqs
}
}
// The returned replica may be inactive. This is OK,
// since this means all the replicas are inactive,
// so let's try proxying the request to any replica.
return r
}
// getHost returns least loaded + round-robin host from replica.
//
// Always returns non-nil.
func (r *replica) getHost() *host {
idx := atomic.AddUint32(&r.nextHostIdx, 1)
n := uint32(len(r.hosts))
if n == 1 {
return r.hosts[0]
}
idx %= n
h := r.hosts[idx]
reqs := h.load()
// Set least priority to inactive host.
if !h.isActive() {
reqs = ^uint32(0)
}
if reqs == 0 {
return h
}
// Scan all the hosts for the least loaded host.
for i := uint32(1); i < n; i++ {
tmpIdx := (idx + i) % n
tmpH := r.hosts[tmpIdx]
if !tmpH.isActive() {
continue
}
tmpReqs := tmpH.load()
if tmpReqs == 0 {
return tmpH
}
if tmpReqs < reqs {
h = tmpH
reqs = tmpReqs
}
}
// The returned host may be inactive. This is OK,
// since this means all the hosts are inactive,
// so let's try proxying the request to any host.
return h
}
// getHost returns least loaded + round-robin host from cluster.
//
// Always returns non-nil.
func (c *cluster) getHost() *host {
r := c.getReplica()
return r.getHost()
}
type rateLimiter struct {
counter
}
func (rl *rateLimiter) run(done <-chan struct{}) {
for {
select {
case <-done:
return
case <-time.After(time.Minute):
rl.store(0)
}
}
}
type counter struct {
value uint32
}
func (c *counter) store(n uint32) { atomic.StoreUint32(&c.value, n) }
func (c *counter) load() uint32 { return atomic.LoadUint32(&c.value) }
func (c *counter) dec() { atomic.AddUint32(&c.value, ^uint32(0)) }
func (c *counter) inc() uint32 { return atomic.AddUint32(&c.value, 1) }