From dafa61ad10d67d1992201f8499ef681e01de8a09 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?M=C3=A1rio=20Freitas?= Date: Fri, 16 Jan 2015 18:54:55 +0900 Subject: [PATCH 1/3] fixed: body not closed for non HTTP 200 responses --- api/acl.go | 24 ++++++++++++++++++------ api/agent.go | 44 +++++++++++++++++++++++++++++++++----------- api/catalog.go | 28 +++++++++++++++++++++------- api/event.go | 8 ++++++-- api/health.go | 16 ++++++++++++---- api/kv.go | 8 ++++++-- api/session.go | 24 ++++++++++++++++++------ api/status.go | 8 ++++++-- 8 files changed, 120 insertions(+), 40 deletions(-) diff --git a/api/acl.go b/api/acl.go index c3fb0d53aa..dfb185bd2a 100644 --- a/api/acl.go +++ b/api/acl.go @@ -34,10 +34,12 @@ func (a *ACL) Create(acl *ACLEntry, q *WriteOptions) (string, *WriteMeta, error) r.setWriteOptions(q) r.obj = acl rtt, resp, err := requireOK(a.c.doRequest(r)) + if resp != nil { + defer resp.Body.Close() + } if err != nil { return "", nil, err } - defer resp.Body.Close() wm := &WriteMeta{RequestTime: rtt} var out struct{ ID string } @@ -53,10 +55,12 @@ func (a *ACL) Update(acl *ACLEntry, q *WriteOptions) (*WriteMeta, error) { r.setWriteOptions(q) r.obj = acl rtt, resp, err := requireOK(a.c.doRequest(r)) + if resp != nil { + defer resp.Body.Close() + } if err != nil { return nil, err } - defer resp.Body.Close() wm := &WriteMeta{RequestTime: rtt} return wm, nil @@ -67,10 +71,12 @@ func (a *ACL) Destroy(id string, q *WriteOptions) (*WriteMeta, error) { r := a.c.newRequest("PUT", "/v1/acl/destroy/"+id) r.setWriteOptions(q) rtt, resp, err := requireOK(a.c.doRequest(r)) + if resp != nil { + defer resp.Body.Close() + } if err != nil { return nil, err } - resp.Body.Close() wm := &WriteMeta{RequestTime: rtt} return wm, nil @@ -81,10 +87,12 @@ func (a *ACL) Clone(id string, q *WriteOptions) (string, *WriteMeta, error) { r := a.c.newRequest("PUT", "/v1/acl/clone/"+id) r.setWriteOptions(q) rtt, resp, err := requireOK(a.c.doRequest(r)) + if resp != nil { + defer resp.Body.Close() + } if err != nil { return "", nil, err } - defer resp.Body.Close() wm := &WriteMeta{RequestTime: rtt} var out struct{ ID string } @@ -99,10 +107,12 @@ func (a *ACL) Info(id string, q *QueryOptions) (*ACLEntry, *QueryMeta, error) { r := a.c.newRequest("GET", "/v1/acl/info/"+id) r.setQueryOptions(q) rtt, resp, err := requireOK(a.c.doRequest(r)) + if resp != nil { + defer resp.Body.Close() + } if err != nil { return nil, nil, err } - defer resp.Body.Close() qm := &QueryMeta{} parseQueryMeta(resp, qm) @@ -123,10 +133,12 @@ func (a *ACL) List(q *QueryOptions) ([]*ACLEntry, *QueryMeta, error) { r := a.c.newRequest("GET", "/v1/acl/list") r.setQueryOptions(q) rtt, resp, err := requireOK(a.c.doRequest(r)) + if resp != nil { + defer resp.Body.Close() + } if err != nil { return nil, nil, err } - defer resp.Body.Close() qm := &QueryMeta{} parseQueryMeta(resp, qm) diff --git a/api/agent.go b/api/agent.go index c31395e189..e200274a86 100644 --- a/api/agent.go +++ b/api/agent.go @@ -82,10 +82,12 @@ func (c *Client) Agent() *Agent { func (a *Agent) Self() (map[string]map[string]interface{}, error) { r := a.c.newRequest("GET", "/v1/agent/self") _, resp, err := requireOK(a.c.doRequest(r)) + if resp != nil { + defer resp.Body.Close() + } if err != nil { return nil, err } - defer resp.Body.Close() var out map[string]map[string]interface{} if err := decodeBody(resp, &out); err != nil { @@ -112,10 +114,12 @@ func (a *Agent) NodeName() (string, error) { func (a *Agent) Checks() (map[string]*AgentCheck, error) { r := a.c.newRequest("GET", "/v1/agent/checks") _, resp, err := requireOK(a.c.doRequest(r)) + if resp != nil { + defer resp.Body.Close() + } if err != nil { return nil, err } - defer resp.Body.Close() var out map[string]*AgentCheck if err := decodeBody(resp, &out); err != nil { @@ -128,10 +132,12 @@ func (a *Agent) Checks() (map[string]*AgentCheck, error) { func (a *Agent) Services() (map[string]*AgentService, error) { r := a.c.newRequest("GET", "/v1/agent/services") _, resp, err := requireOK(a.c.doRequest(r)) + if resp != nil { + defer resp.Body.Close() + } if err != nil { return nil, err } - defer resp.Body.Close() var out map[string]*AgentService if err := decodeBody(resp, &out); err != nil { @@ -148,10 +154,12 @@ func (a *Agent) Members(wan bool) ([]*AgentMember, error) { r.params.Set("wan", "1") } _, resp, err := requireOK(a.c.doRequest(r)) + if resp != nil { + defer resp.Body.Close() + } if err != nil { return nil, err } - defer resp.Body.Close() var out []*AgentMember if err := decodeBody(resp, &out); err != nil { @@ -166,10 +174,12 @@ func (a *Agent) ServiceRegister(service *AgentServiceRegistration) error { r := a.c.newRequest("PUT", "/v1/agent/service/register") r.obj = service _, resp, err := requireOK(a.c.doRequest(r)) + if resp != nil { + defer resp.Body.Close() + } if err != nil { return err } - resp.Body.Close() return nil } @@ -178,10 +188,12 @@ func (a *Agent) ServiceRegister(service *AgentServiceRegistration) error { func (a *Agent) ServiceDeregister(serviceID string) error { r := a.c.newRequest("PUT", "/v1/agent/service/deregister/"+serviceID) _, resp, err := requireOK(a.c.doRequest(r)) + if resp != nil { + defer resp.Body.Close() + } if err != nil { return err } - resp.Body.Close() return nil } @@ -213,10 +225,12 @@ func (a *Agent) UpdateTTL(checkID, note, status string) error { r := a.c.newRequest("PUT", endpoint) r.params.Set("note", note) _, resp, err := requireOK(a.c.doRequest(r)) + if resp != nil { + defer resp.Body.Close() + } if err != nil { return err } - resp.Body.Close() return nil } @@ -226,10 +240,12 @@ func (a *Agent) CheckRegister(check *AgentCheckRegistration) error { r := a.c.newRequest("PUT", "/v1/agent/check/register") r.obj = check _, resp, err := requireOK(a.c.doRequest(r)) + if resp != nil { + defer resp.Body.Close() + } if err != nil { return err } - resp.Body.Close() return nil } @@ -238,10 +254,12 @@ func (a *Agent) CheckRegister(check *AgentCheckRegistration) error { func (a *Agent) CheckDeregister(checkID string) error { r := a.c.newRequest("PUT", "/v1/agent/check/deregister/"+checkID) _, resp, err := requireOK(a.c.doRequest(r)) + if resp != nil { + defer resp.Body.Close() + } if err != nil { return err } - resp.Body.Close() return nil } @@ -253,10 +271,12 @@ func (a *Agent) Join(addr string, wan bool) error { r.params.Set("wan", "1") } _, resp, err := requireOK(a.c.doRequest(r)) + if resp != nil { + defer resp.Body.Close() + } if err != nil { return err } - resp.Body.Close() return nil } @@ -264,9 +284,11 @@ func (a *Agent) Join(addr string, wan bool) error { func (a *Agent) ForceLeave(node string) error { r := a.c.newRequest("PUT", "/v1/agent/force-leave/"+node) _, resp, err := requireOK(a.c.doRequest(r)) + if resp != nil { + defer resp.Body.Close() + } if err != nil { return err } - resp.Body.Close() return nil } diff --git a/api/catalog.go b/api/catalog.go index fee1695677..5336c87d4f 100644 --- a/api/catalog.go +++ b/api/catalog.go @@ -50,10 +50,12 @@ func (c *Catalog) Register(reg *CatalogRegistration, q *WriteOptions) (*WriteMet r.setWriteOptions(q) r.obj = reg rtt, resp, err := requireOK(c.c.doRequest(r)) + if resp != nil { + defer resp.Body.Close() + } if err != nil { return nil, err } - resp.Body.Close() wm := &WriteMeta{} wm.RequestTime = rtt @@ -66,10 +68,12 @@ func (c *Catalog) Deregister(dereg *CatalogDeregistration, q *WriteOptions) (*Wr r.setWriteOptions(q) r.obj = dereg rtt, resp, err := requireOK(c.c.doRequest(r)) + if resp != nil { + defer resp.Body.Close() + } if err != nil { return nil, err } - resp.Body.Close() wm := &WriteMeta{} wm.RequestTime = rtt @@ -81,10 +85,12 @@ func (c *Catalog) Deregister(dereg *CatalogDeregistration, q *WriteOptions) (*Wr func (c *Catalog) Datacenters() ([]string, error) { r := c.c.newRequest("GET", "/v1/catalog/datacenters") _, resp, err := requireOK(c.c.doRequest(r)) + if resp != nil { + defer resp.Body.Close() + } if err != nil { return nil, err } - defer resp.Body.Close() var out []string if err := decodeBody(resp, &out); err != nil { @@ -98,10 +104,12 @@ func (c *Catalog) Nodes(q *QueryOptions) ([]*Node, *QueryMeta, error) { r := c.c.newRequest("GET", "/v1/catalog/nodes") r.setQueryOptions(q) rtt, resp, err := requireOK(c.c.doRequest(r)) + if resp != nil { + defer resp.Body.Close() + } if err != nil { return nil, nil, err } - defer resp.Body.Close() qm := &QueryMeta{} parseQueryMeta(resp, qm) @@ -119,10 +127,12 @@ func (c *Catalog) Services(q *QueryOptions) (map[string][]string, *QueryMeta, er r := c.c.newRequest("GET", "/v1/catalog/services") r.setQueryOptions(q) rtt, resp, err := requireOK(c.c.doRequest(r)) + if resp != nil { + defer resp.Body.Close() + } if err != nil { return nil, nil, err } - defer resp.Body.Close() qm := &QueryMeta{} parseQueryMeta(resp, qm) @@ -143,10 +153,12 @@ func (c *Catalog) Service(service, tag string, q *QueryOptions) ([]*CatalogServi r.params.Set("tag", tag) } rtt, resp, err := requireOK(c.c.doRequest(r)) + if resp != nil { + defer resp.Body.Close() + } if err != nil { return nil, nil, err } - defer resp.Body.Close() qm := &QueryMeta{} parseQueryMeta(resp, qm) @@ -164,10 +176,12 @@ func (c *Catalog) Node(node string, q *QueryOptions) (*CatalogNode, *QueryMeta, r := c.c.newRequest("GET", "/v1/catalog/node/"+node) r.setQueryOptions(q) rtt, resp, err := requireOK(c.c.doRequest(r)) + if resp != nil { + defer resp.Body.Close() + } if err != nil { return nil, nil, err } - defer resp.Body.Close() qm := &QueryMeta{} parseQueryMeta(resp, qm) diff --git a/api/event.go b/api/event.go index 85b5b069b0..175c1a23d0 100644 --- a/api/event.go +++ b/api/event.go @@ -47,10 +47,12 @@ func (e *Event) Fire(params *UserEvent, q *WriteOptions) (string, *WriteMeta, er } rtt, resp, err := requireOK(e.c.doRequest(r)) + if resp != nil { + defer resp.Body.Close() + } if err != nil { return "", nil, err } - defer resp.Body.Close() wm := &WriteMeta{RequestTime: rtt} var out UserEvent @@ -71,10 +73,12 @@ func (e *Event) List(name string, q *QueryOptions) ([]*UserEvent, *QueryMeta, er r.params.Set("name", name) } rtt, resp, err := requireOK(e.c.doRequest(r)) + if resp != nil { + defer resp.Body.Close() + } if err != nil { return nil, nil, err } - defer resp.Body.Close() qm := &QueryMeta{} parseQueryMeta(resp, qm) diff --git a/api/health.go b/api/health.go index 02b161e28e..cf6889028a 100644 --- a/api/health.go +++ b/api/health.go @@ -38,10 +38,12 @@ func (h *Health) Node(node string, q *QueryOptions) ([]*HealthCheck, *QueryMeta, r := h.c.newRequest("GET", "/v1/health/node/"+node) r.setQueryOptions(q) rtt, resp, err := requireOK(h.c.doRequest(r)) + if resp != nil { + defer resp.Body.Close() + } if err != nil { return nil, nil, err } - defer resp.Body.Close() qm := &QueryMeta{} parseQueryMeta(resp, qm) @@ -59,10 +61,12 @@ func (h *Health) Checks(service string, q *QueryOptions) ([]*HealthCheck, *Query r := h.c.newRequest("GET", "/v1/health/checks/"+service) r.setQueryOptions(q) rtt, resp, err := requireOK(h.c.doRequest(r)) + if resp != nil { + defer resp.Body.Close() + } if err != nil { return nil, nil, err } - defer resp.Body.Close() qm := &QueryMeta{} parseQueryMeta(resp, qm) @@ -88,10 +92,12 @@ func (h *Health) Service(service, tag string, passingOnly bool, q *QueryOptions) r.params.Set("passing", "1") } rtt, resp, err := requireOK(h.c.doRequest(r)) + if resp != nil { + defer resp.Body.Close() + } if err != nil { return nil, nil, err } - defer resp.Body.Close() qm := &QueryMeta{} parseQueryMeta(resp, qm) @@ -119,10 +125,12 @@ func (h *Health) State(state string, q *QueryOptions) ([]*HealthCheck, *QueryMet r := h.c.newRequest("GET", "/v1/health/state/"+state) r.setQueryOptions(q) rtt, resp, err := requireOK(h.c.doRequest(r)) + if resp != nil { + defer resp.Body.Close() + } if err != nil { return nil, nil, err } - defer resp.Body.Close() qm := &QueryMeta{} parseQueryMeta(resp, qm) diff --git a/api/kv.go b/api/kv.go index ba74057fcc..1da6c5a3c8 100644 --- a/api/kv.go +++ b/api/kv.go @@ -175,10 +175,12 @@ func (k *KV) put(key string, params map[string]string, body []byte, q *WriteOpti } r.body = bytes.NewReader(body) rtt, resp, err := requireOK(k.c.doRequest(r)) + if resp != nil { + defer resp.Body.Close() + } if err != nil { return false, nil, err } - defer resp.Body.Close() qm := &WriteMeta{} qm.RequestTime = rtt @@ -219,10 +221,12 @@ func (k *KV) deleteInternal(key string, params map[string]string, q *WriteOption r.params.Set(param, val) } rtt, resp, err := requireOK(k.c.doRequest(r)) + if resp != nil { + defer resp.Body.Close() + } if err != nil { return false, nil, err } - defer resp.Body.Close() qm := &WriteMeta{} qm.RequestTime = rtt diff --git a/api/session.go b/api/session.go index e889bbe0de..e840df1c5e 100644 --- a/api/session.go +++ b/api/session.go @@ -97,10 +97,12 @@ func (s *Session) create(obj interface{}, q *WriteOptions) (string, *WriteMeta, r.setWriteOptions(q) r.obj = obj rtt, resp, err := requireOK(s.c.doRequest(r)) + if resp != nil { + defer resp.Body.Close() + } if err != nil { return "", nil, err } - defer resp.Body.Close() wm := &WriteMeta{RequestTime: rtt} var out struct{ ID string } @@ -115,10 +117,12 @@ func (s *Session) Destroy(id string, q *WriteOptions) (*WriteMeta, error) { r := s.c.newRequest("PUT", "/v1/session/destroy/"+id) r.setWriteOptions(q) rtt, resp, err := requireOK(s.c.doRequest(r)) + if resp != nil { + defer resp.Body.Close() + } if err != nil { return nil, err } - resp.Body.Close() wm := &WriteMeta{RequestTime: rtt} return wm, nil @@ -129,10 +133,12 @@ func (s *Session) Renew(id string, q *WriteOptions) (*SessionEntry, *WriteMeta, r := s.c.newRequest("PUT", "/v1/session/renew/"+id) r.setWriteOptions(q) rtt, resp, err := requireOK(s.c.doRequest(r)) + if resp != nil { + defer resp.Body.Close() + } if err != nil { return nil, nil, err } - defer resp.Body.Close() wm := &WriteMeta{RequestTime: rtt} @@ -152,10 +158,12 @@ func (s *Session) Info(id string, q *QueryOptions) (*SessionEntry, *QueryMeta, e r := s.c.newRequest("GET", "/v1/session/info/"+id) r.setQueryOptions(q) rtt, resp, err := requireOK(s.c.doRequest(r)) + if resp != nil { + defer resp.Body.Close() + } if err != nil { return nil, nil, err } - defer resp.Body.Close() qm := &QueryMeta{} parseQueryMeta(resp, qm) @@ -177,10 +185,12 @@ func (s *Session) Node(node string, q *QueryOptions) ([]*SessionEntry, *QueryMet r := s.c.newRequest("GET", "/v1/session/node/"+node) r.setQueryOptions(q) rtt, resp, err := requireOK(s.c.doRequest(r)) + if resp != nil { + defer resp.Body.Close() + } if err != nil { return nil, nil, err } - defer resp.Body.Close() qm := &QueryMeta{} parseQueryMeta(resp, qm) @@ -198,10 +208,12 @@ func (s *Session) List(q *QueryOptions) ([]*SessionEntry, *QueryMeta, error) { r := s.c.newRequest("GET", "/v1/session/list") r.setQueryOptions(q) rtt, resp, err := requireOK(s.c.doRequest(r)) + if resp != nil { + defer resp.Body.Close() + } if err != nil { return nil, nil, err } - defer resp.Body.Close() qm := &QueryMeta{} parseQueryMeta(resp, qm) diff --git a/api/status.go b/api/status.go index 74ef61a678..16434a9847 100644 --- a/api/status.go +++ b/api/status.go @@ -14,10 +14,12 @@ func (c *Client) Status() *Status { func (s *Status) Leader() (string, error) { r := s.c.newRequest("GET", "/v1/status/leader") _, resp, err := requireOK(s.c.doRequest(r)) + if resp != nil { + defer resp.Body.Close() + } if err != nil { return "", err } - defer resp.Body.Close() var leader string if err := decodeBody(resp, &leader); err != nil { @@ -30,10 +32,12 @@ func (s *Status) Leader() (string, error) { func (s *Status) Peers() ([]string, error) { r := s.c.newRequest("GET", "/v1/status/peers") _, resp, err := requireOK(s.c.doRequest(r)) + if resp != nil { + defer resp.Body.Close() + } if err != nil { return nil, err } - defer resp.Body.Close() var peers []string if err := decodeBody(resp, &peers); err != nil { From f30237a92e1fd3625519b4ae574625724c0e12cc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?M=C3=A1rio=20Freitas?= Date: Mon, 19 Jan 2015 11:51:51 +0900 Subject: [PATCH 2/3] Revert "fixed: body not closed for non HTTP 200 responses" This reverts commit dafa61ad10d67d1992201f8499ef681e01de8a09. --- api/acl.go | 24 ++++++------------------ api/agent.go | 44 +++++++++++--------------------------------- api/catalog.go | 28 +++++++--------------------- api/event.go | 8 ++------ api/health.go | 16 ++++------------ api/kv.go | 8 ++------ api/session.go | 24 ++++++------------------ api/status.go | 8 ++------ 8 files changed, 40 insertions(+), 120 deletions(-) diff --git a/api/acl.go b/api/acl.go index dfb185bd2a..c3fb0d53aa 100644 --- a/api/acl.go +++ b/api/acl.go @@ -34,12 +34,10 @@ func (a *ACL) Create(acl *ACLEntry, q *WriteOptions) (string, *WriteMeta, error) r.setWriteOptions(q) r.obj = acl rtt, resp, err := requireOK(a.c.doRequest(r)) - if resp != nil { - defer resp.Body.Close() - } if err != nil { return "", nil, err } + defer resp.Body.Close() wm := &WriteMeta{RequestTime: rtt} var out struct{ ID string } @@ -55,12 +53,10 @@ func (a *ACL) Update(acl *ACLEntry, q *WriteOptions) (*WriteMeta, error) { r.setWriteOptions(q) r.obj = acl rtt, resp, err := requireOK(a.c.doRequest(r)) - if resp != nil { - defer resp.Body.Close() - } if err != nil { return nil, err } + defer resp.Body.Close() wm := &WriteMeta{RequestTime: rtt} return wm, nil @@ -71,12 +67,10 @@ func (a *ACL) Destroy(id string, q *WriteOptions) (*WriteMeta, error) { r := a.c.newRequest("PUT", "/v1/acl/destroy/"+id) r.setWriteOptions(q) rtt, resp, err := requireOK(a.c.doRequest(r)) - if resp != nil { - defer resp.Body.Close() - } if err != nil { return nil, err } + resp.Body.Close() wm := &WriteMeta{RequestTime: rtt} return wm, nil @@ -87,12 +81,10 @@ func (a *ACL) Clone(id string, q *WriteOptions) (string, *WriteMeta, error) { r := a.c.newRequest("PUT", "/v1/acl/clone/"+id) r.setWriteOptions(q) rtt, resp, err := requireOK(a.c.doRequest(r)) - if resp != nil { - defer resp.Body.Close() - } if err != nil { return "", nil, err } + defer resp.Body.Close() wm := &WriteMeta{RequestTime: rtt} var out struct{ ID string } @@ -107,12 +99,10 @@ func (a *ACL) Info(id string, q *QueryOptions) (*ACLEntry, *QueryMeta, error) { r := a.c.newRequest("GET", "/v1/acl/info/"+id) r.setQueryOptions(q) rtt, resp, err := requireOK(a.c.doRequest(r)) - if resp != nil { - defer resp.Body.Close() - } if err != nil { return nil, nil, err } + defer resp.Body.Close() qm := &QueryMeta{} parseQueryMeta(resp, qm) @@ -133,12 +123,10 @@ func (a *ACL) List(q *QueryOptions) ([]*ACLEntry, *QueryMeta, error) { r := a.c.newRequest("GET", "/v1/acl/list") r.setQueryOptions(q) rtt, resp, err := requireOK(a.c.doRequest(r)) - if resp != nil { - defer resp.Body.Close() - } if err != nil { return nil, nil, err } + defer resp.Body.Close() qm := &QueryMeta{} parseQueryMeta(resp, qm) diff --git a/api/agent.go b/api/agent.go index e200274a86..c31395e189 100644 --- a/api/agent.go +++ b/api/agent.go @@ -82,12 +82,10 @@ func (c *Client) Agent() *Agent { func (a *Agent) Self() (map[string]map[string]interface{}, error) { r := a.c.newRequest("GET", "/v1/agent/self") _, resp, err := requireOK(a.c.doRequest(r)) - if resp != nil { - defer resp.Body.Close() - } if err != nil { return nil, err } + defer resp.Body.Close() var out map[string]map[string]interface{} if err := decodeBody(resp, &out); err != nil { @@ -114,12 +112,10 @@ func (a *Agent) NodeName() (string, error) { func (a *Agent) Checks() (map[string]*AgentCheck, error) { r := a.c.newRequest("GET", "/v1/agent/checks") _, resp, err := requireOK(a.c.doRequest(r)) - if resp != nil { - defer resp.Body.Close() - } if err != nil { return nil, err } + defer resp.Body.Close() var out map[string]*AgentCheck if err := decodeBody(resp, &out); err != nil { @@ -132,12 +128,10 @@ func (a *Agent) Checks() (map[string]*AgentCheck, error) { func (a *Agent) Services() (map[string]*AgentService, error) { r := a.c.newRequest("GET", "/v1/agent/services") _, resp, err := requireOK(a.c.doRequest(r)) - if resp != nil { - defer resp.Body.Close() - } if err != nil { return nil, err } + defer resp.Body.Close() var out map[string]*AgentService if err := decodeBody(resp, &out); err != nil { @@ -154,12 +148,10 @@ func (a *Agent) Members(wan bool) ([]*AgentMember, error) { r.params.Set("wan", "1") } _, resp, err := requireOK(a.c.doRequest(r)) - if resp != nil { - defer resp.Body.Close() - } if err != nil { return nil, err } + defer resp.Body.Close() var out []*AgentMember if err := decodeBody(resp, &out); err != nil { @@ -174,12 +166,10 @@ func (a *Agent) ServiceRegister(service *AgentServiceRegistration) error { r := a.c.newRequest("PUT", "/v1/agent/service/register") r.obj = service _, resp, err := requireOK(a.c.doRequest(r)) - if resp != nil { - defer resp.Body.Close() - } if err != nil { return err } + resp.Body.Close() return nil } @@ -188,12 +178,10 @@ func (a *Agent) ServiceRegister(service *AgentServiceRegistration) error { func (a *Agent) ServiceDeregister(serviceID string) error { r := a.c.newRequest("PUT", "/v1/agent/service/deregister/"+serviceID) _, resp, err := requireOK(a.c.doRequest(r)) - if resp != nil { - defer resp.Body.Close() - } if err != nil { return err } + resp.Body.Close() return nil } @@ -225,12 +213,10 @@ func (a *Agent) UpdateTTL(checkID, note, status string) error { r := a.c.newRequest("PUT", endpoint) r.params.Set("note", note) _, resp, err := requireOK(a.c.doRequest(r)) - if resp != nil { - defer resp.Body.Close() - } if err != nil { return err } + resp.Body.Close() return nil } @@ -240,12 +226,10 @@ func (a *Agent) CheckRegister(check *AgentCheckRegistration) error { r := a.c.newRequest("PUT", "/v1/agent/check/register") r.obj = check _, resp, err := requireOK(a.c.doRequest(r)) - if resp != nil { - defer resp.Body.Close() - } if err != nil { return err } + resp.Body.Close() return nil } @@ -254,12 +238,10 @@ func (a *Agent) CheckRegister(check *AgentCheckRegistration) error { func (a *Agent) CheckDeregister(checkID string) error { r := a.c.newRequest("PUT", "/v1/agent/check/deregister/"+checkID) _, resp, err := requireOK(a.c.doRequest(r)) - if resp != nil { - defer resp.Body.Close() - } if err != nil { return err } + resp.Body.Close() return nil } @@ -271,12 +253,10 @@ func (a *Agent) Join(addr string, wan bool) error { r.params.Set("wan", "1") } _, resp, err := requireOK(a.c.doRequest(r)) - if resp != nil { - defer resp.Body.Close() - } if err != nil { return err } + resp.Body.Close() return nil } @@ -284,11 +264,9 @@ func (a *Agent) Join(addr string, wan bool) error { func (a *Agent) ForceLeave(node string) error { r := a.c.newRequest("PUT", "/v1/agent/force-leave/"+node) _, resp, err := requireOK(a.c.doRequest(r)) - if resp != nil { - defer resp.Body.Close() - } if err != nil { return err } + resp.Body.Close() return nil } diff --git a/api/catalog.go b/api/catalog.go index 5336c87d4f..fee1695677 100644 --- a/api/catalog.go +++ b/api/catalog.go @@ -50,12 +50,10 @@ func (c *Catalog) Register(reg *CatalogRegistration, q *WriteOptions) (*WriteMet r.setWriteOptions(q) r.obj = reg rtt, resp, err := requireOK(c.c.doRequest(r)) - if resp != nil { - defer resp.Body.Close() - } if err != nil { return nil, err } + resp.Body.Close() wm := &WriteMeta{} wm.RequestTime = rtt @@ -68,12 +66,10 @@ func (c *Catalog) Deregister(dereg *CatalogDeregistration, q *WriteOptions) (*Wr r.setWriteOptions(q) r.obj = dereg rtt, resp, err := requireOK(c.c.doRequest(r)) - if resp != nil { - defer resp.Body.Close() - } if err != nil { return nil, err } + resp.Body.Close() wm := &WriteMeta{} wm.RequestTime = rtt @@ -85,12 +81,10 @@ func (c *Catalog) Deregister(dereg *CatalogDeregistration, q *WriteOptions) (*Wr func (c *Catalog) Datacenters() ([]string, error) { r := c.c.newRequest("GET", "/v1/catalog/datacenters") _, resp, err := requireOK(c.c.doRequest(r)) - if resp != nil { - defer resp.Body.Close() - } if err != nil { return nil, err } + defer resp.Body.Close() var out []string if err := decodeBody(resp, &out); err != nil { @@ -104,12 +98,10 @@ func (c *Catalog) Nodes(q *QueryOptions) ([]*Node, *QueryMeta, error) { r := c.c.newRequest("GET", "/v1/catalog/nodes") r.setQueryOptions(q) rtt, resp, err := requireOK(c.c.doRequest(r)) - if resp != nil { - defer resp.Body.Close() - } if err != nil { return nil, nil, err } + defer resp.Body.Close() qm := &QueryMeta{} parseQueryMeta(resp, qm) @@ -127,12 +119,10 @@ func (c *Catalog) Services(q *QueryOptions) (map[string][]string, *QueryMeta, er r := c.c.newRequest("GET", "/v1/catalog/services") r.setQueryOptions(q) rtt, resp, err := requireOK(c.c.doRequest(r)) - if resp != nil { - defer resp.Body.Close() - } if err != nil { return nil, nil, err } + defer resp.Body.Close() qm := &QueryMeta{} parseQueryMeta(resp, qm) @@ -153,12 +143,10 @@ func (c *Catalog) Service(service, tag string, q *QueryOptions) ([]*CatalogServi r.params.Set("tag", tag) } rtt, resp, err := requireOK(c.c.doRequest(r)) - if resp != nil { - defer resp.Body.Close() - } if err != nil { return nil, nil, err } + defer resp.Body.Close() qm := &QueryMeta{} parseQueryMeta(resp, qm) @@ -176,12 +164,10 @@ func (c *Catalog) Node(node string, q *QueryOptions) (*CatalogNode, *QueryMeta, r := c.c.newRequest("GET", "/v1/catalog/node/"+node) r.setQueryOptions(q) rtt, resp, err := requireOK(c.c.doRequest(r)) - if resp != nil { - defer resp.Body.Close() - } if err != nil { return nil, nil, err } + defer resp.Body.Close() qm := &QueryMeta{} parseQueryMeta(resp, qm) diff --git a/api/event.go b/api/event.go index 175c1a23d0..85b5b069b0 100644 --- a/api/event.go +++ b/api/event.go @@ -47,12 +47,10 @@ func (e *Event) Fire(params *UserEvent, q *WriteOptions) (string, *WriteMeta, er } rtt, resp, err := requireOK(e.c.doRequest(r)) - if resp != nil { - defer resp.Body.Close() - } if err != nil { return "", nil, err } + defer resp.Body.Close() wm := &WriteMeta{RequestTime: rtt} var out UserEvent @@ -73,12 +71,10 @@ func (e *Event) List(name string, q *QueryOptions) ([]*UserEvent, *QueryMeta, er r.params.Set("name", name) } rtt, resp, err := requireOK(e.c.doRequest(r)) - if resp != nil { - defer resp.Body.Close() - } if err != nil { return nil, nil, err } + defer resp.Body.Close() qm := &QueryMeta{} parseQueryMeta(resp, qm) diff --git a/api/health.go b/api/health.go index cf6889028a..02b161e28e 100644 --- a/api/health.go +++ b/api/health.go @@ -38,12 +38,10 @@ func (h *Health) Node(node string, q *QueryOptions) ([]*HealthCheck, *QueryMeta, r := h.c.newRequest("GET", "/v1/health/node/"+node) r.setQueryOptions(q) rtt, resp, err := requireOK(h.c.doRequest(r)) - if resp != nil { - defer resp.Body.Close() - } if err != nil { return nil, nil, err } + defer resp.Body.Close() qm := &QueryMeta{} parseQueryMeta(resp, qm) @@ -61,12 +59,10 @@ func (h *Health) Checks(service string, q *QueryOptions) ([]*HealthCheck, *Query r := h.c.newRequest("GET", "/v1/health/checks/"+service) r.setQueryOptions(q) rtt, resp, err := requireOK(h.c.doRequest(r)) - if resp != nil { - defer resp.Body.Close() - } if err != nil { return nil, nil, err } + defer resp.Body.Close() qm := &QueryMeta{} parseQueryMeta(resp, qm) @@ -92,12 +88,10 @@ func (h *Health) Service(service, tag string, passingOnly bool, q *QueryOptions) r.params.Set("passing", "1") } rtt, resp, err := requireOK(h.c.doRequest(r)) - if resp != nil { - defer resp.Body.Close() - } if err != nil { return nil, nil, err } + defer resp.Body.Close() qm := &QueryMeta{} parseQueryMeta(resp, qm) @@ -125,12 +119,10 @@ func (h *Health) State(state string, q *QueryOptions) ([]*HealthCheck, *QueryMet r := h.c.newRequest("GET", "/v1/health/state/"+state) r.setQueryOptions(q) rtt, resp, err := requireOK(h.c.doRequest(r)) - if resp != nil { - defer resp.Body.Close() - } if err != nil { return nil, nil, err } + defer resp.Body.Close() qm := &QueryMeta{} parseQueryMeta(resp, qm) diff --git a/api/kv.go b/api/kv.go index 1da6c5a3c8..ba74057fcc 100644 --- a/api/kv.go +++ b/api/kv.go @@ -175,12 +175,10 @@ func (k *KV) put(key string, params map[string]string, body []byte, q *WriteOpti } r.body = bytes.NewReader(body) rtt, resp, err := requireOK(k.c.doRequest(r)) - if resp != nil { - defer resp.Body.Close() - } if err != nil { return false, nil, err } + defer resp.Body.Close() qm := &WriteMeta{} qm.RequestTime = rtt @@ -221,12 +219,10 @@ func (k *KV) deleteInternal(key string, params map[string]string, q *WriteOption r.params.Set(param, val) } rtt, resp, err := requireOK(k.c.doRequest(r)) - if resp != nil { - defer resp.Body.Close() - } if err != nil { return false, nil, err } + defer resp.Body.Close() qm := &WriteMeta{} qm.RequestTime = rtt diff --git a/api/session.go b/api/session.go index e840df1c5e..e889bbe0de 100644 --- a/api/session.go +++ b/api/session.go @@ -97,12 +97,10 @@ func (s *Session) create(obj interface{}, q *WriteOptions) (string, *WriteMeta, r.setWriteOptions(q) r.obj = obj rtt, resp, err := requireOK(s.c.doRequest(r)) - if resp != nil { - defer resp.Body.Close() - } if err != nil { return "", nil, err } + defer resp.Body.Close() wm := &WriteMeta{RequestTime: rtt} var out struct{ ID string } @@ -117,12 +115,10 @@ func (s *Session) Destroy(id string, q *WriteOptions) (*WriteMeta, error) { r := s.c.newRequest("PUT", "/v1/session/destroy/"+id) r.setWriteOptions(q) rtt, resp, err := requireOK(s.c.doRequest(r)) - if resp != nil { - defer resp.Body.Close() - } if err != nil { return nil, err } + resp.Body.Close() wm := &WriteMeta{RequestTime: rtt} return wm, nil @@ -133,12 +129,10 @@ func (s *Session) Renew(id string, q *WriteOptions) (*SessionEntry, *WriteMeta, r := s.c.newRequest("PUT", "/v1/session/renew/"+id) r.setWriteOptions(q) rtt, resp, err := requireOK(s.c.doRequest(r)) - if resp != nil { - defer resp.Body.Close() - } if err != nil { return nil, nil, err } + defer resp.Body.Close() wm := &WriteMeta{RequestTime: rtt} @@ -158,12 +152,10 @@ func (s *Session) Info(id string, q *QueryOptions) (*SessionEntry, *QueryMeta, e r := s.c.newRequest("GET", "/v1/session/info/"+id) r.setQueryOptions(q) rtt, resp, err := requireOK(s.c.doRequest(r)) - if resp != nil { - defer resp.Body.Close() - } if err != nil { return nil, nil, err } + defer resp.Body.Close() qm := &QueryMeta{} parseQueryMeta(resp, qm) @@ -185,12 +177,10 @@ func (s *Session) Node(node string, q *QueryOptions) ([]*SessionEntry, *QueryMet r := s.c.newRequest("GET", "/v1/session/node/"+node) r.setQueryOptions(q) rtt, resp, err := requireOK(s.c.doRequest(r)) - if resp != nil { - defer resp.Body.Close() - } if err != nil { return nil, nil, err } + defer resp.Body.Close() qm := &QueryMeta{} parseQueryMeta(resp, qm) @@ -208,12 +198,10 @@ func (s *Session) List(q *QueryOptions) ([]*SessionEntry, *QueryMeta, error) { r := s.c.newRequest("GET", "/v1/session/list") r.setQueryOptions(q) rtt, resp, err := requireOK(s.c.doRequest(r)) - if resp != nil { - defer resp.Body.Close() - } if err != nil { return nil, nil, err } + defer resp.Body.Close() qm := &QueryMeta{} parseQueryMeta(resp, qm) diff --git a/api/status.go b/api/status.go index 16434a9847..74ef61a678 100644 --- a/api/status.go +++ b/api/status.go @@ -14,12 +14,10 @@ func (c *Client) Status() *Status { func (s *Status) Leader() (string, error) { r := s.c.newRequest("GET", "/v1/status/leader") _, resp, err := requireOK(s.c.doRequest(r)) - if resp != nil { - defer resp.Body.Close() - } if err != nil { return "", err } + defer resp.Body.Close() var leader string if err := decodeBody(resp, &leader); err != nil { @@ -32,12 +30,10 @@ func (s *Status) Leader() (string, error) { func (s *Status) Peers() ([]string, error) { r := s.c.newRequest("GET", "/v1/status/peers") _, resp, err := requireOK(s.c.doRequest(r)) - if resp != nil { - defer resp.Body.Close() - } if err != nil { return nil, err } + defer resp.Body.Close() var peers []string if err := decodeBody(resp, &peers); err != nil { From f45105383c45590b6ca5e0961a0cd1f48affc8b7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?M=C3=A1rio=20Freitas?= Date: Mon, 19 Jan 2015 11:53:21 +0900 Subject: [PATCH 3/3] fixed: requireOK should close Body on error --- api/api.go | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/api/api.go b/api/api.go index 5617293e44..a7753574a5 100644 --- a/api/api.go +++ b/api/api.go @@ -339,12 +339,16 @@ func encodeBody(obj interface{}) (io.Reader, error) { // requireOK is used to wrap doRequest and check for a 200 func requireOK(d time.Duration, resp *http.Response, e error) (time.Duration, *http.Response, error) { if e != nil { - return d, resp, e + if resp != nil { + resp.Body.Close() + } + return d, nil, e } if resp.StatusCode != 200 { var buf bytes.Buffer io.Copy(&buf, resp.Body) - return d, resp, fmt.Errorf("Unexpected response code: %d (%s)", resp.StatusCode, buf.Bytes()) + resp.Body.Close() + return d, nil, fmt.Errorf("Unexpected response code: %d (%s)", resp.StatusCode, buf.Bytes()) } - return d, resp, e + return d, resp, nil }