@@ -164,7 +164,7 @@ func (c *InternalClient) CreateIndex(ctx context.Context, index string, opt pilo
164
164
}
165
165
return err
166
166
}
167
- return nil
167
+ return errors . Wrap ( resp . Body . Close (), "closing response body" )
168
168
}
169
169
170
170
// FragmentNodes returns a list of nodes that own a shard.
@@ -705,6 +705,9 @@ func (c *InternalClient) exportNodeCSV(ctx context.Context, node *pilosa.Node, i
705
705
return nil
706
706
}
707
707
708
+ // RetrieveShardFromURI returns a ReadCloser which contains the data of the
709
+ // specified shard from the specified node. Caller *must* close the returned
710
+ // ReadCloser or risk leaking goroutines/tcp connections.
708
711
func (c * InternalClient ) RetrieveShardFromURI (ctx context.Context , index , field , view string , shard uint64 , uri pilosa.URI ) (io.ReadCloser , error ) {
709
712
span , ctx := tracing .StartSpanFromContext (ctx , "InternalClient.RetrieveShardFromURI" )
710
713
defer span .Finish ()
@@ -800,7 +803,7 @@ func (c *InternalClient) CreateFieldWithOptions(ctx context.Context, index, fiel
800
803
return err
801
804
}
802
805
803
- return nil
806
+ return errors . Wrap ( resp . Body . Close (), "closing response body" )
804
807
}
805
808
806
809
// FragmentBlocks returns a list of block checksums for a fragment on a host.
@@ -994,15 +997,24 @@ func (c *InternalClient) SendMessage(ctx context.Context, uri *pilosa.URI, msg [
994
997
req .Header .Set ("Accept" , "application/json" )
995
998
996
999
// Execute request.
997
- _ , err = c .executeRequest (req .WithContext (ctx ))
998
- return err
1000
+ resp , err := c .executeRequest (req .WithContext (ctx ))
1001
+ if err != nil {
1002
+ return errors .Wrap (err , "executing request" )
1003
+ }
1004
+ return errors .Wrap (resp .Body .Close (), "closing response body" )
999
1005
}
1000
1006
1001
- // executeRequest executes the given request and checks the Response
1007
+ // executeRequest executes the given request and checks the Response. For
1008
+ // responses with non-2XX status, the body is read and closed, and an error is
1009
+ // returned. If the error is nil, the caller must ensure that the response body
1010
+ // is closed.
1002
1011
func (c * InternalClient ) executeRequest (req * http.Request ) (* http.Response , error ) {
1003
1012
tracing .GlobalTracer .InjectHTTPHeaders (req )
1004
1013
resp , err := c .httpClient .Do (req )
1005
1014
if err != nil {
1015
+ if resp != nil {
1016
+ resp .Body .Close ()
1017
+ }
1006
1018
return nil , errors .Wrap (err , "executing request" )
1007
1019
}
1008
1020
if resp .StatusCode < 200 || resp .StatusCode >= 300 {
0 commit comments