-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathrequest_handler.go
More file actions
158 lines (137 loc) · 3.96 KB
/
Copy pathrequest_handler.go
File metadata and controls
158 lines (137 loc) · 3.96 KB
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
package main
import (
"encoding/json"
"net/http"
"strconv"
"time"
"github.com/Prev/HotFunctions/worker_front/types"
)
// RequestHandler of the worker front
type RequestHandler struct {
http.Handler
functionRunner *FunctionRunner
}
func newRequestHandler(options CachingOptions) *RequestHandler {
h := new(RequestHandler)
h.functionRunner = newFunctionRunner(options)
return h
}
type FailResponse struct {
Error bool
Message string
}
type ConfigureSuccessResponse struct {
Message string
State CachingOptions
}
func (h *RequestHandler) ServeHTTP(w http.ResponseWriter, req *http.Request) {
logger.Println(req.Method, req.URL.String())
w.Header().Add("Content-Type", "application/json")
switch req.URL.Path {
case "/configure":
h.ConfigureWorker(&w, req)
case "/clear":
h.Clear(&w, req)
case "/prepare":
h.Prepare(&w, req)
case "/execute":
h.ExecFunction(&w, req)
default:
w.WriteHeader(404)
writeFailResponse(&w, "404 Not found on given path")
}
}
func (h *RequestHandler) ConfigureWorker(w *http.ResponseWriter, req *http.Request) {
q := req.URL.Query()
oldOptions := h.functionRunner.cachingOptions
if v := q["user_code_size_limit"]; len(v) > 0 {
val, _ := strconv.ParseInt(v[0], 10, 64)
h.functionRunner.cachingOptions.UserCodeSizeLimit = val
}
if v := q["container_pool_limit"]; len(v) > 0 {
val, _ := strconv.Atoi(v[0])
h.functionRunner.cachingOptions.ContainerPoolLimit = val
}
if v := q["container_pool_num"]; len(v) > 0 {
val, _ := strconv.Atoi(v[0])
h.functionRunner.cachingOptions.ContainerPoolNum = val
}
if v := q["using_rest_mode"]; len(v) > 0 {
if v[0] == "true" {
h.functionRunner.cachingOptions.UsingRestMode = true
} else if v[0] == "false" {
h.functionRunner.cachingOptions.UsingRestMode = false
}
h.functionRunner.images = make(map[string]Image)
}
if v := q["rest_container_life_time"]; len(v) > 0 {
val, _ := strconv.Atoi(v[0])
h.functionRunner.cachingOptions.RestContainerLifeTime = val
}
message := "nothing changed"
newOptions := h.functionRunner.cachingOptions
if oldOptions.UserCodeSizeLimit != newOptions.UserCodeSizeLimit ||
oldOptions.ContainerPoolLimit != newOptions.ContainerPoolLimit ||
oldOptions.ContainerPoolNum != newOptions.ContainerPoolNum ||
oldOptions.UsingRestMode != newOptions.UsingRestMode ||
oldOptions.RestContainerLifeTime != newOptions.RestContainerLifeTime {
message = "configure changed"
}
resp := ConfigureSuccessResponse{message, h.functionRunner.cachingOptions}
bytes, _ := json.Marshal(resp)
(*w).Write(bytes)
}
func (h *RequestHandler) Clear(w *http.ResponseWriter, req *http.Request) {
q := req.URL.Query()
if v := q["reset_images"]; len(v) > 0 {
if v[0] == "true" {
h.functionRunner.Reset(true)
(*w).Write([]byte("done; reset_images=true"))
return
}
}
h.functionRunner.Reset(false)
(*w).Write([]byte("done; reset_images=false"))
}
func (h *RequestHandler) Prepare(w *http.ResponseWriter, req *http.Request) {
err := h.functionRunner.PrepareImages()
if err != nil {
(*w).Write([]byte(err.Error()))
} else {
(*w).Write([]byte("done"))
}
}
func (h *RequestHandler) ExecFunction(w *http.ResponseWriter, req *http.Request) {
q := req.URL.Query()
nameParam := q["name"]
if len(nameParam) == 0 {
writeFailResponse(w, "param 'name' is not given")
return
}
startTime := makeTimestamp()
functionName := nameParam[0]
out, meta := h.functionRunner.RunFunction(functionName)
if out == nil {
writeFailResponse(w, "error on running a function")
return
}
endTime := makeTimestamp()
logger.Println("fin", functionName)
resp := types.ExecSuccessResponse{
out.Data,
endTime - startTime,
out.EndTime - out.StartTime,
meta,
}
bytes, _ := json.Marshal(resp)
(*w).Write(bytes)
}
func writeFailResponse(w *http.ResponseWriter, message string) {
resp := FailResponse{true, message}
bytes, _ := json.Marshal(resp)
(*w).WriteHeader(500)
(*w).Write(bytes)
}
func makeTimestamp() int64 {
return time.Now().UnixNano() / int64(time.Millisecond)
}