wilfred-s commented on code in PR #757:
URL: https://github.com/apache/yunikorn-core/pull/757#discussion_r1531490304


##########
pkg/webservice/handlers.go:
##########
@@ -1216,3 +1227,40 @@ func getStream(w http.ResponseWriter, r *http.Request) {
                }
        }
 }
+
+func checkHeader(h http.Header, key string, value string) bool {

Review Comment:
   That will cause a leak as the gzip writer must be closed for it not to leak. 
Calling close on the http.ResponseWriter is not possible so we need more. 
Probably the easiest solution is to use the same solution as we have for the 
loggingHandler(). We wrap the compression choice in a handler function, which 
then gets wrapped in the logging handler. That means we have it all in one 
place and expand on it with compressor pooling or other things in the future.
   
   Example code, which is not complete but gives some idea on how we can close 
the compressor. That can be expanded to use a sync.Pool to not recreate the zip 
writer each time and just reset it before use.
   ```
   type gzipResponseWriter struct {
        io.Writer
        http.ResponseWriter
   }
   
   func (w gzipResponseWriter) Write(b []byte) (int, error) {
        return w.Writer.Write(b)
   }
   
   func makeGzipHandler(fn http.HandlerFunc) http.HandlerFunc {
        return func(w http.ResponseWriter, r *http.Request) {
                if !strings.Contains(r.Header.Get("Accept-Encoding"), "gzip") {
                        fn(w, r)
                        return
                }
                w.Header().Set("Content-Encoding", "gzip")
                   w.Header().Del("Content-Length")
                gz := gzip.NewWriter(w)
                defer gz.Close()
                gzr := gzipResponseWriter{Writer: gz, ResponseWriter: w}
                fn(gzr, r)
        }
   }
   ```



-- 
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