-
Notifications
You must be signed in to change notification settings - Fork 8
/
http.go
39 lines (35 loc) · 982 Bytes
/
http.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
package http
import (
"context"
"crypto/tls"
"fmt"
"net"
"net/http"
"strconv"
"time"
"golang.org/x/net/http2"
)
// Client creates a http client use h2c
func Client() *http.Client {
return &http.Client{
// Most RPC servers don't use HTTP redirects
CheckRedirect: func(_ *http.Request, _ []*http.Request) error {
return http.ErrUseLastResponse
},
Transport: &http2.Transport{
AllowHTTP: true,
DialTLSContext: func(ctx context.Context, network, addr string, cfg *tls.Config) (net.Conn, error) {
// If you're also using this client for non-h2c traffic, you may want to
// delegate to tls.Dial if the network isn't TCP or the addr isn't in an
// allow-list.
return net.Dial(network, addr)
},
PingTimeout: 30 * time.Second,
ReadIdleTimeout: 30 * time.Second,
},
}
}
// URL create a http connection address
func URL(host string, port int) string {
return fmt.Sprintf("http://%s", net.JoinHostPort(host, strconv.Itoa(port)))
}