forked from gojek/heimdall
-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.go
145 lines (118 loc) · 3.55 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
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
package examples
import (
"fmt"
"io/ioutil"
"net/http"
"time"
"github.com/gojektech/heimdall"
"github.com/gojektech/heimdall/httpclient"
"github.com/gojektech/heimdall/hystrix"
"github.com/pkg/errors"
)
const (
baseURL = "http://localhost:9090"
)
func httpClientUsage() error {
timeout := 100 * time.Millisecond
httpClient := httpclient.NewClient(
httpclient.WithHTTPTimeout(timeout),
httpclient.WithRetryCount(2),
httpclient.WithRetrier(heimdall.NewRetrier(heimdall.NewConstantBackoff(10, 5))),
)
headers := http.Header{}
headers.Set("Content-Type", "application/json")
response, err := httpClient.Get(baseURL, headers)
if err != nil {
return errors.Wrap(err, "failed to make a request to server")
}
defer response.Body.Close()
respBody, err := ioutil.ReadAll(response.Body)
if err != nil {
return errors.Wrap(err, "failed to read response body")
}
fmt.Printf("Response: %s", string(respBody))
return nil
}
func hystrixClientUsage() error {
timeout := 100 * time.Millisecond
hystrixClient := hystrix.NewClient(
hystrix.WithHTTPTimeout(timeout),
hystrix.WithCommandName("MyCommand"),
hystrix.WithHystrixTimeout(1100),
hystrix.WithMaxConcurrentRequests(100),
hystrix.WithErrorPercentThreshold(25),
hystrix.WithSleepWindow(10),
hystrix.WithRequestVolumeThreshold(10),
)
headers := http.Header{}
response, err := hystrixClient.Get(baseURL, headers)
if err != nil {
return errors.Wrap(err, "failed to make a request to server")
}
defer response.Body.Close()
respBody, err := ioutil.ReadAll(response.Body)
if err != nil {
return errors.Wrap(err, "failed to read response body")
}
fmt.Printf("Response: %s", string(respBody))
return nil
}
type myHTTPClient struct {
client http.Client
}
func (c *myHTTPClient) Do(request *http.Request) (*http.Response, error) {
request.SetBasicAuth("username", "passwd")
return c.client.Do(request)
}
func customHTTPClientUsage() error {
httpClient := httpclient.NewClient(
httpclient.WithHTTPTimeout(0*time.Millisecond),
httpclient.WithHTTPClient(&myHTTPClient{
// replace with custom HTTP client
client: http.Client{Timeout: 25 * time.Millisecond},
}),
httpclient.WithRetryCount(2),
httpclient.WithRetrier(heimdall.NewRetrier(heimdall.NewConstantBackoff(10, 5))),
)
headers := http.Header{}
headers.Set("Content-Type", "application/json")
response, err := httpClient.Get(baseURL, headers)
if err != nil {
return errors.Wrap(err, "failed to make a request to server")
}
defer response.Body.Close()
respBody, err := ioutil.ReadAll(response.Body)
if err != nil {
return errors.Wrap(err, "failed to read response body")
}
fmt.Printf("Response: %s", string(respBody))
return nil
}
func customHystrixClientUsage() error {
timeout := 0 * time.Millisecond
hystrixClient := hystrix.NewClient(
hystrix.WithHTTPTimeout(timeout),
hystrix.WithCommandName("MyCommand"),
hystrix.WithHystrixTimeout(1100),
hystrix.WithMaxConcurrentRequests(100),
hystrix.WithErrorPercentThreshold(25),
hystrix.WithSleepWindow(10),
hystrix.WithRequestVolumeThreshold(10),
hystrix.WithHTTPClient(&myHTTPClient{
// replace with custom HTTP client
client: http.Client{Timeout: 25 * time.Millisecond},
}),
)
headers := http.Header{}
response, err := hystrixClient.Get(baseURL, headers)
if err != nil {
return errors.Wrap(err, "failed to make a request to server")
}
defer response.Body.Close()
respBody, err := ioutil.ReadAll(response.Body)
if err != nil {
return errors.Wrap(err, "failed to read response body")
}
fmt.Printf("Response: %s", string(respBody))
return nil
}