-
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 13 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,7 @@ | |
| package webservice | ||
|
|
||
| import ( | ||
| "compress/gzip" | ||
| "encoding/json" | ||
| "fmt" | ||
| "io" | ||
|
|
@@ -59,6 +60,7 @@ const ( | |
| GroupNameMissing = "Group name is missing" | ||
| ApplicationDoesNotExists = "Application not found" | ||
| NodeDoesNotExists = "Node not found" | ||
| UnsupportedCompType = "Compression type not support" | ||
| ) | ||
|
|
||
| var allowedActiveStatusMsg string | ||
|
|
@@ -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) | ||
|
|
@@ -745,6 +751,10 @@ func getQueueApplications(w http.ResponseWriter, r *http.Request) { | |
| for _, app := range queue.GetCopyOfApps() { | ||
| appsDao = append(appsDao, getApplicationDAO(app)) | ||
| } | ||
| if checkHeader(r.Header, "Accept-Encoding", "gzip") { | ||
| compress(w, appsDao) | ||
| return | ||
| } | ||
|
|
||
| if err := json.NewEncoder(w).Encode(appsDao); err != nil { | ||
| buildJSONErrorResponse(w, err.Error(), http.StatusInternalServerError) | ||
|
|
@@ -1216,3 +1226,38 @@ 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 strings.Contains(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 | ||
| } | ||
|
|
||
| writer := gzip.NewWriter(w) | ||
|
||
| w.Header().Set("Content-Encoding", "gzip") | ||
| _, err = writer.Write(response) | ||
| if err != nil { | ||
| _ = writer.Close() | ||
| buildJSONErrorResponse(w, err.Error(), http.StatusInternalServerError) | ||
targetoee marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| return | ||
| } | ||
| err = writer.Flush() | ||
| if err != nil { | ||
| _ = writer.Close() | ||
| buildJSONErrorResponse(w, err.Error(), http.StatusInternalServerError) | ||
| return | ||
| } | ||
|
|
||
| _ = writer.Close() | ||
| } | ||
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.
nit: "not supported"
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 shouldn't be here at all. As I've repeatedly indicated we should never fail with an error, just fall back to no compression.