From a99c6c4cbe9f8bcbe674e5d9960312fe39c47d16 Mon Sep 17 00:00:00 2001 From: Anthony Lapenna Date: Tue, 28 Mar 2017 14:28:17 +0200 Subject: [PATCH] fix(backend): use a thread-safe implementation of map for proxies (#728) --- api/http/docker_handler.go | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/api/http/docker_handler.go b/api/http/docker_handler.go index 5a8668a3a..cb89d0093 100644 --- a/api/http/docker_handler.go +++ b/api/http/docker_handler.go @@ -11,6 +11,7 @@ import ( "os" "github.com/gorilla/mux" + "github.com/orcaman/concurrent-map" ) // DockerHandler represents an HTTP API handler for proxying requests to the Docker API. @@ -19,7 +20,7 @@ type DockerHandler struct { Logger *log.Logger EndpointService portainer.EndpointService ProxyFactory ProxyFactory - proxies map[portainer.EndpointID]http.Handler + proxies cmap.ConcurrentMap } // NewDockerHandler returns a new instance of DockerHandler. @@ -30,7 +31,7 @@ func NewDockerHandler(mw *middleWareService, resourceControlService portainer.Re ProxyFactory: ProxyFactory{ ResourceControlService: resourceControlService, }, - proxies: make(map[portainer.EndpointID]http.Handler), + proxies: cmap.New(), } h.PathPrefix("/{id}/").Handler( mw.authenticated(http.HandlerFunc(h.proxyRequestsToDockerAPI))) @@ -72,13 +73,16 @@ func (handler *DockerHandler) proxyRequestsToDockerAPI(w http.ResponseWriter, r return } - proxy := handler.proxies[endpointID] - if proxy == nil { + var proxy http.Handler + item, ok := handler.proxies.Get(string(endpointID)) + if !ok { proxy, err = handler.createAndRegisterEndpointProxy(endpoint) if err != nil { Error(w, err, http.StatusBadRequest, handler.Logger) return } + } else { + proxy = item.(http.Handler) } http.StripPrefix("/"+id, proxy).ServeHTTP(w, r) } @@ -105,6 +109,6 @@ func (handler *DockerHandler) createAndRegisterEndpointProxy(endpoint *portainer proxy = handler.ProxyFactory.newSocketProxy(endpointURL.Path) } - handler.proxies[endpoint.ID] = proxy + handler.proxies.Set(string(endpoint.ID), proxy) return proxy, nil }