-
Notifications
You must be signed in to change notification settings - Fork 11
/
pinger.go
83 lines (73 loc) · 1.82 KB
/
pinger.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
package health
import (
"context"
"fmt"
"net/http"
"net/url"
)
type (
// Pinger makes it possible to ping a service.
Pinger interface {
// Name of remote service.
Name() string
// Ping the remote service, return a non nil error if the
// service is not available.
Ping(context.Context) error
}
// Option configures a Pinger.
Option func(o *options)
client struct {
name string
req *http.Request
}
options struct {
scheme string
path string
}
)
// NewPinger returns a new health-check client for the given service. It panics
// if the given host address is malformed. The default scheme is "http" and the
// default path is "/livez". Both can be overridden via options.
func NewPinger(name, addr string, opts ...Option) Pinger {
options := &options{scheme: "http", path: "/livez"}
for _, o := range opts {
o(options)
}
u := url.URL{Scheme: options.scheme, Host: addr, Path: options.path}
req, err := http.NewRequest("GET", u.String(), nil)
if err != nil {
panic(err)
}
return &client{
name: name,
req: req,
}
}
func (c *client) Name() string {
return c.name
}
func (c *client) Ping(ctx context.Context) error {
resp, err := http.DefaultClient.Do(c.req.WithContext(ctx))
if err != nil {
return fmt.Errorf("failed to make health check request to %q: %v", c.name, err)
}
defer resp.Body.Close()
if resp.StatusCode < 200 || resp.StatusCode > 300 {
return fmt.Errorf("health-check for %q returned status %d", c.name, resp.StatusCode)
}
return nil
}
// WithScheme sets the scheme used to ping the service.
// Default scheme is "http".
func WithScheme(scheme string) Option {
return func(o *options) {
o.scheme = scheme
}
}
// WithPath sets the path used to ping the service.
// Default path is "/livez".
func WithPath(path string) Option {
return func(o *options) {
o.path = path
}
}