-
Couldn't load subscription status.
- Fork 256
[YUNIKORN-2249] Add compression option to getQueueApplication API #757
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from 17 commits
9ef7968
85dd3de
b5e2651
e7ea5bc
7238626
ce40af6
995cb97
4cb1d9c
40b9649
4f57568
4e7228a
2205902
b811993
8a6f9cf
53c8827
1c4a0e1
41c0570
ca18e79
0a6c931
3f806ba
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -19,9 +19,12 @@ | |
| package webservice | ||
|
|
||
| import ( | ||
| "compress/gzip" | ||
| "context" | ||
| "errors" | ||
| "io" | ||
| "net/http" | ||
| "strings" | ||
| "time" | ||
|
|
||
| "github.com/julienschmidt/httprouter" | ||
|
|
@@ -43,7 +46,7 @@ type WebService struct { | |
| func newRouter() *httprouter.Router { | ||
| router := httprouter.New() | ||
| for _, webRoute := range webRoutes { | ||
| handler := loggingHandler(webRoute.HandlerFunc, webRoute.Name) | ||
| handler := gzipHandler(loggingHandler(webRoute.HandlerFunc, webRoute.Name)) | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't think we should unconditionally GZIP every response, only those that might be large. I'd suggest adding a "Compress" boolean to the route object in routes.go and use that to determine whether to add the gzip handler. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For example, none of the /debug routes should have it (many of those generate binary data which is not compressible, and/or are simple text that wouldn't benefit). It should probably not be on anything that typically results in < 10 KB of data. |
||
| router.Handler(webRoute.Method, webRoute.Pattern, handler) | ||
| } | ||
| return router | ||
|
|
@@ -94,3 +97,27 @@ func (m *WebService) StopWebApp() error { | |
|
|
||
| return nil | ||
| } | ||
|
|
||
| type gzipResponseWriter struct { | ||
| io.Writer | ||
| http.ResponseWriter | ||
| } | ||
|
|
||
| func (w gzipResponseWriter) Write(b []byte) (int, error) { | ||
| return w.Writer.Write(b) | ||
| } | ||
|
|
||
| func gzipHandler(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) | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As we apply the compression to all restful APIs by changing the
webservicedirectly, it seems we should add test forwebserviceinstead of verifying all APIs here. For example: we can modifynewRouterto make it accept custom routes, and then we write some dumb routes in testing to check the gzip compression.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
+1 for the dummy route
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The test about
webservicehas been added. I don't think we should modifynewRouterjust for the test case, so I kept it as it is. Instead, I simulate the action inwebserviceand then check whether the result is correct.