rob05c commented on a change in pull request #6017:
URL: https://github.com/apache/trafficcontrol/pull/6017#discussion_r669047436



##########
File path: traffic_ops/traffic_ops_golang/routing/middleware/readwhilewriter.go
##########
@@ -0,0 +1,197 @@
+package middleware
+
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+import (
+       "net/http"
+       "strconv"
+       "sync"
+
+       "github.com/apache/trafficcontrol/lib/go-log"
+       "github.com/apache/trafficcontrol/lib/go-tc"
+       "github.com/apache/trafficcontrol/lib/go-util"
+       "github.com/apache/trafficcontrol/traffic_ops/traffic_ops_golang/auth"
+)
+
+// ReadWhileWriterWrapper blocks multiple requests for the same object,
+// and makes a single request to the real handler (which is probably 
expensive, e.g. database calls),
+// and returns the result to all callers.
+func ReadWhileWriterWrapper() Middleware {
+       rwr := NewRWR()
+       return func(h http.HandlerFunc) http.HandlerFunc {
+               return WrapReadWhileWriter(h, rwr)
+       }
+}
+
+func WrapReadWhileWriter(h http.HandlerFunc, rwr *RWR) http.HandlerFunc {
+       return func(w http.ResponseWriter, r *http.Request) {
+               log.Infoln("ReadWhileWriter starting")
+               if r.Method != http.MethodGet {
+                       // only GETs use RWR. TODO: determine if we should rwr 
HEAD
+                       h(w, r)
+                       return
+               }
+               user, err := auth.GetCurrentUser(r.Context())
+               if err != nil {
+                       h(w, r) // not our job to error, pass the request along
+                       return
+               }
+               // Note no error doesn't mean a valid user, there may be no 
user (which is allowed for some endpoints).
+               // In that case, user.TenantID will be TenantIDInvalid.
+               // Which is fine, we'll use that as a cache key like any valid 
tenant,
+               // and all unauthenticated requests will share requests.
+
+               // TODO: add "allowed to request no-cache" as a Tenant Role 
Permission
+               if user.TenantID != auth.TenantIDInvalid && requestNoCache(r) {
+                       h(w, r)
+                       return
+               }
+
+               cacheKey := makeRWRCacheKey(r, user)
+
+               multiRequestWrite(h, w, r, rwr, cacheKey)
+       }
+}
+
+type ReqObj struct {
+       Body    []byte
+       Code    int
+       Headers http.Header
+       // TODO add headers

Review comment:
       Yes, just missed removing the comment. Done.




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


Reply via email to