-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.go
66 lines (53 loc) · 1.47 KB
/
main.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
// Copyright wasilibs authors
// SPDX-License-Identifier: MIT
package main
import (
"bytes"
"strconv"
"github.com/tetratelabs/proxy-wasm-go-sdk/proxywasm"
"github.com/tetratelabs/proxy-wasm-go-sdk/proxywasm/types"
_ "github.com/wasilibs/nottinygc"
)
func main() {
proxywasm.SetVMContext(&vm{})
}
type vm struct {
types.DefaultVMContext
}
func (v *vm) NewPluginContext(contextID uint32) types.PluginContext {
return &plugin{}
}
type plugin struct {
// Embed the default plugin context here,
// so that we don't need to reimplement all the methods.
types.DefaultPluginContext
size int
}
// OnPluginStart Override types.DefaultPluginContext.
func (h *plugin) OnPluginStart(_ int) types.OnPluginStartStatus {
data, err := proxywasm.GetPluginConfiguration()
if err != nil {
panic(err)
}
sz, err := strconv.Atoi(string(bytes.TrimSpace(data)))
if err != nil {
panic(err)
}
h.size = sz
return types.OnPluginStartStatusOK
}
// NewHttpContext Override types.DefaultPluginContext to allow us to declare a request handler for each
// intercepted request the Envoy Sidecar sends us
func (h *plugin) NewHttpContext(_ uint32) types.HttpContext {
return &tester{size: h.size}
}
type tester struct {
types.DefaultHttpContext
size int
}
func (c *tester) OnHttpRequestHeaders(numHeaders int, endOfStream bool) types.Action {
b := make([]byte, c.size)
proxywasm.LogInfof("alloc success, point address: %p", b)
proxywasm.SendHttpResponse(200, nil, nil, -1)
return types.ActionContinue
}