-
Notifications
You must be signed in to change notification settings - Fork 13
/
mixinnet_clients.go
61 lines (50 loc) · 1.21 KB
/
mixinnet_clients.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
package mixin
import (
"context"
"math/rand"
"time"
"github.com/go-resty/resty/v2"
)
var (
mixinnetHosts = []string{
"http://node-42.f1ex.io:8239",
"http://mixin-node-01.b.watch:8239",
"http://mixin-node-02.b.watch:8239",
"http://lehigh.hotot.org:8239",
"http://node-okashi.mixin.fan:8239",
}
mixinNetClients = map[string]*resty.Client{}
)
func UseMixinNetHosts(hosts []string) {
if len(hosts) == 0 {
panic("empty mixin net host")
}
mixinnetHosts = hosts
mixinNetClients = map[string]*resty.Client{}
}
func MixinNetClientFromContext(ctx context.Context) *resty.Client {
var host string
if v := ctx.Value(mixinnetHostKey); v != nil {
if h, ok := v.(string); ok && h != "" {
host = h
}
}
if host == "" {
host = RandomMixinNetHost()
}
if client, ok := mixinNetClients[host]; ok {
return client
}
client := resty.New().
SetHeader("Content-Type", "application/json").
SetHostURL(host).
SetTimeout(10 * time.Second)
mixinNetClients[host] = client
return client
}
func WithMixinNetHost(ctx context.Context, host string) context.Context {
return context.WithValue(ctx, mixinnetHostKey, host)
}
func RandomMixinNetHost() string {
return mixinnetHosts[rand.Int()%len(mixinnetHosts)]
}