|
| 1 | +// Copyright 2020-2024 Tetrate |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package main |
| 16 | + |
| 17 | +import ( |
| 18 | + "encoding/hex" |
| 19 | + |
| 20 | + "github.com/proxy-wasm/proxy-wasm-go-sdk/proxywasm" |
| 21 | + "github.com/proxy-wasm/proxy-wasm-go-sdk/proxywasm/types" |
| 22 | +) |
| 23 | + |
| 24 | +const tickMilliseconds uint32 = 1 |
| 25 | + |
| 26 | +func main() {} |
| 27 | +func init() { |
| 28 | + proxywasm.SetVMContext(&vmContext{}) |
| 29 | +} |
| 30 | + |
| 31 | +// vmContext implements types.VMContext. |
| 32 | +type vmContext struct { |
| 33 | + // Embed the default VM context here, |
| 34 | + // so that we don't need to reimplement all the methods. |
| 35 | + types.DefaultVMContext |
| 36 | +} |
| 37 | + |
| 38 | +// NewPluginContext implements types.VMContext. |
| 39 | +func (*vmContext) NewPluginContext(contextID uint32) types.PluginContext { |
| 40 | + return &pluginContext{} |
| 41 | +} |
| 42 | + |
| 43 | +// pluginContext implements types.PluginContext. |
| 44 | +type pluginContext struct { |
| 45 | + // Embed the default plugin context here, |
| 46 | + // so that we don't need to reimplement all the methods. |
| 47 | + types.DefaultPluginContext |
| 48 | + callNum uint32 |
| 49 | +} |
| 50 | + |
| 51 | +// OnPluginStart implements types.PluginContext. |
| 52 | +func (ctx *pluginContext) OnPluginStart(pluginConfigurationSize int) types.OnPluginStartStatus { |
| 53 | + if err := proxywasm.SetTickPeriodMilliSeconds(tickMilliseconds); err != nil { |
| 54 | + proxywasm.LogCriticalf("failed to set tick period: %v", err) |
| 55 | + return types.OnPluginStartStatusFailed |
| 56 | + } |
| 57 | + proxywasm.LogInfof("set tick period milliseconds: %d", tickMilliseconds) |
| 58 | + return types.OnPluginStartStatusOK |
| 59 | +} |
| 60 | + |
| 61 | +// OnTick implements types.PluginContext. |
| 62 | +func (ctx *pluginContext) OnTick() { |
| 63 | + ctx.callNum++ |
| 64 | + ret, err := proxywasm.CallForeignFunction("compress", []byte("hello world!")) |
| 65 | + if err != nil { |
| 66 | + proxywasm.LogCriticalf("foreign function (compress) failed: %v", err) |
| 67 | + } |
| 68 | + proxywasm.LogInfof("foreign function (compress) called: %d, result: %s", ctx.callNum, hex.EncodeToString(ret)) |
| 69 | +} |
0 commit comments