20200: Fix test typo
[arvados.git] / lib / controller / handler.go
index 4c6fca7f77276c3981c591d18429d8520d3e76b7..5d6bd9570c813e0f2eb7c721d0b5e02648fb452d 100644 (file)
@@ -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)