|
| 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 | + "crypto/rand" |
| 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 = 100 |
| 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{contextID: contextID} |
| 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 | + contextID uint32 |
| 49 | + callBack func(numHeaders, bodySize, numTrailers int) |
| 50 | + cnt int |
| 51 | +} |
| 52 | + |
| 53 | +// OnPluginStart implements types.PluginContext. |
| 54 | +func (ctx *pluginContext) OnPluginStart(pluginConfigurationSize int) types.OnPluginStartStatus { |
| 55 | + if err := proxywasm.SetTickPeriodMilliSeconds(tickMilliseconds); err != nil { |
| 56 | + proxywasm.LogCriticalf("failed to set tick period: %v", err) |
| 57 | + return types.OnPluginStartStatusFailed |
| 58 | + } |
| 59 | + proxywasm.LogInfof("set tick period milliseconds: %d", tickMilliseconds) |
| 60 | + ctx.callBack = func(numHeaders, bodySize, numTrailers int) { |
| 61 | + ctx.cnt++ |
| 62 | + proxywasm.LogInfof("called %d for contextID=%d", ctx.cnt, ctx.contextID) |
| 63 | + headers, err := proxywasm.GetHttpCallResponseHeaders() |
| 64 | + if err != nil && err != types.ErrorStatusNotFound { |
| 65 | + panic(err) |
| 66 | + } |
| 67 | + for _, h := range headers { |
| 68 | + proxywasm.LogInfof("response header for the dispatched call: %s: %s", h[0], h[1]) |
| 69 | + } |
| 70 | + headers, err = proxywasm.GetHttpCallResponseTrailers() |
| 71 | + if err != nil && err != types.ErrorStatusNotFound { |
| 72 | + panic(err) |
| 73 | + } |
| 74 | + for _, h := range headers { |
| 75 | + proxywasm.LogInfof("response trailer for the dispatched call: %s: %s", h[0], h[1]) |
| 76 | + } |
| 77 | + } |
| 78 | + return types.OnPluginStartStatusOK |
| 79 | +} |
| 80 | + |
| 81 | +// OnTick implements types.PluginContext. |
| 82 | +func (ctx *pluginContext) OnTick() { |
| 83 | + headers := [][2]string{ |
| 84 | + {":method", "GET"}, {":authority", "some_authority"}, {"accept", "*/*"}, |
| 85 | + } |
| 86 | + // Pick random value to select the request path. |
| 87 | + buf := make([]byte, 1) |
| 88 | + _, _ = rand.Read(buf) |
| 89 | + if buf[0]%2 == 0 { |
| 90 | + headers = append(headers, [2]string{":path", "/ok"}) |
| 91 | + } else { |
| 92 | + headers = append(headers, [2]string{":path", "/fail"}) |
| 93 | + } |
| 94 | + if _, err := proxywasm.DispatchHttpCall("web_service", headers, nil, nil, 5000, ctx.callBack); err != nil { |
| 95 | + proxywasm.LogCriticalf("dispatch httpcall failed: %v", err) |
| 96 | + } |
| 97 | +} |
0 commit comments