forked from jina-ai/client-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.go
74 lines (66 loc) · 1.78 KB
/
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
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
package client
import (
"fmt"
"math/rand"
"net"
"net/http"
"strings"
"time"
"github.com/jina-ai/client-go/jina"
"github.com/viki-org/dnscache"
)
var Resolver = dnscache.New(10 * time.Second)
var HttpClient *http.Client
type CallbackType func(*jina.DataRequestProto)
type Client interface {
POST(requests <-chan *jina.DataRequestProto, onDone, onError, onAlways CallbackType) error
SequentialPOST(requests <-chan *jina.DataRequestProto, onDone, onError, onAlways CallbackType) error
}
type HealthCheckClient interface {
HealthCheck() (bool, error)
}
// Clint & resolver taken from https://github.com/juicedata/juicefs/blob/main/pkg/object/restful.go
func updateHTTPClient() {
HttpClient = &http.Client{
Transport: &http.Transport{
Proxy: http.ProxyFromEnvironment,
TLSHandshakeTimeout: time.Second * 20,
ResponseHeaderTimeout: time.Second * 300,
IdleConnTimeout: time.Second * 300,
MaxIdleConnsPerHost: 50,
Dial: func(network string, address string) (net.Conn, error) {
separator := strings.LastIndex(address, ":")
host := address[:separator]
port := address[separator:]
ips, err := Resolver.Fetch(host)
if err != nil {
return nil, err
}
if len(ips) == 0 {
return nil, fmt.Errorf("no such host: %s", host)
}
var conn net.Conn
n := len(ips)
first := rand.Intn(n)
dialer := &net.Dialer{Timeout: time.Second * 10}
for i := 0; i < n; i++ {
ip := ips[(first+i)%n]
address = ip.String()
if port != "" {
address = net.JoinHostPort(address, port[1:])
}
conn, err = dialer.Dial(network, address)
if err == nil {
return conn, nil
}
}
return nil, err
},
DisableCompression: true,
},
Timeout: time.Hour,
}
}
func init() {
updateHTTPClient()
}