Revert "fixed: body not closed for non HTTP 200 responses"

This reverts commit dafa61ad10d67d1992201f8499ef681e01de8a09.
This commit is contained in:
Mário Freitas 2015-01-19 11:51:51 +09:00
parent 18c4d6ea9e
commit 2db0a65a74
8 changed files with 40 additions and 120 deletions

View File

@ -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)

View File

@ -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
}

View File

@ -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)

View File

@ -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)

View File

@ -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)

View File

@ -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

View File

@ -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)

View File

@ -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 {