forked from aerokube/ggr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.go
97 lines (86 loc) · 1.77 KB
/
config.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
package main
import (
. "github.com/aerokube/ggr/config"
"math/rand"
"strings"
)
type set interface {
contains(el string) bool
add(el string)
size() int
}
func newSet(data ...string) *setImpl {
set := &setImpl{make(map[string]struct{})}
for _, el := range data {
set.add(el)
}
return set
}
type setImpl struct {
data map[string]struct{}
}
func (ss *setImpl) contains(el string) bool {
_, ok := ss.data[el]
return ok
}
func (ss *setImpl) add(el string) {
ss.data[el] = struct{}{}
}
func (ss *setImpl) size() int {
return len(ss.data)
}
func sessionURL(h *Host) string {
return h.Route() + routePath
}
type ggrBrowsers struct {
Browsers
}
func (b *ggrBrowsers) find(browser, version string, platform string, excludedHosts set, excludedRegions set) (Hosts, string, set) {
var hosts Hosts
for _, b := range b.Browsers.Browsers {
if b.Name == browser {
if version == "" {
version = b.DefaultVersion
}
if platform == "" {
platform = b.DefaultPlatform
}
for _, v := range b.Versions {
if strings.HasPrefix(v.Number, version) && strings.HasPrefix(v.Platform, platform) {
version = v.Number
next:
for _, r := range v.Regions {
if excludedRegions.size() == len(v.Regions) {
excludedRegions = newSet()
}
if excludedRegions.contains(r.Name) {
continue next
}
for _, h := range r.Hosts {
if !excludedHosts.contains(h.Net()) {
hosts = append(hosts, h)
}
}
}
}
}
}
}
return hosts, version, excludedRegions
}
func choose(hosts Hosts) (*Host, int) {
total := 0
for _, h := range hosts {
total += h.Count
}
if total > 0 {
r := rand.Intn(total)
for i, host := range hosts {
r -= host.Count
if r < 0 {
return &hosts[i], i
}
}
}
return nil, -1
}