-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathclient.go
64 lines (56 loc) · 1.43 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
package main
import (
"fmt"
"io/ioutil"
"log"
"net/http"
"time"
)
func main() {
server := []string{
"http://localhost:8080",
"http://localhost:8081",
"http://localhost:8082",
}
// Add a time limit for all requests made by this client.
client := &http.Client{Timeout: 10 * time.Second}
for {
before := time.Now()
res := Get(server[0], client)
//res := MultiGet(server, client)
after := time.Now()
fmt.Println("Response:", res)
fmt.Println("Time:", after.Sub(before))
fmt.Println()
time.Sleep(500 * time.Millisecond)
}
}
type Response struct {
Body string
StatusCode int
}
func (r *Response) String() string {
return fmt.Sprintf("%q (%d)", r.Body, r.StatusCode)
}
// Get makes an HTTP Get request and returns an abbreviated response.
// The response is empty if the request fails.
func Get(url string, client *http.Client) *Response {
res, err := client.Get(url)
if err != nil {
return &Response{}
}
// res.Body != nil when err == nil
defer res.Body.Close()
body, err := ioutil.ReadAll(res.Body)
if err != nil {
log.Fatalf("ReadAll: %v", err)
}
return &Response{string(body), res.StatusCode}
}
// MultiGet makes an HTTP Get request to each url and returns
// the response from the first server to answer with status code 200.
// If none of the servers answer before timeout, the response is 503
// – Service unavailable.
func MultiGet(urls []string, client *http.Client) *Response {
return nil // TODO
}