This repository has been archived by the owner on Jan 21, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 20
/
bulk_app_state.go
81 lines (69 loc) · 1.94 KB
/
bulk_app_state.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
package handlers
import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"time"
"code.cloudfoundry.org/lager"
"github.com/cloudfoundry/hm9000/store"
"code.cloudfoundry.org/clock"
)
type bulkHandler struct {
logger lager.Logger
store store.Store
clock clock.Clock
}
type AppStateRequest struct {
AppGuid string `json:"droplet"`
AppVersion string `json:"version"`
}
func NewBulkAppStateHandler(logger lager.Logger, store store.Store, clock clock.Clock) http.Handler {
return &bulkHandler{
logger: logger,
store: store,
clock: clock,
}
}
func (handler *bulkHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
startTime := time.Now()
requests := make([]AppStateRequest, 0)
bodyBytes, err := ioutil.ReadAll(r.Body)
if err != nil {
handler.logger.Error("Failed to handle bulk_app_state request", err)
w.Write([]byte("{}"))
}
err = json.Unmarshal(bodyBytes, &requests)
if err != nil {
handler.logger.Error("Failed to unmarshal bulk app state request", err, lager.Data{
"payload": string(bodyBytes),
"elapsed time": fmt.Sprintf("%s", time.Since(startTime)),
})
w.Write([]byte("{}"))
return
}
err = handler.store.VerifyFreshness(handler.clock.Now())
if err != nil {
handler.logger.Error("Failed to verify freshness while handling bulk app state request", err, lager.Data{
"payload": string(bodyBytes),
"elapsed time": fmt.Sprintf("%s", time.Since(startTime)),
})
w.Write([]byte("{}"))
return
}
var apps = make(map[string]interface{})
for _, request := range requests {
app, err := handler.store.GetApp(request.AppGuid, request.AppVersion)
if err == nil {
apps[app.AppGuid] = app
}
}
appsJson, err := json.Marshal(apps)
if err != nil {
handler.logger.Error("Failed to marshal app states while handling bulk app state request", err, lager.Data{
"payload": string(bodyBytes),
"elapsed time": fmt.Sprintf("%s", time.Since(startTime)),
})
}
w.Write([]byte(appsJson))
}