Skip to content
This repository was archived by the owner on Nov 8, 2022. It is now read-only.

Commit 7b23de9

Browse files
committed
Fixes #1324: Properly closes connections in tribe
Properly closes some http.Respons's that were not previously closed.
1 parent d9b23fa commit 7b23de9

2 files changed

Lines changed: 57 additions & 41 deletions

File tree

mgmt/rest/client/client.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,7 @@ func (c *Client) do(method, path string, ct contentType, body ...[]byte) (*rbody
172172
}
173173
return nil, fmt.Errorf("URL target is not available. %v", err)
174174
}
175+
defer rsp.Body.Close()
175176
case "PUT":
176177
var b *bytes.Reader
177178
if len(body) == 0 {
@@ -193,6 +194,7 @@ func (c *Client) do(method, path string, ct contentType, body ...[]byte) (*rbody
193194
}
194195
return nil, fmt.Errorf("URL target is not available. %v", err)
195196
}
197+
defer rsp.Body.Close()
196198
case "DELETE":
197199
var b *bytes.Reader
198200
if len(body) == 0 {
@@ -214,6 +216,7 @@ func (c *Client) do(method, path string, ct contentType, body ...[]byte) (*rbody
214216
}
215217
return nil, fmt.Errorf("URL target is not available. %v", err)
216218
}
219+
defer rsp.Body.Close()
217220
case "POST":
218221
var b *bytes.Reader
219222
if len(body) == 0 {
@@ -234,6 +237,7 @@ func (c *Client) do(method, path string, ct contentType, body ...[]byte) (*rbody
234237
}
235238
return nil, fmt.Errorf("URL target is not available. %v", err)
236239
}
240+
defer rsp.Body.Close()
237241
}
238242

239243
return httpRespToAPIResp(rsp)

mgmt/tribe/worker/worker.go

Lines changed: 53 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -324,55 +324,67 @@ func (w worker) loadPlugin(plugin core.Plugin) error {
324324
}).Info("unable to create client")
325325
continue
326326
}
327-
resp, err := c.TribeRequest()
327+
f, err := w.downloadPlugin(c, plugin)
328+
// If we can't download from this member, try the next
328329
if err != nil {
329-
logger.WithFields(log.Fields{
330-
"err": err,
331-
"url": url,
332-
}).Info("plugin not found")
330+
logger.Error(err)
333331
continue
334332
}
335-
if resp.StatusCode == 200 {
336-
if resp.Header.Get("Content-Type") != "application/x-gzip" {
337-
logger.WithField("content-type", resp.Header.Get("Content-Type")).Error("Expected application/x-gzip")
338-
}
339-
dir, err := ioutil.TempDir("", "")
340-
if err != nil {
341-
logger.Error(err)
342-
return err
343-
}
344-
f, err := os.Create(path.Join(dir, fmt.Sprintf("%s-%s-%d", plugin.TypeName(), plugin.Name(), plugin.Version())))
345-
if err != nil {
346-
logger.Error(err)
347-
f.Close()
348-
return err
349-
}
350-
io.Copy(f, resp.Body)
351-
f.Close()
352-
err = os.Chmod(f.Name(), 0700)
353-
if err != nil {
354-
logger.Error(err)
355-
return err
356-
}
357-
rp, err := core.NewRequestedPlugin(f.Name())
358-
if err != nil {
359-
logger.Error(err)
360-
return err
361-
}
362-
_, err = w.pluginManager.Load(rp)
363-
if err != nil {
364-
logger.Error(err)
365-
return err
366-
}
367-
if w.isPluginLoaded(plugin.Name(), plugin.TypeName(), plugin.Version()) {
368-
return nil
369-
}
370-
return errors.New("failed to load plugin")
333+
rp, err := core.NewRequestedPlugin(f.Name())
334+
if err != nil {
335+
logger.Error(err)
336+
return err
337+
}
338+
_, err = w.pluginManager.Load(rp)
339+
if err != nil {
340+
logger.Error(err)
341+
return err
342+
}
343+
if w.isPluginLoaded(plugin.Name(), plugin.TypeName(), plugin.Version()) {
344+
return nil
371345
}
346+
return errors.New("failed to load plugin")
372347
}
373348
return errors.New("failed to find a member with the plugin")
374349
}
375350

351+
func (w worker) downloadPlugin(c *client.Client, plugin core.Plugin) (*os.File, error) {
352+
logger := w.logger.WithFields(log.Fields{
353+
"plugin-name": plugin.Name(),
354+
"plugin-version": plugin.Version(),
355+
"plugin-type": plugin.TypeName(),
356+
"url": c.URL,
357+
"_block": "download-plugin",
358+
})
359+
resp, err := c.TribeRequest()
360+
if err != nil {
361+
logger.WithFields(log.Fields{
362+
"err": err,
363+
}).Info("plugin not found")
364+
return nil, fmt.Errorf("Plugin not found at %s: %s", c.URL, err.Error())
365+
}
366+
defer resp.Body.Close()
367+
if resp.StatusCode == 200 {
368+
if resp.Header.Get("Content-Type") != "application/x-gzip" {
369+
logger.WithField("content-type", resp.Header.Get("Content-Type")).Error("Expected application/x-gzip")
370+
}
371+
dir, err := ioutil.TempDir("", "")
372+
if err != nil {
373+
logger.Error(err)
374+
return nil, err
375+
}
376+
fpath := path.Join(dir, fmt.Sprintf("%s-%s-%d", plugin.TypeName(), plugin.Name(), plugin.Version()))
377+
f, err := os.OpenFile(fpath, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0700)
378+
if err != nil {
379+
logger.Error(err)
380+
return nil, err
381+
}
382+
io.Copy(f, resp.Body)
383+
f.Close()
384+
return f, nil
385+
}
386+
return nil, fmt.Errorf("Status code not 200 was %v: %s", resp.StatusCode, c.URL)
387+
}
376388
func (w worker) createTask(taskID string, startOnCreate bool) {
377389
logger := w.logger.WithFields(log.Fields{
378390
"task-id": taskID,

0 commit comments

Comments
 (0)