-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.go
45 lines (40 loc) · 943 Bytes
/
client.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
package client
import (
"time"
"math/rand"
"net/url"
)
type ClientECOS struct {
currentUrl string
currentIndexUrl int
currentProtocol string
url []string
maxRetries int
}
func NewClientECOS(urls []string, maxRetries int) (*ClientECOS) {
rand.Seed(time.Now().UnixNano())
au := make([]string, 0)
for _, ui := range urls {
u, err := url.Parse(ui)
if err != nil {
continue
}
if u.Scheme == "http" || u.Scheme == "https" {
au = append(au, ui)
}
}
client := &ClientECOS{url: au, maxRetries: maxRetries}
client.selectRandomServer()
return client
}
func (c *ClientECOS) selectRandomServer() {
c.currentIndexUrl = rand.Intn(len(c.url))
c.currentUrl = c.url[c.currentIndexUrl]
}
func (c *ClientECOS) getAnotherServer() {
c.currentIndexUrl ++
if len(c.url) <= c.currentIndexUrl {
c.currentIndexUrl = 0
}
c.currentUrl = c.url[c.currentIndexUrl]
}