-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathendpoint.go
44 lines (40 loc) · 991 Bytes
/
endpoint.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
package detective
import (
"encoding/json"
"errors"
"net/http"
"time"
)
// Doer represents the standard HTTP client interface
type Doer interface {
Do(*http.Request) (*http.Response, error)
}
type endpoint struct {
name string
req http.Request
client Doer
}
func (e *endpoint) getState(fromChain string) State {
init := time.Now()
currentReq := e.req
currentReq.Header.Set(fromHeader, fromChain)
res, err := e.client.Do(¤tReq)
diff := time.Now().Sub(init)
s := State{Name: e.name, Latency: diff}
if err != nil {
return s.withError(err)
}
if res.StatusCode != http.StatusOK {
return s.withError(errors.New("service " + e.name + " returned http status: " + res.Status))
}
if res.Body == nil {
return s.withError(errors.New("service " + e.name + " returned no response body"))
}
defer res.Body.Close()
var state State
if err := json.NewDecoder(res.Body).Decode(&state); err != nil {
return s.withError(err)
}
state.Latency = diff
return state
}