Skip to content
This repository was archived by the owner on Dec 11, 2023. It is now read-only.

Commit 3ffb275

Browse files
committed
Limits configurable via env vars
1 parent eb570a4 commit 3ffb275

File tree

1 file changed

+37
-10
lines changed

1 file changed

+37
-10
lines changed

main.go

Lines changed: 37 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,6 @@ import (
2929
"github.com/triggermesh/aws-custom-runtime/pkg/events/apiGateway"
3030
)
3131

32-
const (
33-
numberOfinvokers = 8 // Number of bootstrap processes
34-
requestSizeLimit = 1e+7 // Request bosy size limit, 10Mb
35-
functionTTL = 3e+9 // Funtions deadline, 3 seconds
36-
)
37-
3832
type message struct {
3933
id string
4034
deadline int64
@@ -47,6 +41,10 @@ type responseWrapper struct {
4741
}
4842

4943
var (
44+
numberOfinvokers = 8 // Number of bootstrap processes
45+
requestSizeLimit int64 = 5 // Request body size limit, Mb
46+
functionTTL int64 = 10 // Funtions deadline, seconds
47+
5048
tasks chan message
5149
results map[string]chan message
5250

@@ -85,7 +83,9 @@ func setupEnv() error {
8583
}
8684

8785
func newTask(w http.ResponseWriter, r *http.Request) {
88-
body, err := ioutil.ReadAll(http.MaxBytesReader(w, r.Body, requestSizeLimit))
86+
requestSizeLimitInBytes := requestSizeLimit * 1e+6
87+
functionTTLInNanoSeconds := functionTTL * 1e+9
88+
body, err := ioutil.ReadAll(http.MaxBytesReader(w, r.Body, requestSizeLimitInBytes))
8989
if err != nil {
9090
http.Error(w, err.Error(), http.StatusInternalServerError)
9191
return
@@ -95,7 +95,7 @@ func newTask(w http.ResponseWriter, r *http.Request) {
9595
now := time.Now().UnixNano()
9696
task := message{
9797
id: fmt.Sprintf("%d", now),
98-
deadline: now + functionTTL,
98+
deadline: now + functionTTLInNanoSeconds,
9999
data: body,
100100
}
101101
fmt.Printf("<- %s %s\n", task.id, task.data)
@@ -109,7 +109,7 @@ func newTask(w http.ResponseWriter, r *http.Request) {
109109
tasks <- task
110110

111111
select {
112-
case <-time.After(time.Duration(functionTTL)):
112+
case <-time.After(time.Duration(functionTTLInNanoSeconds)):
113113
fmt.Printf("-> ! %s Deadline is reached\n", task.id)
114114
w.WriteHeader(http.StatusGone)
115115
w.Write([]byte(fmt.Sprintf("Deadline is reached, data %s", task.data)))
@@ -214,6 +214,30 @@ func mapEvent(h http.Handler) http.Handler {
214214
})
215215
}
216216

217+
func setLimits() {
218+
if v, ok := os.LookupEnv("INVOKER_COUNT"); ok {
219+
if vv, err := strconv.Atoi(v); err != nil {
220+
fmt.Printf("can't set invokers limit, using default value %d\n", numberOfinvokers)
221+
} else {
222+
numberOfinvokers = vv
223+
}
224+
}
225+
if v, ok := os.LookupEnv("REQUEST_SIZE_LIMIT"); ok {
226+
if vv, err := strconv.Atoi(v); err != nil {
227+
fmt.Printf("can't set request size limit, using default value %d\n", requestSizeLimit)
228+
} else {
229+
requestSizeLimit = int64(vv)
230+
}
231+
}
232+
if v, ok := os.LookupEnv("FUNCTION_TTL"); ok {
233+
if vv, err := strconv.Atoi(v); err != nil {
234+
fmt.Printf("can't set function ttl, using default value %d\n", functionTTL)
235+
} else {
236+
functionTTL = int64(vv)
237+
}
238+
}
239+
}
240+
217241
func api() error {
218242
apiRouter := http.NewServeMux()
219243
apiRouter.HandleFunc(awsEndpoint+"/init/error", initError)
@@ -227,6 +251,9 @@ func main() {
227251
results = make(map[string]chan message)
228252
defer close(tasks)
229253

254+
fmt.Println("Setting limits")
255+
setLimits()
256+
230257
fmt.Println("Setup env")
231258
if err := setupEnv(); err != nil {
232259
log.Fatalln(err)
@@ -237,8 +264,8 @@ func main() {
237264
log.Fatalln(api())
238265
}()
239266

240-
fmt.Println("Starting invokers")
241267
for i := 0; i < numberOfinvokers; i++ {
268+
fmt.Println("Starting bootstrap", i+1)
242269
go func() {
243270
if err := exec.Command("sh", "-c", environment["LAMBDA_TASK_ROOT"]+"/bootstrap").Run(); err != nil {
244271
log.Fatalln(err)

0 commit comments

Comments
 (0)