-
Notifications
You must be signed in to change notification settings - Fork 5
/
warmup.go
40 lines (35 loc) · 975 Bytes
/
warmup.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
package vesper
import (
"context"
)
// WarmupEvent is the manual event
// See https://www.npmjs.com/package/serverless-plugin-warmup
// {
// "Event": {
// "source": "serverless-plugin-warmup"
// }
// }
type warmupEvent struct {
Event struct {
Source string
}
}
// WarmupMiddleware detects a warmup invocation event from the
// plugin "serverless-plugin-warmup", and returns early if found
//
// See https://www.npmjs.com/package/serverless-plugin-warmup for more
func WarmupMiddleware(f LambdaFunc) LambdaFunc {
return func(ctx context.Context, in interface{}) (interface{}, error) {
log.Println("[warmupMiddleware] START")
var event warmupEvent
if err := ExtractType(ctx, &event); err == nil {
if event.Event.Source == "serverless-plugin-warmup" {
log.Println("[warmupMiddleware] warmup event detected, exiting")
return "warmup", nil
}
}
res, err := f(ctx, in)
log.Println("[warmupMiddleware] END: ", res)
return res, err
}
}