-
Notifications
You must be signed in to change notification settings - Fork 798
/
loadbalancer.go
132 lines (111 loc) · 3.54 KB
/
loadbalancer.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
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package cluster
import (
"math/rand"
"sync"
"sync/atomic"
"time"
"mosn.io/mosn/pkg/types"
)
// NewLoadBalancer can be register self defined type
var lbFactories map[types.LoadBalancerType]func(types.HostSet) types.LoadBalancer
func RegisterLBType(lbType types.LoadBalancerType, f func(types.HostSet) types.LoadBalancer) {
if lbFactories == nil {
lbFactories = make(map[types.LoadBalancerType]func(types.HostSet) types.LoadBalancer)
}
lbFactories[lbType] = f
}
var rrFactory *roundRobinLoadBalancerFactory
func init() {
rrFactory = &roundRobinLoadBalancerFactory{
rand: rand.New(rand.NewSource(time.Now().UnixNano())),
}
RegisterLBType(types.RoundRobin, rrFactory.newRoundRobinLoadBalancer)
RegisterLBType(types.Random, newRandomLoadBalancer)
}
func NewLoadBalancer(lbType types.LoadBalancerType, hosts types.HostSet) types.LoadBalancer {
if f, ok := lbFactories[lbType]; ok {
return f(hosts)
}
return rrFactory.newRoundRobinLoadBalancer(hosts)
}
// LoadBalancer Implementations
type randomLoadBalancer struct {
mutex sync.Mutex
rand *rand.Rand
hosts types.HostSet
}
func newRandomLoadBalancer(hosts types.HostSet) types.LoadBalancer {
return &randomLoadBalancer{
rand: rand.New(rand.NewSource(time.Now().UnixNano())),
hosts: hosts,
}
}
func (lb *randomLoadBalancer) ChooseHost(context types.LoadBalancerContext) types.Host {
targets := lb.hosts.HealthyHosts()
if len(targets) == 0 {
return nil
}
lb.mutex.Lock()
defer lb.mutex.Unlock()
idx := lb.rand.Intn(len(targets))
return targets[idx]
}
func (lb *randomLoadBalancer) IsExistsHosts(metadata types.MetadataMatchCriteria) bool {
return len(lb.hosts.Hosts()) > 0
}
func (lb *randomLoadBalancer) HostNum(metadata types.MetadataMatchCriteria) int {
return len(lb.hosts.Hosts())
}
type roundRobinLoadBalancer struct {
hosts types.HostSet
rrIndex uint32
}
type roundRobinLoadBalancerFactory struct {
mutex sync.Mutex
rand *rand.Rand
}
func (f *roundRobinLoadBalancerFactory) newRoundRobinLoadBalancer(hosts types.HostSet) types.LoadBalancer {
var idx uint32
hostsList := hosts.Hosts()
f.mutex.Lock()
defer f.mutex.Unlock()
if len(hostsList) != 0 {
idx = f.rand.Uint32() % uint32(len(hostsList))
}
return &roundRobinLoadBalancer{
hosts: hosts,
rrIndex: idx,
}
}
func (lb *roundRobinLoadBalancer) ChooseHost(context types.LoadBalancerContext) types.Host {
targets := lb.hosts.HealthyHosts()
if len(targets) == 0 {
return nil
}
index := atomic.AddUint32(&lb.rrIndex, 1) % uint32(len(targets))
return targets[index]
}
func (lb *roundRobinLoadBalancer) IsExistsHosts(metadata types.MetadataMatchCriteria) bool {
return len(lb.hosts.Hosts()) > 0
}
func (lb *roundRobinLoadBalancer) HostNum(metadata types.MetadataMatchCriteria) int {
return len(lb.hosts.Hosts())
}
// TODO:
// WRR