From 16f59454392b5fe50bbb96eaf069c15b4800af3c Mon Sep 17 00:00:00 2001 From: Pierre Wacrenier Date: Thu, 5 Feb 2015 01:08:18 +0100 Subject: [PATCH] Fix race condition on exec Signed-off-by: Pierre Wacrenier --- api/api.go | 11 ++++++++--- api/utils.go | 10 +++++++++- 2 files changed, 17 insertions(+), 4 deletions(-) diff --git a/api/api.go b/api/api.go index 9e506368c9..c329e06557 100644 --- a/api/api.go +++ b/api/api.go @@ -279,12 +279,17 @@ func proxyContainerAndForceRefresh(c *context, w http.ResponseWriter, r *http.Re return } - if err := proxy(c.tlsConfig, container.Node.Addr, w, r); err != nil { + cb := func(resp *http.Response) { + if resp.StatusCode == http.StatusCreated { + log.Debugf("[REFRESH CONTAINER] --> %s", container.Id) + container.Node.RefreshContainer(container.Id, true) + } + } + + if err := proxyAsync(c.tlsConfig, container.Node.Addr, w, r, cb); err != nil { httpError(w, err.Error(), http.StatusInternalServerError) } - log.Debugf("[REFRESH CONTAINER] --> %s", container.Id) - container.Node.RefreshContainer(container.Id, true) } // Proxy a request to the right node diff --git a/api/utils.go b/api/utils.go index 3455335dbb..546592619c 100644 --- a/api/utils.go +++ b/api/utils.go @@ -50,7 +50,7 @@ func copyHeader(dst, src http.Header) { } } -func proxy(tlsConfig *tls.Config, addr string, w http.ResponseWriter, r *http.Request) error { +func proxyAsync(tlsConfig *tls.Config, addr string, w http.ResponseWriter, r *http.Request, callback func(*http.Response)) error { // Use a new client for each request client, scheme := newClientAndScheme(tlsConfig) // RequestURI may not be sent to client @@ -65,6 +65,10 @@ func proxy(tlsConfig *tls.Config, addr string, w http.ResponseWriter, r *http.Re return err } + if callback != nil { + callback(resp) + } + copyHeader(w.Header(), resp.Header) w.WriteHeader(resp.StatusCode) io.Copy(NewWriteFlusher(w), resp.Body) @@ -73,6 +77,10 @@ func proxy(tlsConfig *tls.Config, addr string, w http.ResponseWriter, r *http.Re return nil } +func proxy(tlsConfig *tls.Config, addr string, w http.ResponseWriter, r *http.Request) error { + return proxyAsync(tlsConfig, addr, w, r, nil) +} + func hijack(tlsConfig *tls.Config, addr string, w http.ResponseWriter, r *http.Request) error { if parts := strings.SplitN(addr, "://", 2); len(parts) == 2 { addr = parts[1]