X-Git-Url: https://git.arvados.org/arvados.git/blobdiff_plain/4529d84afb3549ccb4ae9005a8f64f558c2bbe5c..db118cca358662e57a3dd0c1186dce1f0a62ca52:/lib/controller/handler.go?ds=sidebyside diff --git a/lib/controller/handler.go b/lib/controller/handler.go index 4c6fca7f77..5d6bd9570c 100644 --- a/lib/controller/handler.go +++ b/lib/controller/handler.go @@ -38,6 +38,7 @@ type Handler struct { secureClient *http.Client insecureClient *http.Client dbConnector ctrlctx.DBConnector + limitLogCreate chan struct{} } func (h *Handler) ServeHTTP(w http.ResponseWriter, req *http.Request) { @@ -134,6 +135,7 @@ func (h *Handler) setup() { hs := http.NotFoundHandler() hs = prepend(hs, h.proxyRailsAPI) + hs = prepend(hs, h.limitLogCreateRequests) hs = h.setupProxyRemoteCluster(hs) hs = prepend(hs, oidcAuthorizer.Middleware) mux.Handle("/", hs) @@ -147,6 +149,12 @@ func (h *Handler) setup() { ic.CheckRedirect = neverRedirect h.insecureClient = &ic + logCreateLimit := int(float64(h.Cluster.API.MaxConcurrentRequests) * h.Cluster.API.LogCreateRequestFraction) + if logCreateLimit == 0 && h.Cluster.API.LogCreateRequestFraction > 0 { + logCreateLimit = 1 + } + h.limitLogCreate = make(chan struct{}, logCreateLimit) + h.proxy = &proxy{ Name: "arvados-controller", } @@ -182,6 +190,20 @@ func (h *Handler) localClusterRequest(req *http.Request) (*http.Response, error) return h.proxy.Do(req, urlOut, client) } +func (h *Handler) limitLogCreateRequests(w http.ResponseWriter, req *http.Request, next http.Handler) { + if cap(h.limitLogCreate) > 0 && req.Method == http.MethodPost && strings.HasPrefix(req.URL.Path, "/arvados/v1/logs") { + select { + case h.limitLogCreate <- struct{}{}: + defer func() { <-h.limitLogCreate }() + next.ServeHTTP(w, req) + default: + http.Error(w, "Excess log messages", http.StatusServiceUnavailable) + } + return + } + next.ServeHTTP(w, req) +} + func (h *Handler) proxyRailsAPI(w http.ResponseWriter, req *http.Request, next http.Handler) { resp, err := h.localClusterRequest(req) n, err := h.proxy.ForwardResponse(w, resp, err)