add caching to status response
@@ -31,17 +31,26 @@ type ServerItem struct {
}
type ServerList []ServerItem
+type statusResponse map[string]interface{}
+var cachedResponse *statusResponse = nil
+var lastUpdate time.Time
+
func GetStatus(w http.ResponseWriter, req *http.Request) {
+ respStatus := http.StatusOK
+ var response *statusResponse
+ if cachedResponse != nil && lastUpdate.After(time.Now().Add(60*time.Second)) {
+ response = cachedResponse
+ } else {
+
services := map[string]ServerList{
"lbrynet": ServerList{},
"player": ServerList{},
}
- response := map[string]interface{}{
+ response = &statusResponse{
"timestamp": fmt.Sprintf("%v", time.Now().UTC()),
"services": services,
"general_state": StatusOK,
}
- respStatus := http.StatusOK
failureDetected := false
router := router.NewDefault()
@@ -80,9 +89,11 @@ func GetStatus(w http.ResponseWriter, req *http.Request) {
services["player"] = append(services["player"], srv)
}
if failureDetected {
- response["general_state"] = StatusFailing
+ (*response)["general_state"] = StatusFailing
+ }
+ cachedResponse = response
+ lastUpdate = time.Now()
}
-
w.Header().Add("content-type", "application/json; charset=utf-8")
w.WriteHeader(respStatus)
respByte, _ := json.MarshalIndent(&response, "", " ")
