Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 19 additions & 7 deletions health/pinger.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"net/http"
"net/url"
"time"
)

type (
Expand All @@ -21,13 +22,15 @@ type (
Option func(o *options)

client struct {
name string
req *http.Request
name string
req *http.Request
httpClient *http.Client
}

options struct {
scheme string
path string
scheme string
path string
timeout time.Duration
}
)

Expand All @@ -45,8 +48,9 @@ func NewPinger(name, addr string, opts ...Option) Pinger {
panic(err)
}
return &client{
name: name,
req: req,
name: name,
req: req,
httpClient: &http.Client{Timeout: options.timeout},
}
}

Expand All @@ -55,7 +59,7 @@ func (c *client) Name() string {
}

func (c *client) Ping(ctx context.Context) error {
resp, err := http.DefaultClient.Do(c.req.WithContext(ctx))
resp, err := c.httpClient.Do(c.req.WithContext(ctx))
if err != nil {
return fmt.Errorf("failed to make health check request to %q: %v", c.name, err)
}
Expand All @@ -81,3 +85,11 @@ func WithPath(path string) Option {
o.path = path
}
}

// WithTimeout sets the timeout used to ping the service.
// Default is no timeout.
func WithTimeout(timeout time.Duration) Option {
return func(o *options) {
o.timeout = timeout
}
}
38 changes: 31 additions & 7 deletions health/pinger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"net/http/httptest"
"net/url"
"testing"
"time"
)

func TestPing(t *testing.T) {
Expand Down Expand Up @@ -58,18 +59,38 @@ func TestPing(t *testing.T) {
}
})
}
t.Run("timeout", func(t *testing.T) {
handler := func(w http.ResponseWriter, r *http.Request) {
time.Sleep(2 * time.Second)
w.WriteHeader(http.StatusOK)
}
svr := httptest.NewServer(http.HandlerFunc(handler))
defer svr.Close()
u, _ := url.Parse(svr.URL)
pinger := NewPinger("dependency", u.Host, WithTimeout(time.Second))
if pinger.Name() != "dependency" {
t.Errorf("got name: %s, expected dependency", pinger.Name())
}
err := pinger.Ping(context.Background())
expected := fmt.Errorf(`failed to make health check request to "dependency": Get "%s/livez": context deadline exceeded (Client.Timeout exceeded while awaiting headers)`, svr.URL)
if err != nil && err.Error() != expected.Error() {
t.Errorf("got: %v, expected: %v", err, expected)
}
})
}

func TestOptions(t *testing.T) {
cases := []struct {
name string
option Option
expectedScheme string
expectedPath string
name string
option Option
expectedScheme string
expectedPath string
expectedTimeout time.Duration
}{
{"default", nil, "http", "/livez"},
{"scheme", WithScheme("https"), "https", "/livez"},
{"path", WithPath("/healthcheck"), "http", "/healthcheck"},
{"default", nil, "http", "/livez", 0},
{"scheme", WithScheme("https"), "https", "/livez", 0},
{"path", WithPath("/healthcheck"), "http", "/healthcheck", 0},
{"timeout", WithTimeout(10 * time.Second), "http", "/livez", 10 * time.Second},
}
for _, c := range cases {
t.Run(c.name, func(t *testing.T) {
Expand All @@ -88,6 +109,9 @@ func TestOptions(t *testing.T) {
if pinger.req.URL.Path != c.expectedPath {
t.Errorf("got path: %s, expected %s", pinger.req.URL.Path, c.expectedPath)
}
if pinger.httpClient.Timeout != c.expectedTimeout {
t.Errorf("got timeout: %s, expected %s", pinger.httpClient.Timeout, c.expectedTimeout)
}
})
}
}
Loading