client: move timeout into caller of httpClient

This commit is contained in:
Brian Waldon 2014-10-30 17:00:16 -07:00
parent 7c1f4a9baf
commit 9d07db4432
3 changed files with 26 additions and 19 deletions

View File

@ -53,12 +53,6 @@ type httpClient struct {
timeout time.Duration
}
func (c *httpClient) doWithTimeout(act httpAction) (int, []byte, error) {
ctx, cancel := context.WithTimeout(context.Background(), c.timeout)
defer cancel()
return c.do(ctx, act)
}
func (c *httpClient) do(ctx context.Context, act httpAction) (int, []byte, error) {
req := act.httpRequest(c.endpoint)

View File

@ -58,12 +58,12 @@ func newHTTPKeysAPIWithPrefix(tr *http.Transport, ep string, to time.Duration, p
c := &httpClient{
transport: tr,
endpoint: *u,
timeout: to,
}
kAPI := httpKeysAPI{
client: c,
prefix: prefix,
client: c,
prefix: prefix,
timeout: to,
}
return &kAPI, nil
@ -100,8 +100,9 @@ func (n *Node) String() string {
}
type httpKeysAPI struct {
client *httpClient
prefix string
client *httpClient
prefix string
timeout time.Duration
}
func (k *httpKeysAPI) Create(key, val string, ttl time.Duration) (*Response, error) {
@ -115,7 +116,9 @@ func (k *httpKeysAPI) Create(key, val string, ttl time.Duration) (*Response, err
create.TTL = &uttl
}
code, body, err := k.client.doWithTimeout(create)
ctx, cancel := context.WithTimeout(context.Background(), k.timeout)
code, body, err := k.client.do(ctx, create)
cancel()
if err != nil {
return nil, err
}
@ -130,7 +133,9 @@ func (k *httpKeysAPI) Get(key string) (*Response, error) {
Recursive: false,
}
code, body, err := k.client.doWithTimeout(get)
ctx, cancel := context.WithTimeout(context.Background(), k.timeout)
code, body, err := k.client.do(ctx, get)
cancel()
if err != nil {
return nil, err
}

View File

@ -25,6 +25,7 @@ import (
"path"
"time"
"github.com/coreos/etcd/Godeps/_workspace/src/code.google.com/p/go.net/context"
"github.com/coreos/etcd/etcdserver/etcdhttp/httptypes"
"github.com/coreos/etcd/pkg/types"
)
@ -42,11 +43,11 @@ func NewMembersAPI(tr *http.Transport, ep string, to time.Duration) (MembersAPI,
c := &httpClient{
transport: tr,
endpoint: *u,
timeout: to,
}
mAPI := httpMembersAPI{
client: c,
client: c,
timeout: to,
}
return &mAPI, nil
@ -59,12 +60,15 @@ type MembersAPI interface {
}
type httpMembersAPI struct {
client *httpClient
client *httpClient
timeout time.Duration
}
func (m *httpMembersAPI) List() ([]httptypes.Member, error) {
req := &membersAPIActionList{}
code, body, err := m.client.doWithTimeout(req)
ctx, cancel := context.WithTimeout(context.Background(), m.timeout)
code, body, err := m.client.do(ctx, req)
cancel()
if err != nil {
return nil, err
}
@ -88,7 +92,9 @@ func (m *httpMembersAPI) Add(peerURL string) (*httptypes.Member, error) {
}
req := &membersAPIActionAdd{peerURLs: urls}
code, body, err := m.client.doWithTimeout(req)
ctx, cancel := context.WithTimeout(context.Background(), m.timeout)
code, body, err := m.client.do(ctx, req)
cancel()
if err != nil {
return nil, err
}
@ -107,7 +113,9 @@ func (m *httpMembersAPI) Add(peerURL string) (*httptypes.Member, error) {
func (m *httpMembersAPI) Remove(memberID string) error {
req := &membersAPIActionRemove{memberID: memberID}
code, _, err := m.client.doWithTimeout(req)
ctx, cancel := context.WithTimeout(context.Background(), m.timeout)
code, _, err := m.client.do(ctx, req)
cancel()
if err != nil {
return err
}