-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
102 lines (89 loc) · 1.58 KB
/
main.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
package main
import (
"fmt"
)
type URL struct {
Url string //url连接
Weight int64 //权重
CallTimes int64 //调用次数统计
}
var weitht int64 = 0
var index int64 = -1
var urls []*URL = []*URL{
&URL{
Url: "127.0.0.1:8080",
Weight: 2,
CallTimes: 0,
},
&URL{
Url: "127.0.0.1:8081",
Weight: 4,
CallTimes: 0,
},
}
//计算两个数的最大公约数
func gcd(a, b int64) int64 {
if b == 0 {
return a
}
return gcd(b, a%b)
}
//计算多个数的最大公约数
func getGCD() int64 {
var weights []int64
for _, url := range urls {
weights = append(weights, url.Weight)
}
g := weights[0]
for i := 1; i < len(weights)-1; i++ {
oldGcd := g
g = gcd(oldGcd, weights[i])
}
return g
}
//获取当前轮次的url
func getUrl() *URL {
gcd := getGCD()
for {
index = (index + 1) % int64(len(urls))
if index == 0 {
weitht = weitht - gcd
if weitht <= 0 {
weitht = getMaxWeight()
if weitht == 0 {
return &URL{}
}
}
}
if urls[index].Weight >= weitht {
return urls[index]
}
}
}
//获取最大权重
func getMaxWeight() int64 {
var max int64 = 0
for _, url := range urls {
if url.Weight >= int64(max) {
max = url.Weight
}
}
return max
}
//测试函数
func main() {
//模拟1000次调用,统计每个url的调用次数
for i := 0; i < 1000; i++ {
url := getUrl()
fmt.Println("call: ", url.Url)
if url.CallTimes != 0 {
url.CallTimes++
} else {
url.CallTimes = 1
}
}
fmt.Printf("result : \n")
for i, url := range urls {
fmt.Println(i, url.Url, url.Weight, url.CallTimes)
}
}