Skip to content

Commit

Permalink
Add context to direct http health connections
Browse files Browse the repository at this point in the history
  • Loading branch information
croomes committed Sep 12, 2017
1 parent dade5ca commit 1c1b62e
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 10 deletions.
14 changes: 6 additions & 8 deletions health.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,9 @@ package storageos
import (
"context"
"encoding/json"
//"errors"
//"fmt"
"github.com/storageos/go-api/types"
"net/http"
//"net/url"

"github.com/storageos/go-api/types"
)

var (
Expand All @@ -16,7 +14,7 @@ var (
)

// CPHealth returns the health of the control plane server at a given url.
func (c *Client) CPHealth(hostname string) (*types.CPHealthStatus, error) {
func (c *Client) CPHealth(ctx context.Context, hostname string) (*types.CPHealthStatus, error) {

req, err := http.NewRequest("GET", "http://"+hostname+":5705/v1/"+HealthAPIPrefix, nil)
if err != nil {
Expand All @@ -28,7 +26,7 @@ func (c *Client) CPHealth(hostname string) (*types.CPHealthStatus, error) {
req.SetBasicAuth(c.username, c.secret)
}

resp, err := c.HTTPClient.Do(req.WithContext(context.Background()))
resp, err := c.HTTPClient.Do(req.WithContext(ctx))
if err != nil {
return nil, err
}
Expand All @@ -43,7 +41,7 @@ func (c *Client) CPHealth(hostname string) (*types.CPHealthStatus, error) {
}

// DPHealth returns the health of the data plane server at a given url.
func (c *Client) DPHealth(hostname string) (*types.DPHealthStatus, error) {
func (c *Client) DPHealth(ctx context.Context, hostname string) (*types.DPHealthStatus, error) {

req, err := http.NewRequest("GET", "http://"+hostname+":8001/v1/"+HealthAPIPrefix, nil)
if err != nil {
Expand All @@ -55,7 +53,7 @@ func (c *Client) DPHealth(hostname string) (*types.DPHealthStatus, error) {
req.SetBasicAuth(c.username, c.secret)
}

resp, err := c.HTTPClient.Do(req.WithContext(context.Background()))
resp, err := c.HTTPClient.Do(req.WithContext(ctx))
if err != nil {
return nil, err
}
Expand Down
5 changes: 3 additions & 2 deletions health_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package storageos

import (
"context"
"encoding/json"
"net/http"
"reflect"
Expand Down Expand Up @@ -45,7 +46,7 @@ func TestCPHealth(t *testing.T) {
}

client := newTestClient(&FakeRoundTripper{message: data, status: http.StatusOK})
cpHealth, err := client.CPHealth("someHostname")
cpHealth, err := client.CPHealth(context.Background(), "someHostname")
if err != nil {
t.Error(err)
}
Expand Down Expand Up @@ -96,7 +97,7 @@ func TestDPHealth(t *testing.T) {
}

client := newTestClient(&FakeRoundTripper{message: data, status: http.StatusOK})
dpHealth, err := client.DPHealth("someHostname")
dpHealth, err := client.DPHealth(context.Background(), "someHostname")
if err != nil {
t.Error(err)
}
Expand Down

0 comments on commit 1c1b62e

Please sign in to comment.