-
Notifications
You must be signed in to change notification settings - Fork 189
/
Copy pathclient_fasthttp.go
102 lines (86 loc) · 2.44 KB
/
client_fasthttp.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
//go:build !nethttp
// Package transport provides long-lived http/tcp connections for
// intra-cluster communications (see README for details and usage example).
/*
* Copyright (c) 2018-2024, NVIDIA CORPORATION. All rights reserved.
*/
package transport
import (
"io"
"net"
"net/http"
"strconv"
"github.com/NVIDIA/aistore/api/apc"
"github.com/NVIDIA/aistore/cmn"
"github.com/NVIDIA/aistore/cmn/cos"
"github.com/valyala/fasthttp"
)
const ua = "aisnode/streams"
type Client interface {
Do(req *fasthttp.Request, resp *fasthttp.Response) error
}
func whichClient() string { return "fasthttp" }
// overriding fasthttp default `const DefaultDialTimeout = 3 * time.Second`
func dialTimeout(addr string) (net.Conn, error) {
return fasthttp.DialTimeout(addr, cmn.DfltDialupTimeout)
}
// intra-cluster networking: fasthttp client
func NewIntraDataClient() Client {
config := cmn.GCO.Get()
httcfg := &config.Net.HTTP
// (compare with cmn/client.go)
cl := &fasthttp.Client{
Dial: dialTimeout,
ReadBufferSize: cos.NonZero(httcfg.ReadBufferSize, int(cmn.DefaultReadBufferSize)), // 4K
WriteBufferSize: cos.NonZero(httcfg.WriteBufferSize, int(cmn.DefaultWriteBufferSize)), // ditto
}
if config.Net.HTTP.UseHTTPS {
tlsConfig, err := cmn.NewTLS(config.Net.HTTP.ToTLS(), true /*intra-cluster*/) // streams
if err != nil {
cos.ExitLog(err)
}
cl.TLSConfig = tlsConfig
}
return cl
}
func (s *streamBase) doPlain(body io.Reader) (err error) {
var (
req = fasthttp.AcquireRequest()
resp = fasthttp.AcquireResponse()
)
err = s._do(body, req, resp)
fasthttp.ReleaseRequest(req)
fasthttp.ReleaseResponse(resp)
return err
}
func (s *streamBase) doCmpr(body io.Reader) (err error) {
var (
req = fasthttp.AcquireRequest()
resp = fasthttp.AcquireResponse()
)
req.Header.Set(apc.HdrCompress, apc.LZ4Compression)
err = s._do(body, req, resp)
fasthttp.ReleaseRequest(req)
fasthttp.ReleaseResponse(resp)
s.streamer.resetCompression()
return err
}
func (s *streamBase) _do(body io.Reader, req *fasthttp.Request, resp *fasthttp.Response) (err error) {
req.Header.SetMethod(http.MethodPut)
req.SetRequestURI(s.dstURL)
req.SetBodyStream(body, -1)
req.Header.Set(apc.HdrSessID, strconv.FormatInt(s.sessID, 10))
req.Header.Set(cos.HdrUserAgent, ua)
// do
err = s.client.Do(req, resp)
if err != nil {
s.yelp(err)
return err
}
// drain response & cleanup
err = resp.BodyWriteTo(io.Discard)
if err != nil {
s.yelp(err)
}
return nil
}