generated from projectsesame/envoy-extproc-payloadlimit-demo-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
delayed-return.go
108 lines (73 loc) · 2.76 KB
/
delayed-return.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
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
package main
import (
"log"
"strconv"
"time"
typev3 "github.com/envoyproxy/go-control-plane/envoy/type/v3"
ep "github.com/wrossmorrow/envoy-extproc-sdk-go"
)
type delayedReturnRequestProcessor struct {
opts *ep.ProcessingOptions
}
func (s *delayedReturnRequestProcessor) GetName() string {
return "delayed-return"
}
func (s *delayedReturnRequestProcessor) GetOptions() *ep.ProcessingOptions {
return s.opts
}
const kDelayedTime = "delayed-time"
func (s *delayedReturnRequestProcessor) ProcessRequestHeaders(ctx *ep.RequestContext, headers ep.AllHeaders) error {
cancel := func(code int32, msg string) error {
return ctx.CancelRequest(code, map[string]ep.HeaderValue{}, typev3.StatusCode_name[code])
}
var delayedTime int64
var err error
if headers.RawHeaders[kDelayedTime] == nil {
delayedTime = 100
log.Printf("[%s], delayed-time header is not set, use default value 100", ctx.RequestID)
} else {
delayedTime, err = strconv.ParseInt(string(headers.RawHeaders[kDelayedTime]), 10, 64)
if err != nil {
return cancel(400, "delayed-time header is invalid")
}
}
if delayedTime < 0 {
return cancel(400, "delayed-time header is invalid")
}
ctx.SetValue("delayed-time", delayedTime)
logDelayedTime(ctx, "ProcessRequestHeaders")
return ctx.ContinueRequest()
}
func (s *delayedReturnRequestProcessor) ProcessRequestBody(ctx *ep.RequestContext, body []byte) error {
logDelayedTime(ctx, "ProcessRequestBody")
return ctx.ContinueRequest()
}
func logDelayedTime(ctx *ep.RequestContext, method string) {
var delayedTime int64
v, _ := ctx.GetValue(`delayed-time`)
delayedTime = v.(int64)
now := time.Now()
time.Sleep(time.Duration(delayedTime) * time.Millisecond)
log.Printf("[%s],process %s, delayed time: %d, elapsed time: %d\n", ctx.RequestID, method, delayedTime, time.Since(now).Milliseconds())
}
func (s *delayedReturnRequestProcessor) ProcessRequestTrailers(ctx *ep.RequestContext, trailers ep.AllHeaders) error {
logDelayedTime(ctx, "ProcessRequestTrailers")
return ctx.ContinueRequest()
}
func (s *delayedReturnRequestProcessor) ProcessResponseHeaders(ctx *ep.RequestContext, headers ep.AllHeaders) error {
logDelayedTime(ctx, "ProcessResponseHeaders")
return ctx.ContinueRequest()
}
func (s *delayedReturnRequestProcessor) ProcessResponseBody(ctx *ep.RequestContext, body []byte) error {
logDelayedTime(ctx, "ProcessResponseBody")
return ctx.ContinueRequest()
}
func (s *delayedReturnRequestProcessor) ProcessResponseTrailers(ctx *ep.RequestContext, trailers ep.AllHeaders) error {
logDelayedTime(ctx, "ProcessResponseTrailers")
return ctx.ContinueRequest()
}
func (s *delayedReturnRequestProcessor) Init(opts *ep.ProcessingOptions, nonFlagArgs []string) error {
s.opts = opts
return nil
}
func (s *delayedReturnRequestProcessor) Finish() {}