-
Notifications
You must be signed in to change notification settings - Fork 10
/
apicache.go
104 lines (90 loc) · 2.8 KB
/
apicache.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
package apicache
import (
"net"
"net/http"
"time"
"github.com/antihax/httpcache"
httpredis "github.com/antihax/httpcache/redis"
"github.com/garyburd/redigo/redis"
)
// CreateHTTPClientCache creates an error limiting client with auto retry and redis cache
func CreateHTTPClientCache(redis *redis.Pool) *http.Client {
// Create a Redis http client for the CCP APIs.
transportCache := httpcache.NewTransport(httpredis.NewWithClient(redis))
// Attach a basic transport with our chained custom transport.
transportCache.Transport = &APICacheTransport{
&http.Transport{
MaxIdleConns: 2,
DialContext: (&net.Dialer{
Timeout: 10 * time.Second,
KeepAlive: 10 * time.Second,
}).DialContext,
ForceAttemptHTTP2: true,
IdleConnTimeout: 10 * time.Second,
TLSHandshakeTimeout: 10 * time.Second,
ResponseHeaderTimeout: 15 * time.Second,
ExpectContinueTimeout: 1 * time.Second,
MaxIdleConnsPerHost: 2,
},
}
client := &http.Client{Transport: transportCache}
if client == nil {
panic("http client is null")
}
return client
}
// CreateLimitedHTTPClientCache creates an error limiting client with auto retry, redis cache,
// and limit it to 100 connections
func CreateLimitedHTTPClientCache(redis *redis.Pool) *http.Client {
// Create a Redis http client for the CCP APIs.
transportCache := httpcache.NewTransport(httpredis.NewWithClient(redis))
cache := &LimitedTransport{
&APICacheTransport{
&http.Transport{
MaxIdleConns: 2,
DialContext: (&net.Dialer{
Timeout: 10 * time.Second,
KeepAlive: 10 * time.Second,
}).DialContext,
ForceAttemptHTTP2: true,
IdleConnTimeout: 10 * time.Second,
TLSHandshakeTimeout: 10 * time.Second,
ResponseHeaderTimeout: 15 * time.Second,
ExpectContinueTimeout: 1 * time.Second,
MaxIdleConnsPerHost: 2,
},
},
}
// Attach a basic transport with our chained custom transport.
transportCache.Transport = cache
client := &http.Client{Transport: transportCache}
if client == nil {
panic("http client is null")
}
return client
}
// CreateHTTPClientCache creates an error limiting client with auto retry and no cache
func CreateHTTPClient() *http.Client {
// Create a Redis http client for the CCP APIs.
// Attach a basic transport with our chained custom transport.
t := &APICacheTransport{
&http.Transport{
MaxIdleConns: 2,
DialContext: (&net.Dialer{
Timeout: 10 * time.Second,
KeepAlive: 10 * time.Second,
}).DialContext,
ForceAttemptHTTP2: true,
IdleConnTimeout: 10 * time.Second,
TLSHandshakeTimeout: 10 * time.Second,
ResponseHeaderTimeout: 15 * time.Second,
ExpectContinueTimeout: 1 * time.Second,
MaxIdleConnsPerHost: 2,
},
}
client := &http.Client{Transport: t}
if client == nil {
panic("http client is null")
}
return client
}