-
Notifications
You must be signed in to change notification settings - 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 6 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,6 +19,8 @@ | |
| package webservice | ||
|
|
||
| import ( | ||
| "bytes" | ||
| "compress/gzip" | ||
| "encoding/json" | ||
| "fmt" | ||
| "io" | ||
|
|
@@ -153,6 +155,10 @@ func writeHeaders(w http.ResponseWriter) { | |
| w.Header().Set("Access-Control-Allow-Headers", "X-Requested-With,Content-Type,Accept,Origin") | ||
| } | ||
|
|
||
| func writeHeader(w http.ResponseWriter, key, val string) { | ||
| w.Header().Set(key, val) | ||
| } | ||
|
|
||
| func buildJSONErrorResponse(w http.ResponseWriter, detail string, code int) { | ||
| w.WriteHeader(code) | ||
| errorInfo := dao.NewYAPIError(nil, code, detail) | ||
|
|
@@ -746,6 +752,11 @@ func getQueueApplications(w http.ResponseWriter, r *http.Request) { | |
| appsDao = append(appsDao, getApplicationDAO(app)) | ||
| } | ||
|
|
||
| if checkHeader(r.Header, "Content-Encoding", "gzip") { | ||
| compress(w, appsDao) | ||
| return | ||
| } | ||
|
|
||
| if err := json.NewEncoder(w).Encode(appsDao); err != nil { | ||
| buildJSONErrorResponse(w, err.Error(), http.StatusInternalServerError) | ||
| } | ||
|
|
@@ -1216,3 +1227,40 @@ func getStream(w http.ResponseWriter, r *http.Request) { | |
| } | ||
| } | ||
| } | ||
|
|
||
| func checkHeader(h http.Header, key string, value string) bool { | ||
|
||
| values := h.Values(key) | ||
| for _, v := range values { | ||
| if v == value { | ||
| return true | ||
| } | ||
| } | ||
| return false | ||
| } | ||
|
|
||
| func compress(w http.ResponseWriter, data any) { | ||
| response, err := json.Marshal(data) | ||
| if err != nil { | ||
| buildJSONErrorResponse(w, err.Error(), http.StatusInternalServerError) | ||
| return | ||
| } | ||
|
|
||
| var compressedData bytes.Buffer | ||
| writer := gzip.NewWriter(&compressedData) | ||
| _, err = writer.Write(response) | ||
| if err != nil { | ||
| buildJSONErrorResponse(w, err.Error(), http.StatusInternalServerError) | ||
targetoee marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| return | ||
| } | ||
|
|
||
| err = writer.Close() | ||
| if err != nil { | ||
| buildJSONErrorResponse(w, err.Error(), http.StatusInternalServerError) | ||
| return | ||
| } | ||
|
|
||
| writeHeader(w, "Content-Encoding", "gzip") | ||
| if _, err = w.Write(compressedData.Bytes()); err != nil { | ||
| buildJSONErrorResponse(w, err.Error(), http.StatusInternalServerError) | ||
| } | ||
| } | ||
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.
This PR aims to enhance only
getQueueApplication. However, I'd like to open the room to discuss whether we should bring this enhancement to all restful APIs. @targetoee WDYT?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.
That's a good idea. It can be an optional choice for user to provide more flexibility.
Some APIs typically don't return large amounts of data in common cases, so it may be necessary to discuss which ones require this functionality.
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 improvement you propose is a kind of infra to our Restful APIs, so I prefer to bring such benefit to all APIs if there is no obvious side-effect or cost.
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.
That is exactly why I would make the two functions so we can easily apply it to any REST end points. Anything that sends more than a single IP packet as the response can benefit.
For the streaming API, which uses really small messages that fit in a single IP packet, compressing might be more overhead than the gains we get so that one might not be a candidate everything else is.