This repository has been archived by the owner on Oct 30, 2023. It is now read-only.
forked from aerokube/ggr
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathconfig.go
165 lines (149 loc) · 3.24 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
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
package main
import (
"encoding/json"
"math/rand"
"net/http"
"strings"
"sync"
"time"
. "github.com/aerokube/ggr/config"
)
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 capacity struct {
Queued *int `json:"queued,omitempty" binding:"required"`
Pending *int `json:"pending,omitempty" binding:"required"`
Used *int `json:"used,omitempty" binding:"required"`
Total *int `json:"total,omitempty" binding:"required"`
}
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() + paths.Route
}
type ggrBrowsers struct {
Browsers
}
const anyPlatform = "ANY"
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 == anyPlatform {
platform = b.DefaultPlatform
}
for _, v := range b.Versions {
if strings.HasPrefix(v.Number, version) && (v.Platform == "" || 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
}
func findFirstNodeByQueue(h *Host, hosts *Hosts, mutex *sync.RWMutex) (host *Host) {
if len(*hosts) < 1 {
return h
}
mutex.Lock()
resultMap := make(map[*Host]*capacity)
defer mutex.Unlock()
var netClient = &http.Client{
Timeout: time.Second * 10,
}
for i := range *hosts {
rsp, err := netClient.Get((*hosts)[i].StatusEndPoint())
if err != nil {
continue
}
var capa capacity
err = json.NewDecoder(rsp.Body).Decode(&capa)
if err != nil && i < len(*hosts) {
continue
}
if capa.Pending == nil || capa.Queued == nil || capa.Total == nil || capa.Used == nil {
continue
}
resultMap[&(*hosts)[i]] = &capa
}
netClient.CloseIdleConnections()
if len(resultMap) < 1 {
return h
}
targetHost := mostFreeHost(resultMap)
if targetHost == h {
return h
}
return targetHost
}
func mostFreeHost(target map[*Host]*capacity) *Host {
var queued int
var targetHost *Host
i := len(target)
for k, v := range target {
if i == len(target) {
queued = *v.Queued
targetHost = k
}
if *v.Queued < queued {
queued = *v.Queued
targetHost = k
}
i--
}
return targetHost
}