mirror of
https://github.com/etcd-io/etcd.git
synced 2024-09-27 06:25:44 +00:00
client: move timeout into caller of httpClient
This commit is contained in:
parent
7c1f4a9baf
commit
9d07db4432
@ -53,12 +53,6 @@ type httpClient struct {
|
|||||||
timeout time.Duration
|
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) {
|
func (c *httpClient) do(ctx context.Context, act httpAction) (int, []byte, error) {
|
||||||
req := act.httpRequest(c.endpoint)
|
req := act.httpRequest(c.endpoint)
|
||||||
|
|
||||||
|
@ -58,12 +58,12 @@ func newHTTPKeysAPIWithPrefix(tr *http.Transport, ep string, to time.Duration, p
|
|||||||
c := &httpClient{
|
c := &httpClient{
|
||||||
transport: tr,
|
transport: tr,
|
||||||
endpoint: *u,
|
endpoint: *u,
|
||||||
timeout: to,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
kAPI := httpKeysAPI{
|
kAPI := httpKeysAPI{
|
||||||
client: c,
|
client: c,
|
||||||
prefix: prefix,
|
prefix: prefix,
|
||||||
|
timeout: to,
|
||||||
}
|
}
|
||||||
|
|
||||||
return &kAPI, nil
|
return &kAPI, nil
|
||||||
@ -102,6 +102,7 @@ func (n *Node) String() string {
|
|||||||
type httpKeysAPI struct {
|
type httpKeysAPI struct {
|
||||||
client *httpClient
|
client *httpClient
|
||||||
prefix string
|
prefix string
|
||||||
|
timeout time.Duration
|
||||||
}
|
}
|
||||||
|
|
||||||
func (k *httpKeysAPI) Create(key, val string, ttl time.Duration) (*Response, error) {
|
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
|
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 {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
@ -130,7 +133,9 @@ func (k *httpKeysAPI) Get(key string) (*Response, error) {
|
|||||||
Recursive: false,
|
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 {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
@ -25,6 +25,7 @@ import (
|
|||||||
"path"
|
"path"
|
||||||
"time"
|
"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/etcdserver/etcdhttp/httptypes"
|
||||||
"github.com/coreos/etcd/pkg/types"
|
"github.com/coreos/etcd/pkg/types"
|
||||||
)
|
)
|
||||||
@ -42,11 +43,11 @@ func NewMembersAPI(tr *http.Transport, ep string, to time.Duration) (MembersAPI,
|
|||||||
c := &httpClient{
|
c := &httpClient{
|
||||||
transport: tr,
|
transport: tr,
|
||||||
endpoint: *u,
|
endpoint: *u,
|
||||||
timeout: to,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
mAPI := httpMembersAPI{
|
mAPI := httpMembersAPI{
|
||||||
client: c,
|
client: c,
|
||||||
|
timeout: to,
|
||||||
}
|
}
|
||||||
|
|
||||||
return &mAPI, nil
|
return &mAPI, nil
|
||||||
@ -60,11 +61,14 @@ type MembersAPI interface {
|
|||||||
|
|
||||||
type httpMembersAPI struct {
|
type httpMembersAPI struct {
|
||||||
client *httpClient
|
client *httpClient
|
||||||
|
timeout time.Duration
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *httpMembersAPI) List() ([]httptypes.Member, error) {
|
func (m *httpMembersAPI) List() ([]httptypes.Member, error) {
|
||||||
req := &membersAPIActionList{}
|
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 {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
@ -88,7 +92,9 @@ func (m *httpMembersAPI) Add(peerURL string) (*httptypes.Member, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
req := &membersAPIActionAdd{peerURLs: urls}
|
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 {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
@ -107,7 +113,9 @@ func (m *httpMembersAPI) Add(peerURL string) (*httptypes.Member, error) {
|
|||||||
|
|
||||||
func (m *httpMembersAPI) Remove(memberID string) error {
|
func (m *httpMembersAPI) Remove(memberID string) error {
|
||||||
req := &membersAPIActionRemove{memberID: memberID}
|
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 {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user