-
Notifications
You must be signed in to change notification settings - Fork 192
/
Copy pathhreq.go
177 lines (159 loc) · 3.71 KB
/
hreq.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
166
167
168
169
170
171
172
173
174
175
176
177
// Package cmn provides common constants, types, and utilities for AIS clients
// and AIStore.
/*
* Copyright (c) 2018-2025, NVIDIA CORPORATION. All rights reserved.
*/
package cmn
import (
"context"
"io"
"net/http"
"net/url"
"sync"
"time"
"github.com/NVIDIA/aistore/cmn/cos"
)
// usage 1: initialize and fill out HTTP request.
// usage 2: intra-cluster control-plane (except streams)
// usage 3: PUT and APPEND API
// BodyR optimizes-out allocations - if non-nil and implements `io.Closer`, will always be closed by `client.Do`
type HreqArgs struct {
BodyR io.ReadCloser
Header http.Header // request headers
Query url.Values // query, e.g. ?a=x&b=y&c=z
RawQuery string // raw query
Method string
Base string // base URL, e.g. http://xyz.abc
Path string // path URL, e.g. /x/y/z
Body []byte
}
var (
hraPool sync.Pool
hra0 HreqArgs
)
var (
hpool sync.Pool
hmap sync.Map
req0 http.Request
)
func AllocHra() (a *HreqArgs) {
if v := hraPool.Get(); v != nil {
a = v.(*HreqArgs)
return
}
return &HreqArgs{}
}
func FreeHra(a *HreqArgs) {
*a = hra0
hraPool.Put(a)
}
func (u *HreqArgs) URL() string {
url := cos.JoinPath(u.Base, u.Path)
if u.RawQuery != "" {
return url + "?" + u.RawQuery
}
if rawq := u.Query.Encode(); rawq != "" {
return url + "?" + rawq
}
return url
}
// TODO: remaining usage - `api` package (remove when time permits)
func (u *HreqArgs) ReqDeprecated() (*http.Request, error) {
r := u.BodyR
if r == nil && u.Body != nil {
r = cos.NewByteReader(u.Body)
}
req, err := http.NewRequest(u.Method, u.URL(), r)
if err != nil {
return nil, err
}
if u.Header != nil {
copyHeaders(u.Header, &req.Header)
}
return req, nil
}
// NOTE: unlike standard http.NewRequest (above)
// - this method returns context-less request
// - it also does not assign req.GetBody - it is the caller's responsibility to assign one when and if required
func (u *HreqArgs) Req() (*http.Request, error) {
var (
l = len(u.Body)
rc io.ReadCloser
)
switch {
case u.BodyR != nil:
rc = u.BodyR
case l > 0:
rc = cos.NewByteReader(u.Body)
default:
rc = http.NoBody
}
req, err := newRequest(u.Method, u.URL())
if err != nil {
return nil, err
}
req.Body = rc
req.ContentLength = int64(l) // todo: preferably, with BodyR case as well
if u.Header != nil {
copyHeaders(u.Header, &req.Header)
}
return req, nil
}
// TODO: HreqFree(original req) vs "shallow copy" produced by WithContext
func (u *HreqArgs) ReqWith(timeout time.Duration) (*http.Request, context.Context, context.CancelFunc, error) {
req, err := u.Req()
if err != nil {
return nil, nil, nil, err
}
if u.Method == http.MethodPost || u.Method == http.MethodPut {
req.Header.Set(cos.HdrContentType, cos.ContentJSON)
}
ctx, cancel := context.WithTimeout(context.Background(), timeout)
req = req.WithContext(ctx)
return req, ctx, cancel, nil
}
func newRequest(method, urls string) (*http.Request, error) {
var (
err error
u *url.URL
)
// 1. parse and reuse url
if parsed, ok := hmap.Load(urls); ok {
u = parsed.(*url.URL)
} else {
u, err = url.Parse(urls)
if err != nil {
return nil, err
}
// NOTE: given current usage scenarios, there's no real need to ever clear this map
hmap.Store(urls, u)
}
// 2. reuse and initialize request
req := hreqAlloc()
{
req.Method = method
req.URL = u
req.Proto = "HTTP/1.1"
req.ProtoMajor = 1
req.ProtoMinor = 1
if req.Header == nil {
req.Header = make(http.Header, 4)
}
req.Host = u.Host
}
return req, nil
}
func hreqAlloc() *http.Request {
r := hpool.Get()
if r != nil {
return r.(*http.Request)
}
return &http.Request{}
}
func HreqFree(r *http.Request) {
hdr := r.Header
*r = req0
clear(hdr)
r.Header = hdr
hpool.Put(r)
}