From 0931d78f2c14cca39dffd57c6c416dc149398efc Mon Sep 17 00:00:00 2001 From: Takeshi Yoneda Date: Tue, 28 May 2024 10:30:12 +0900 Subject: [PATCH] Clean ups example codes Signed-off-by: Takeshi Yoneda --- Makefile | 9 ++++--- NOTICE | 2 +- e2e/e2e_test.go | 2 +- e2e/main_test.go | 2 +- examples/dispatch_call_on_tick/main.go | 10 ++++--- examples/foreign_call_on_tick/main.go | 13 ++++----- examples/helloworld/main.go | 13 ++++----- examples/http_auth_random/main.go | 20 ++++++++------ examples/http_body/main.go | 22 ++++++++------- examples/http_body_chunk/main.go | 15 ++++++----- examples/http_headers/main.go | 16 ++++++----- examples/http_routing/main.go | 13 +++++---- examples/json_validation/main.go | 20 +++++++------- examples/metrics/main.go | 13 ++++++--- examples/multiple_dispatches/main.go | 15 ++++++----- examples/network/main.go | 20 +++++++------- examples/postpone_requests/main.go | 19 +++++++------ examples/properties/main.go | 14 +++++----- examples/properties/main_test.go | 5 ++-- examples/shared_data/main.go | 17 +++++++----- examples/shared_queue/receiver/main.go | 10 ++++--- examples/shared_queue/sender/main.go | 27 +++++++++---------- examples/vm_plugin_configuration/main.go | 9 ++++--- proxywasm/entrypoint.go | 2 +- proxywasm/hostcall.go | 2 +- proxywasm/hostcall_test.go | 2 +- proxywasm/internal/abi_callback_alloc.go | 2 +- .../internal/abi_callback_configuration.go | 2 +- .../abi_callback_configuration_test.go | 2 +- proxywasm/internal/abi_callback_l4.go | 2 +- proxywasm/internal/abi_callback_l4_test.go | 2 +- proxywasm/internal/abi_callback_l7.go | 2 +- proxywasm/internal/abi_callback_l7_test.go | 2 +- proxywasm/internal/abi_callback_lifecycle.go | 2 +- .../internal/abi_callback_lifecycle_test.go | 2 +- proxywasm/internal/abi_callback_queue.go | 2 +- proxywasm/internal/abi_callback_queue_test.go | 2 +- .../internal/abi_callback_test_export.go | 2 +- proxywasm/internal/abi_callback_timers.go | 2 +- .../internal/abi_callback_timers_test.go | 2 +- proxywasm/internal/abi_callback_version.go | 2 +- proxywasm/internal/abi_hostcalls.go | 2 +- proxywasm/internal/abi_hostcalls_mock.go | 2 +- proxywasm/internal/hostcall_utils_go.go | 2 +- proxywasm/internal/hostcall_utils_test.go | 2 +- proxywasm/internal/hostcall_utils_tinygo.go | 2 +- proxywasm/internal/hostcall_utls.go | 2 +- proxywasm/internal/serde.go | 2 +- proxywasm/internal/serde_test.go | 2 +- proxywasm/internal/vmstate.go | 2 +- proxywasm/internal/vmstate_test.go | 2 +- proxywasm/internal/vmstate_test_export.go | 2 +- proxywasm/proxytest/http.go | 2 +- proxywasm/proxytest/network.go | 2 +- proxywasm/proxytest/option.go | 2 +- proxywasm/proxytest/proxytest.go | 2 +- proxywasm/proxytest/root.go | 2 +- proxywasm/types/context.go | 2 +- proxywasm/types/types.go | 2 +- 59 files changed, 212 insertions(+), 166 deletions(-) diff --git a/Makefile b/Makefile index c8319c07..d5ebc018 100755 --- a/Makefile +++ b/Makefile @@ -1,5 +1,5 @@ -goimports := golang.org/x/tools/cmd/goimports@v0.7.0 -golangci_lint := github.com/golangci/golangci-lint/cmd/golangci-lint@v1.52.2 +goimports := golang.org/x/tools/cmd/goimports@v0.21.0 +golangci_lint := github.com/golangci/golangci-lint/cmd/golangci-lint@v1.59.0 .PHONY: build.example @@ -40,7 +40,10 @@ run: .PHONY: lint lint: - @go run $(golangci_lint) run + @find . -name "go.mod" \ + | grep go.mod \ + | xargs -I {} bash -c 'dirname {}' \ + | xargs -I {} bash -c 'echo "=> {}"; cd {}; go run $(golangci_lint) run; ' .PHONY: format format: diff --git a/NOTICE b/NOTICE index 871b9ceb..ad469643 100644 --- a/NOTICE +++ b/NOTICE @@ -1,2 +1,2 @@ proxy-wasm-go-sdk -Copyright 2020-2021 Tetrate +Copyright 2020-2024 Tetrate diff --git a/e2e/e2e_test.go b/e2e/e2e_test.go index 16904f78..ed83e9db 100644 --- a/e2e/e2e_test.go +++ b/e2e/e2e_test.go @@ -1,4 +1,4 @@ -// Copyright 2020-2021 Tetrate +// Copyright 2020-2024 Tetrate // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. diff --git a/e2e/main_test.go b/e2e/main_test.go index 7f4ac7a2..c0083f96 100644 --- a/e2e/main_test.go +++ b/e2e/main_test.go @@ -1,4 +1,4 @@ -// Copyright 2021 Tetrate +// Copyright 2024 Tetrate // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. diff --git a/examples/dispatch_call_on_tick/main.go b/examples/dispatch_call_on_tick/main.go index 685a2609..3bb03100 100644 --- a/examples/dispatch_call_on_tick/main.go +++ b/examples/dispatch_call_on_tick/main.go @@ -1,4 +1,4 @@ -// Copyright 2020-2021 Tetrate +// Copyright 2020-2024 Tetrate // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. @@ -27,17 +27,19 @@ func main() { proxywasm.SetVMContext(&vmContext{}) } +// vmContext implements types.VMContext. type vmContext struct { // Embed the default VM context here, // so that we don't need to reimplement all the methods. types.DefaultVMContext } -// Override types.DefaultVMContext. +// NewPluginContext implements types.VMContext. func (*vmContext) NewPluginContext(contextID uint32) types.PluginContext { return &pluginContext{contextID: contextID} } +// pluginContext implements types.PluginContext. type pluginContext struct { // Embed the default plugin context here, // so that we don't need to reimplement all the methods. @@ -47,7 +49,7 @@ type pluginContext struct { cnt int } -// Override types.DefaultPluginContext. +// OnPluginStart implements types.PluginContext. func (ctx *pluginContext) OnPluginStart(pluginConfigurationSize int) types.OnPluginStartStatus { if err := proxywasm.SetTickPeriodMilliSeconds(tickMilliseconds); err != nil { proxywasm.LogCriticalf("failed to set tick period: %v", err) @@ -75,7 +77,7 @@ func (ctx *pluginContext) OnPluginStart(pluginConfigurationSize int) types.OnPlu return types.OnPluginStartStatusOK } -// Override types.DefaultPluginContext. +// OnTick implements types.PluginContext. func (ctx *pluginContext) OnTick() { headers := [][2]string{ {":method", "GET"}, {":authority", "some_authority"}, {"accept", "*/*"}, diff --git a/examples/foreign_call_on_tick/main.go b/examples/foreign_call_on_tick/main.go index 72f6702c..cc757f8f 100644 --- a/examples/foreign_call_on_tick/main.go +++ b/examples/foreign_call_on_tick/main.go @@ -1,4 +1,4 @@ -// Copyright 2020-2021 Tetrate +// Copyright 2020-2024 Tetrate // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. @@ -27,26 +27,27 @@ func main() { proxywasm.SetVMContext(&vmContext{}) } +// vmContext implements types.VMContext. type vmContext struct { // Embed the default VM context here, // so that we don't need to reimplement all the methods. types.DefaultVMContext } -// Override types.DefaultVMContext. +// NewPluginContext implements types.VMContext. func (*vmContext) NewPluginContext(contextID uint32) types.PluginContext { return &pluginContext{} } +// pluginContext implements types.PluginContext. type pluginContext struct { // Embed the default plugin context here, // so that we don't need to reimplement all the methods. types.DefaultPluginContext - contextID uint32 - callNum uint32 + callNum uint32 } -// Override types.DefaultPluginContext. +// OnPluginStart implements types.PluginContext. func (ctx *pluginContext) OnPluginStart(pluginConfigurationSize int) types.OnPluginStartStatus { if err := proxywasm.SetTickPeriodMilliSeconds(tickMilliseconds); err != nil { proxywasm.LogCriticalf("failed to set tick period: %v", err) @@ -56,7 +57,7 @@ func (ctx *pluginContext) OnPluginStart(pluginConfigurationSize int) types.OnPlu return types.OnPluginStartStatusOK } -// Override types.DefaultPluginContext. +// OnTick implements types.PluginContext. func (ctx *pluginContext) OnTick() { ctx.callNum++ ret, err := proxywasm.CallForeignFunction("compress", []byte("hello world!")) diff --git a/examples/helloworld/main.go b/examples/helloworld/main.go index 722c303c..e9801ce3 100644 --- a/examples/helloworld/main.go +++ b/examples/helloworld/main.go @@ -1,4 +1,4 @@ -// Copyright 2020-2021 Tetrate +// Copyright 2020-2024 Tetrate // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. @@ -28,25 +28,26 @@ func main() { proxywasm.SetVMContext(&vmContext{}) } +// vmContext implements types.VMContext. type vmContext struct { // Embed the default VM context here, // so that we don't need to reimplement all the methods. types.DefaultVMContext } -// Override types.DefaultVMContext. +// NewPluginContext implements types.VMContext. func (*vmContext) NewPluginContext(contextID uint32) types.PluginContext { return &helloWorld{} } +// helloWorld implements types.PluginContext. type helloWorld struct { // Embed the default plugin context here, // so that we don't need to reimplement all the methods. types.DefaultPluginContext - contextID uint32 } -// Override types.DefaultPluginContext. +// OnPluginStart implements types.PluginContext. func (ctx *helloWorld) OnPluginStart(pluginConfigurationSize int) types.OnPluginStartStatus { rand.Seed(time.Now().UnixNano()) @@ -58,12 +59,12 @@ func (ctx *helloWorld) OnPluginStart(pluginConfigurationSize int) types.OnPlugin return types.OnPluginStartStatusOK } -// Override types.DefaultPluginContext. +// OnTick implements types.PluginContext. func (ctx *helloWorld) OnTick() { t := time.Now().UnixNano() proxywasm.LogInfof("It's %d: random value: %d", t, rand.Uint64()) proxywasm.LogInfof("OnTick called") } -// Override types.DefaultPluginContext. +// NewHttpContext implements types.PluginContext. func (*helloWorld) NewHttpContext(uint32) types.HttpContext { return &types.DefaultHttpContext{} } diff --git a/examples/http_auth_random/main.go b/examples/http_auth_random/main.go index 2e5e3a98..443160ac 100644 --- a/examples/http_auth_random/main.go +++ b/examples/http_auth_random/main.go @@ -1,4 +1,4 @@ -// Copyright 2020-2021 Tetrate +// Copyright 2020-2024 Tetrate // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. @@ -27,28 +27,31 @@ func main() { proxywasm.SetVMContext(&vmContext{}) } +// vmContext implements types.VMContext. type vmContext struct { // Embed the default VM context here, // so that we don't need to reimplement all the methods. types.DefaultVMContext } -// Override types.DefaultVMContext. +// NewPluginContext implements types.VMContext. func (*vmContext) NewPluginContext(contextID uint32) types.PluginContext { return &pluginContext{} } +// pluginContext implements types.PluginContext. type pluginContext struct { // Embed the default plugin context here, // so that we don't need to reimplement all the methods. types.DefaultPluginContext } -// Override types.DefaultPluginContext. +// NewHttpContext implements types.PluginContext. func (*pluginContext) NewHttpContext(contextID uint32) types.HttpContext { return &httpAuthRandom{contextID: contextID} } +// httpAuthRandom implements types.HttpContext. type httpAuthRandom struct { // Embed the default http context here, // so that we don't need to reimplement all the methods. @@ -56,7 +59,7 @@ type httpAuthRandom struct { contextID uint32 } -// Override types.DefaultHttpContext. +// OnHttpRequestHeaders implements types.HttpContext. func (ctx *httpAuthRandom) OnHttpRequestHeaders(numHeaders int, endOfStream bool) types.Action { hs, err := proxywasm.GetHttpRequestHeaders() if err != nil { @@ -77,6 +80,7 @@ func (ctx *httpAuthRandom) OnHttpRequestHeaders(numHeaders int, endOfStream bool return types.ActionPause } +// httpCallResponseCallback is a callback function when the http call response is received after dispatching. func httpCallResponseCallback(numHeaders, bodySize, numTrailers int) { hs, err := proxywasm.GetHttpCallResponseHeaders() if err != nil { @@ -91,20 +95,20 @@ func httpCallResponseCallback(numHeaders, bodySize, numTrailers int) { b, err := proxywasm.GetHttpCallResponseBody(0, bodySize) if err != nil { proxywasm.LogCriticalf("failed to get response body: %v", err) - proxywasm.ResumeHttpRequest() + _ = proxywasm.ResumeHttpRequest() return } s := fnv.New32a() if _, err := s.Write(b); err != nil { proxywasm.LogCriticalf("failed to calculate hash: %v", err) - proxywasm.ResumeHttpRequest() + _ = proxywasm.ResumeHttpRequest() return } if s.Sum32()%2 == 0 { proxywasm.LogInfo("access granted") - proxywasm.ResumeHttpRequest() + _ = proxywasm.ResumeHttpRequest() return } @@ -114,6 +118,6 @@ func httpCallResponseCallback(numHeaders, bodySize, numTrailers int) { {"powered-by", "proxy-wasm-go-sdk!!"}, }, []byte(body), -1); err != nil { proxywasm.LogErrorf("failed to send local response: %v", err) - proxywasm.ResumeHttpRequest() + _ = proxywasm.ResumeHttpRequest() } } diff --git a/examples/http_body/main.go b/examples/http_body/main.go index c8a07a81..3c9d39e4 100644 --- a/examples/http_body/main.go +++ b/examples/http_body/main.go @@ -1,4 +1,4 @@ -// Copyright 2020-2021 Tetrate +// Copyright 2020-2024 Tetrate // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. @@ -29,17 +29,19 @@ func main() { proxywasm.SetVMContext(&vmContext{}) } +// vmContext implements types.VMContext. type vmContext struct { // Embed the default VM context here, // so that we don't need to reimplement all the methods. types.DefaultVMContext } -// Override types.DefaultVMContext. +// NewPluginContext implements types.VMContext. func (*vmContext) NewPluginContext(contextID uint32) types.PluginContext { return &pluginContext{} } +// pluginContext implements types.PluginContext. type pluginContext struct { // Embed the default plugin context here, // so that we don't need to reimplement all the methods. @@ -47,7 +49,7 @@ type pluginContext struct { shouldEchoBody bool } -// Override types.DefaultPluginContext. +// NewHttpContext implements types.PluginContext. func (ctx *pluginContext) NewHttpContext(contextID uint32) types.HttpContext { if ctx.shouldEchoBody { return &echoBodyContext{} @@ -55,7 +57,7 @@ func (ctx *pluginContext) NewHttpContext(contextID uint32) types.HttpContext { return &setBodyContext{} } -// Override types.DefaultPluginContext. +// OnPluginStart implements types.PluginContext. func (ctx *pluginContext) OnPluginStart(pluginConfigurationSize int) types.OnPluginStartStatus { data, err := proxywasm.GetPluginConfiguration() if err != nil { @@ -65,6 +67,7 @@ func (ctx *pluginContext) OnPluginStart(pluginConfigurationSize int) types.OnPlu return types.OnPluginStartStatusOK } +// setBodyContext implements types.HttpContext. type setBodyContext struct { // Embed the default root http context here, // so that we don't need to reimplement all the methods. @@ -73,7 +76,7 @@ type setBodyContext struct { bufferOperation string } -// Override types.DefaultHttpContext. +// OnHttpRequestHeaders implements types.HttpContext. func (ctx *setBodyContext) OnHttpRequestHeaders(numHeaders int, endOfStream bool) types.Action { mode, err := proxywasm.GetHttpRequestHeader("buffer-replace-at") if err == nil && mode == "response" { @@ -104,7 +107,7 @@ func (ctx *setBodyContext) OnHttpRequestHeaders(numHeaders int, endOfStream bool return types.ActionContinue } -// Override types.DefaultHttpContext. +// OnHttpRequestBody implements types.HttpContext. func (ctx *setBodyContext) OnHttpRequestBody(bodySize int, endOfStream bool) types.Action { if ctx.modifyResponse { return types.ActionContinue @@ -137,7 +140,7 @@ func (ctx *setBodyContext) OnHttpRequestBody(bodySize int, endOfStream bool) typ return types.ActionContinue } -// Override types.DefaultHttpContext. +// OnHttpResponseHeaders implements types.HttpContext. func (ctx *setBodyContext) OnHttpResponseHeaders(numHeaders int, endOfStream bool) types.Action { if !ctx.modifyResponse { return types.ActionContinue @@ -151,7 +154,7 @@ func (ctx *setBodyContext) OnHttpResponseHeaders(numHeaders int, endOfStream boo return types.ActionContinue } -// Override types.DefaultHttpContext. +// OnHttpResponseBody implements types.HttpContext. func (ctx *setBodyContext) OnHttpResponseBody(bodySize int, endOfStream bool) types.Action { if !ctx.modifyResponse { return types.ActionContinue @@ -184,6 +187,7 @@ func (ctx *setBodyContext) OnHttpResponseBody(bodySize int, endOfStream bool) ty return types.ActionContinue } +// echoBodyContext implements types.HttpContext. type echoBodyContext struct { // Embed the default plugin context // so that you don't need to reimplement all the methods by yourself. @@ -191,7 +195,7 @@ type echoBodyContext struct { totalRequestBodySize int } -// Override types.DefaultHttpContext. +// OnHttpRequestBody implements types.HttpContext. func (ctx *echoBodyContext) OnHttpRequestBody(bodySize int, endOfStream bool) types.Action { ctx.totalRequestBodySize = bodySize if !endOfStream { diff --git a/examples/http_body_chunk/main.go b/examples/http_body_chunk/main.go index 84b15d42..62e53eee 100644 --- a/examples/http_body_chunk/main.go +++ b/examples/http_body_chunk/main.go @@ -1,4 +1,4 @@ -// Copyright 2020-2021 Tetrate +// Copyright 2020-2024 Tetrate // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. @@ -26,33 +26,36 @@ func main() { proxywasm.SetVMContext(&vmContext{}) } +// vmContext implements types.VMContext. type vmContext struct { // Embed the default VM context here, // so that we don't need to reimplement all the methods. types.DefaultVMContext } -// Override types.DefaultVMContext. +// NewPluginContext implements types.VMContext. func (*vmContext) NewPluginContext(contextID uint32) types.PluginContext { return &pluginContext{} } +// pluginContext implements types.PluginContext. type pluginContext struct { // Embed the default plugin context here, // so that we don't need to reimplement all the methods. types.DefaultPluginContext } -// Override types.DefaultPluginContext. +// NewHttpContext implements types.PluginContext. func (ctx *pluginContext) NewHttpContext(contextID uint32) types.HttpContext { return &setBodyContext{} } -// Override types.DefaultPluginContext. +// OnPluginStart implements types.PluginContext. func (ctx *pluginContext) OnPluginStart(pluginConfigurationSize int) types.OnPluginStartStatus { return types.OnPluginStartStatusOK } +// setBodyContext implements types.HttpContext. type setBodyContext struct { // Embed the default root http context here, // so that we don't need to reimplement all the methods. @@ -61,7 +64,7 @@ type setBodyContext struct { receivedChunks int } -// Override types.DefaultHttpContext. +// OnHttpRequestBody implements types.HttpContext. func (ctx *setBodyContext) OnHttpRequestBody(bodySize int, endOfStream bool) types.Action { proxywasm.LogInfof("OnHttpRequestBody called. BodySize: %d, totalRequestBodyReadSize: %d, endOfStream: %v", bodySize, ctx.totalRequestBodyReadSize, endOfStream) @@ -87,7 +90,7 @@ func (ctx *setBodyContext) OnHttpRequestBody(bodySize int, endOfStream bool) typ {"powered-by", "proxy-wasm-go-sdk"}, }, []byte(patternFound), -1); err != nil { proxywasm.LogCriticalf("failed to send local response: %v", err) - proxywasm.ResumeHttpRequest() + _ = proxywasm.ResumeHttpRequest() } else { proxywasm.LogInfo("local 403 response sent") } diff --git a/examples/http_headers/main.go b/examples/http_headers/main.go index ca3d5301..fc6a2ed4 100644 --- a/examples/http_headers/main.go +++ b/examples/http_headers/main.go @@ -1,4 +1,4 @@ -// Copyright 2020-2021 Tetrate +// Copyright 2020-2024 Tetrate // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. @@ -27,17 +27,19 @@ func main() { proxywasm.SetVMContext(&vmContext{}) } +// vmContext implements types.VMContext. type vmContext struct { // Embed the default VM context here, // so that we don't need to reimplement all the methods. types.DefaultVMContext } -// Override types.DefaultVMContext. +// NewPluginContext implements types.VMContext. func (*vmContext) NewPluginContext(contextID uint32) types.PluginContext { return &pluginContext{} } +// pluginContext implements types.PluginContext. type pluginContext struct { // Embed the default plugin context here, // so that we don't need to reimplement all the methods. @@ -49,7 +51,7 @@ type pluginContext struct { headerValue string } -// Override types.DefaultPluginContext. +// NewHttpContext implements types.PluginContext. func (p *pluginContext) NewHttpContext(contextID uint32) types.HttpContext { return &httpHeaders{ contextID: contextID, @@ -58,6 +60,7 @@ func (p *pluginContext) NewHttpContext(contextID uint32) types.HttpContext { } } +// OnPluginStart implements types.PluginContext. func (p *pluginContext) OnPluginStart(pluginConfigurationSize int) types.OnPluginStartStatus { proxywasm.LogDebug("loading plugin config") data, err := proxywasm.GetPluginConfiguration() @@ -88,6 +91,7 @@ func (p *pluginContext) OnPluginStart(pluginConfigurationSize int) types.OnPlugi return types.OnPluginStartStatusOK } +// httpHeaders implements types.HttpContext. type httpHeaders struct { // Embed the default http context here, // so that we don't need to reimplement all the methods. @@ -97,7 +101,7 @@ type httpHeaders struct { headerValue string } -// Override types.DefaultHttpContext. +// OnHttpRequestHeaders implements types.HttpContext. func (ctx *httpHeaders) OnHttpRequestHeaders(numHeaders int, endOfStream bool) types.Action { err := proxywasm.ReplaceHttpRequestHeader("test", "best") if err != nil { @@ -115,7 +119,7 @@ func (ctx *httpHeaders) OnHttpRequestHeaders(numHeaders int, endOfStream bool) t return types.ActionContinue } -// Override types.DefaultHttpContext. +// OnHttpResponseHeaders implements types.HttpContext. func (ctx *httpHeaders) OnHttpResponseHeaders(_ int, _ bool) types.Action { proxywasm.LogInfof("adding header: %s=%s", ctx.headerName, ctx.headerValue) @@ -143,7 +147,7 @@ func (ctx *httpHeaders) OnHttpResponseHeaders(_ int, _ bool) types.Action { return types.ActionContinue } -// Override types.DefaultHttpContext. +// OnHttpStreamDone implements types.HttpContext. func (ctx *httpHeaders) OnHttpStreamDone() { proxywasm.LogInfof("%d finished", ctx.contextID) } diff --git a/examples/http_routing/main.go b/examples/http_routing/main.go index 371aa101..89ab9b9e 100644 --- a/examples/http_routing/main.go +++ b/examples/http_routing/main.go @@ -1,4 +1,4 @@ -// Copyright 2020-2021 Tetrate +// Copyright 2020-2024 Tetrate // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. @@ -26,17 +26,19 @@ func main() { proxywasm.SetVMContext(&vmContext{}) } +// vmContext implements types.VMContext. type vmContext struct { // Embed the default VM context here, // so that we don't need to reimplement all the methods. types.DefaultVMContext } -// Override types.DefaultVMContext. +// NewPluginContext implements types.VMContext. func (*vmContext) NewPluginContext(contextID uint32) types.PluginContext { return &pluginContext{} } +// pluginContext implements types.PluginContext. type pluginContext struct { // Embed the default plugin context here, // so that we don't need to reimplement all the methods. @@ -45,7 +47,7 @@ type pluginContext struct { diceOverride uint32 // For unit test } -// Override types.DefaultPluginContext. +// OnPluginStart implements types.PluginContext. func (ctx *pluginContext) OnPluginStart(pluginConfigurationSize int) types.OnPluginStartStatus { data, err := proxywasm.GetPluginConfiguration() if err != nil && err != types.ErrorStatusNotFound { @@ -62,11 +64,12 @@ func (ctx *pluginContext) OnPluginStart(pluginConfigurationSize int) types.OnPlu return types.OnPluginStartStatusOK } -// Override types.DefaultPluginContext. +// NewHttpContext implements types.PluginContext. func (ctx *pluginContext) NewHttpContext(contextID uint32) types.HttpContext { return &httpRouting{diceOverride: ctx.diceOverride} } +// httpRouting implements types.HttpContext. type httpRouting struct { // Embed the default http context here, // so that we don't need to reimplement all the methods. @@ -82,7 +85,7 @@ func dice() uint32 { return binary.LittleEndian.Uint32(buf) } -// Override types.DefaultHttpContext. +// OnHttpRequestHeaders implements types.HttpContext. func (ctx *httpRouting) OnHttpRequestHeaders(numHeaders int, endOfStream bool) types.Action { // Randomly routing to the canary cluster. var value uint32 diff --git a/examples/json_validation/main.go b/examples/json_validation/main.go index d8a3402e..1d688e90 100644 --- a/examples/json_validation/main.go +++ b/examples/json_validation/main.go @@ -16,19 +16,19 @@ func main() { proxywasm.SetVMContext(&vmContext{}) } -// vmContext implements types.VMContext interface of proxy-wasm-go SDK. +// vmContext implements types.VMContext. type vmContext struct { // Embed the default VM context here, // so that we don't need to reimplement all the methods. types.DefaultVMContext } -// Override types.DefaultVMContext. +// NewPluginContext implements types.VMContext. func (*vmContext) NewPluginContext(contextID uint32) types.PluginContext { return &pluginContext{} } -// pluginContext implements types.PluginContext interface of proxy-wasm-go SDK. +// pluginContext implements types.PluginContext. type pluginContext struct { // Embed the default plugin context here, // so that we don't need to reimplement all the methods. @@ -43,7 +43,7 @@ type pluginConfiguration struct { requiredKeys []string } -// Override types.DefaultPluginContext. +// OnPluginStart implements types.PluginContext. func (ctx *pluginContext) OnPluginStart(pluginConfigurationSize int) types.OnPluginStartStatus { data, err := proxywasm.GetPluginConfiguration() if err != nil && err != types.ErrorStatusNotFound { @@ -59,7 +59,7 @@ func (ctx *pluginContext) OnPluginStart(pluginConfigurationSize int) types.OnPlu return types.OnPluginStartStatusOK } -// parsePluginConfiguration parses the json plugin confiuration data and returns pluginConfiguration. +// parsePluginConfiguration parses the json plugin configuration data and returns pluginConfiguration. // Note that this parses the json data by gjson, since TinyGo doesn't support encoding/json. // You can also try https://github.com/mailru/easyjson, which supports decoding to a struct. func parsePluginConfiguration(data []byte) (pluginConfiguration, error) { @@ -81,12 +81,12 @@ func parsePluginConfiguration(data []byte) (pluginConfiguration, error) { return *config, nil } -// Override types.DefaultPluginContext. +// NewHttpContext implements types.PluginContext. func (ctx *pluginContext) NewHttpContext(contextID uint32) types.HttpContext { return &payloadValidationContext{requiredKeys: ctx.configuration.requiredKeys} } -// payloadValidationContext implements types.HttpContext interface of proxy-wasm-go SDK. +// payloadValidationContext implements types.HttpContext. type payloadValidationContext struct { // Embed the default root http context here, // so that we don't need to reimplement all the methods. @@ -95,9 +95,7 @@ type payloadValidationContext struct { requiredKeys []string } -var _ types.HttpContext = (*payloadValidationContext)(nil) - -// Override types.DefaultHttpContext. +// OnHttpRequestHeaders implements types.HttpContext. func (*payloadValidationContext) OnHttpRequestHeaders(numHeaders int, _ bool) types.Action { contentType, err := proxywasm.GetHttpRequestHeader("content-type") if err != nil || contentType != "application/json" { @@ -113,7 +111,7 @@ func (*payloadValidationContext) OnHttpRequestHeaders(numHeaders int, _ bool) ty return types.ActionContinue } -// Override types.DefaultHttpContext. +// OnHttpRequestBody implements types.HttpContext. func (ctx *payloadValidationContext) OnHttpRequestBody(bodySize int, endOfStream bool) types.Action { ctx.totalRequestBodySize += bodySize if !endOfStream { diff --git a/examples/metrics/main.go b/examples/metrics/main.go index f502f07e..c7695df1 100644 --- a/examples/metrics/main.go +++ b/examples/metrics/main.go @@ -1,4 +1,4 @@ -// Copyright 2020-2021 Tetrate +// Copyright 2020-2024 Tetrate // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. @@ -25,28 +25,31 @@ func main() { proxywasm.SetVMContext(&vmContext{}) } +// vmContext implements types.VMContext. type vmContext struct { // Embed the default VM context here, // so that we don't need to reimplement all the methods. types.DefaultVMContext } -// Override types.DefaultVMContext. +// NewPluginContext implements types.VMContext. func (*vmContext) NewPluginContext(contextID uint32) types.PluginContext { return &metricPluginContext{} } +// metricPluginContext implements types.PluginContext. type metricPluginContext struct { // Embed the default plugin context here, // so that we don't need to reimplement all the methods. types.DefaultPluginContext } -// Override types.DefaultPluginContext. +// NewHttpContext implements types.PluginContext. func (ctx *metricPluginContext) NewHttpContext(contextID uint32) types.HttpContext { return &metricHttpContext{} } +// metricHttpContext implements types.HttpContext. type metricHttpContext struct { // Embed the default http context here, // so that we don't need to reimplement all the methods. @@ -58,9 +61,11 @@ const ( customHeaderValueTagKey = "value" ) +// counters is a map from custom header value to a counter metric. +// Note that Proxy-Wasm plugins are single threaded, so no need to use a lock. var counters = map[string]proxywasm.MetricCounter{} -// Override types.DefaultHttpContext. +// OnHttpRequestHeaders implements types.HttpContext. func (ctx *metricHttpContext) OnHttpRequestHeaders(numHeaders int, endOfStream bool) types.Action { customHeaderValue, err := proxywasm.GetHttpRequestHeader(customHeaderKey) if err == nil { diff --git a/examples/multiple_dispatches/main.go b/examples/multiple_dispatches/main.go index 79ba30b3..87e2ac30 100644 --- a/examples/multiple_dispatches/main.go +++ b/examples/multiple_dispatches/main.go @@ -1,4 +1,4 @@ -// Copyright 2020-2021 Tetrate +// Copyright 2020-2024 Tetrate // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. @@ -27,28 +27,31 @@ func main() { proxywasm.SetVMContext(&vmContext{}) } +// vmContext implements types.VMContext. type vmContext struct { // Embed the default VM context here, // so that we don't need to reimplement all the methods. types.DefaultVMContext } -// Override types.DefaultVMContext. +// NewPluginContext implements types.VMContext. func (*vmContext) NewPluginContext(contextID uint32) types.PluginContext { return &pluginContext{} } +// pluginContext implements types.PluginContext. type pluginContext struct { // Embed the default plugin context here, // so that we don't need to reimplement all the methods. types.DefaultPluginContext } -// Override types.DefaultPluginContext. +// NewHttpContext implements types.PluginContext. func (*pluginContext) NewHttpContext(contextID uint32) types.HttpContext { return &httpContext{contextID: contextID} } +// httpContext implements types.HttpContext. type httpContext struct { // Embed the default http context here, // so that we don't need to reimplement all the methods. @@ -61,7 +64,7 @@ type httpContext struct { const totalDispatchNum = 10 -// Override types.DefaultHttpContext. +// OnHttpResponseHeaders implements types.HttpContext. func (ctx *httpContext) OnHttpResponseHeaders(numHeaders int, endOfStream bool) types.Action { // On each request response, we dispatch the http calls `totalDispatchNum` times. // Note: DispatchHttpCall is asynchronously processed, so each loop is non-blocking. @@ -86,9 +89,9 @@ func (ctx *httpContext) dispatchCallback(numHeaders, bodySize, numTrailers int) if ctx.pendingDispatchedRequest == 0 { // This case, all the dispatched request was processed. // Adds a response header to the original response. - proxywasm.AddHttpResponseHeader("total-dispatched", strconv.Itoa(totalDispatchNum)) + _ = proxywasm.AddHttpResponseHeader("total-dispatched", strconv.Itoa(totalDispatchNum)) // And then contniue the original reponse. - proxywasm.ResumeHttpResponse() + _ = proxywasm.ResumeHttpResponse() proxywasm.LogInfof("response resumed after processed %d dispatched request", totalDispatchNum) } else { proxywasm.LogInfof("pending dispatched requests: %d", ctx.pendingDispatchedRequest) diff --git a/examples/network/main.go b/examples/network/main.go index bcaef31b..12416f76 100644 --- a/examples/network/main.go +++ b/examples/network/main.go @@ -1,4 +1,4 @@ -// Copyright 2020-2021 Tetrate +// Copyright 2020-2024 Tetrate // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. @@ -23,17 +23,19 @@ func main() { proxywasm.SetVMContext(&vmContext{}) } +// vmContext implements types.VMContext. type vmContext struct { // Embed the default VM context here, // so that we don't need to reimplement all the methods. types.DefaultVMContext } -// Override types.DefaultVMContext. +// NewPluginContext implements types.VMContext. func (*vmContext) NewPluginContext(contextID uint32) types.PluginContext { return &pluginContext{counter: proxywasm.DefineCounterMetric("proxy_wasm_go.connection_counter")} } +// pluginContext implements types.PluginContext. type pluginContext struct { // Embed the default plugin context here, // so that we don't need to reimplement all the methods. @@ -41,11 +43,12 @@ type pluginContext struct { counter proxywasm.MetricCounter } -// Override types.DefaultPluginContext. +// NewTcpContext implements types.PluginContext. func (ctx *pluginContext) NewTcpContext(contextID uint32) types.TcpContext { return &networkContext{counter: ctx.counter} } +// networkContext implements types.TcpContext. type networkContext struct { // Embed the default tcp context here, // so that we don't need to reimplement all the methods. @@ -53,13 +56,13 @@ type networkContext struct { counter proxywasm.MetricCounter } -// Override types.DefaultTcpContext. +// OnNewConnection implements types.TcpContext. func (ctx *networkContext) OnNewConnection() types.Action { proxywasm.LogInfo("new connection!") return types.ActionContinue } -// Override types.DefaultTcpContext. +// OnDownstreamData implements types.TcpContext. func (ctx *networkContext) OnDownstreamData(dataSize int, endOfStream bool) types.Action { if dataSize == 0 { return types.ActionContinue @@ -75,13 +78,12 @@ func (ctx *networkContext) OnDownstreamData(dataSize int, endOfStream bool) type return types.ActionContinue } -// Override types.DefaultTcpContext. +// OnDownstreamClose implements types.TcpContext. func (ctx *networkContext) OnDownstreamClose(types.PeerType) { proxywasm.LogInfo("downstream connection close!") - return } -// Override types.DefaultTcpContext. +// OnUpstreamData implements types.TcpContext. func (ctx *networkContext) OnUpstreamData(dataSize int, endOfStream bool) types.Action { if dataSize == 0 { return types.ActionContinue @@ -115,7 +117,7 @@ func (ctx *networkContext) OnUpstreamData(dataSize int, endOfStream bool) types. return types.ActionContinue } -// Override types.DefaultTcpContext. +// OnStreamDone implements types.TcpContext. func (ctx *networkContext) OnStreamDone() { ctx.counter.Increment(1) proxywasm.LogInfo("connection complete!") diff --git a/examples/postpone_requests/main.go b/examples/postpone_requests/main.go index 6ba7e5bb..974e73a8 100644 --- a/examples/postpone_requests/main.go +++ b/examples/postpone_requests/main.go @@ -1,4 +1,4 @@ -// Copyright 2020-2021 Tetrate +// Copyright 2020-2024 Tetrate // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. @@ -25,13 +25,14 @@ func main() { proxywasm.SetVMContext(&vmContext{}) } +// vmContext implements types.VMContext. type vmContext struct { // Embed the default VM context here, // so that we don't need to reimplement all the methods. types.DefaultVMContext } -// Override types.DefaultVMContext. +// NewPluginContext implements types.VMContext. func (*vmContext) NewPluginContext(contextID uint32) types.PluginContext { return &pluginContext{ contextID: contextID, @@ -39,6 +40,7 @@ func (*vmContext) NewPluginContext(contextID uint32) types.PluginContext { } } +// pluginContext implements types.PluginContext. type pluginContext struct { // Embed the default plugin context here, // so that we don't need to reimplement all the methods. @@ -47,7 +49,7 @@ type pluginContext struct { postponed []uint32 } -// Override types.DefaultPluginContext. +// OnPluginStart implements types.PluginContext. func (ctx *pluginContext) OnPluginStart(pluginConfigurationSize int) types.OnPluginStartStatus { if err := proxywasm.SetTickPeriodMilliSeconds(tickMilliseconds); err != nil { proxywasm.LogCriticalf("failed to set tick period: %v", err) @@ -56,18 +58,18 @@ func (ctx *pluginContext) OnPluginStart(pluginConfigurationSize int) types.OnPlu return types.OnPluginStartStatusOK } -// Override types.DefaultPluginContext. +// OnTick implements types.PluginContext. func (ctx *pluginContext) OnTick() { for len(ctx.postponed) > 0 { httpCtxId, tail := ctx.postponed[0], ctx.postponed[1:] proxywasm.LogInfof("resume request with contextID=%v", httpCtxId) - proxywasm.SetEffectiveContext(httpCtxId) - proxywasm.ResumeHttpRequest() + _ = proxywasm.SetEffectiveContext(httpCtxId) + _ = proxywasm.ResumeHttpRequest() ctx.postponed = tail } } -// Override types.DefaultPluginContext. +// NewHttpContext implements types.PluginContext. func (ctx *pluginContext) NewHttpContext(contextID uint32) types.HttpContext { return &httpContext{ contextID: contextID, @@ -75,6 +77,7 @@ func (ctx *pluginContext) NewHttpContext(contextID uint32) types.HttpContext { } } +// httpContext implements types.HttpContext. type httpContext struct { // Embed the default http context here, // so that we don't need to reimplement all the methods. @@ -83,7 +86,7 @@ type httpContext struct { pluginCtx *pluginContext } -// Override types.DefaultHttpContext. +// OnHttpRequestHeaders implements types.HttpContext. func (ctx *httpContext) OnHttpRequestHeaders(numHeaders int, endOfStream bool) types.Action { proxywasm.LogInfof("postpone request with contextID=%d", ctx.contextID) ctx.pluginCtx.postponed = append(ctx.pluginCtx.postponed, ctx.contextID) diff --git a/examples/properties/main.go b/examples/properties/main.go index 4f110e4d..fbc1d5f3 100644 --- a/examples/properties/main.go +++ b/examples/properties/main.go @@ -1,4 +1,4 @@ -// Copyright 2020-2021 Tetrate +// Copyright 2020-2024 Tetrate // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. @@ -23,13 +23,14 @@ func main() { proxywasm.SetVMContext(&vmContext{}) } +// vmContext implements types.VMContext. type vmContext struct { // Embed the default VM context here, // so that we don't need to reimplement all the methods. types.DefaultVMContext } -// Override types.DefaultVMContext. +// NewPluginContext implements types.VMContext. func (*vmContext) NewPluginContext(contextID uint32) types.PluginContext { return &pluginContext{} } @@ -40,11 +41,12 @@ type pluginContext struct { types.DefaultPluginContext } -// Override types.DefaultPluginContext. +// NewHttpContext implements types.PluginContext. func (*pluginContext) NewHttpContext(contextID uint32) types.HttpContext { return &properties{contextID: contextID} } +// properties implements types.HttpContext. type properties struct { // Embed the default http context here, // so that we don't need to reimplement all the methods. @@ -58,7 +60,7 @@ var propertyPrefix = []string{ "envoy.filters.http.wasm", } -// Override types.DefaultHttpContext. +// OnHttpRequestHeaders implements types.HttpContext. func (ctx *properties) OnHttpRequestHeaders(numHeaders int, endOfStream bool) types.Action { auth, err := proxywasm.GetProperty(append(propertyPrefix, "auth")) if err != nil { @@ -86,14 +88,14 @@ func (ctx *properties) OnHttpRequestHeaders(numHeaders int, endOfStream bool) ty // Reject requests without authentication header if !authHeader { - proxywasm.SendHttpResponse(401, nil, nil, 16) + _ = proxywasm.SendHttpResponse(401, nil, nil, 16) return types.ActionPause } return types.ActionContinue } -// Override types.DefaultHttpContext. +// OnHttpStreamDone implements types.HttpContext. func (ctx *properties) OnHttpStreamDone() { proxywasm.LogInfof("%d finished", ctx.contextID) } diff --git a/examples/properties/main_test.go b/examples/properties/main_test.go index 49446eac..80ac20f2 100644 --- a/examples/properties/main_test.go +++ b/examples/properties/main_test.go @@ -34,14 +34,15 @@ func TestProperties_OnHttpRequestHeaders(t *testing.T) { // Check Envoy logs. logs := host.GetInfoLogs() - require.Contains(t, logs, fmt.Sprintf("no auth header for route")) + require.Contains(t, logs, "no auth header for route") require.Contains(t, logs, fmt.Sprintf("%d finished", id)) }) // Set property path := "auth" data := "cookie" - host.SetProperty(append(propertyPrefix, path), []byte(data)) + err := host.SetProperty(append(propertyPrefix, path), []byte(data)) + require.NoError(t, err) // Get property actualData, _ := host.GetProperty(append(propertyPrefix, path)) diff --git a/examples/shared_data/main.go b/examples/shared_data/main.go index a09ed64e..e1168a9a 100644 --- a/examples/shared_data/main.go +++ b/examples/shared_data/main.go @@ -1,4 +1,4 @@ -// Copyright 2020-2021 Tetrate +// Copyright 2020-2024 Tetrate // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. @@ -31,13 +31,15 @@ func main() { } type ( - vmContext struct{} + // vmContext implements types.VMContext. + vmContext struct{} + // pluginContext implements types.PluginContext. pluginContext struct { // Embed the default plugin context here, // so that we don't need to reimplement all the methods. types.DefaultPluginContext } - + // httpContext implements types.HttpContext. httpContext struct { // Embed the default http context here, // so that we don't need to reimplement all the methods. @@ -45,7 +47,7 @@ type ( } ) -// Override types.VMContext. +// OnVMStart implements types.VMContext. func (*vmContext) OnVMStart(vmConfigurationSize int) types.OnVMStartStatus { initialValueBuf := make([]byte, 0) // Empty data to indicate that the data is not initialized. if err := proxywasm.SetSharedData(sharedDataKey, initialValueBuf, 0); err != nil { @@ -54,17 +56,17 @@ func (*vmContext) OnVMStart(vmConfigurationSize int) types.OnVMStartStatus { return types.OnVMStartStatusOK } -// Override types.DefaultVMContext. +// NewPluginContext implements types.VMContext. func (*vmContext) NewPluginContext(contextID uint32) types.PluginContext { return &pluginContext{} } -// Override types.DefaultPluginContext. +// NewHttpContext implements types.PluginContext. func (*pluginContext) NewHttpContext(contextID uint32) types.HttpContext { return &httpContext{} } -// Override types.DefaultHttpContext. +// OnHttpRequestHeaders implements types.HttpContext. func (ctx *httpContext) OnHttpRequestHeaders(numHeaders int, endOfStream bool) types.Action { for { value, err := ctx.incrementData() @@ -78,6 +80,7 @@ func (ctx *httpContext) OnHttpRequestHeaders(numHeaders int, endOfStream bool) t return types.ActionContinue } +// incrementData increments the shared data value by 1. func (ctx *httpContext) incrementData() (uint64, error) { data, cas, err := proxywasm.GetSharedData(sharedDataKey) if err != nil { diff --git a/examples/shared_queue/receiver/main.go b/examples/shared_queue/receiver/main.go index a01b9ec4..41528947 100644 --- a/examples/shared_queue/receiver/main.go +++ b/examples/shared_queue/receiver/main.go @@ -1,4 +1,4 @@ -// Copyright 2020-2021 Tetrate +// Copyright 2020-2024 Tetrate // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. @@ -25,17 +25,19 @@ func main() { proxywasm.SetVMContext(&vmContext{}) } +// vmContext implements types.VMContext. type vmContext struct { // Embed the default VM context here, // so that we don't need to reimplement all the methods. types.DefaultVMContext } -// Override types.DefaultVMContext. +// NewPluginContext implements types.VMContext. func (*vmContext) NewPluginContext(contextID uint32) types.PluginContext { return &receiverPluginContext{contextID: contextID} } +// receiverPluginContext implements types.PluginContext. type receiverPluginContext struct { // Embed the default plugin context here, // so that we don't need to reimplement all the methods. @@ -44,7 +46,7 @@ type receiverPluginContext struct { queueName string } -// Override types.DefaultPluginContext. +// OnPluginStart implements types.PluginContext. func (ctx *receiverPluginContext) OnPluginStart(pluginConfigurationSize int) types.OnPluginStartStatus { // Get Plugin configuration. config, err := proxywasm.GetPluginConfiguration() @@ -63,7 +65,7 @@ func (ctx *receiverPluginContext) OnPluginStart(pluginConfigurationSize int) typ return types.OnPluginStartStatusOK } -// Override types.DefaultPluginContext. +// OnQueueReady implements types.PluginContext. func (ctx *receiverPluginContext) OnQueueReady(queueID uint32) { data, err := proxywasm.DequeueSharedQueue(queueID) switch err { diff --git a/examples/shared_queue/sender/main.go b/examples/shared_queue/sender/main.go index 78424866..8499b6bd 100644 --- a/examples/shared_queue/sender/main.go +++ b/examples/shared_queue/sender/main.go @@ -1,4 +1,4 @@ -// Copyright 2020-2021 Tetrate +// Copyright 2020-2024 Tetrate // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. @@ -23,26 +23,25 @@ import ( "github.com/tetratelabs/proxy-wasm-go-sdk/proxywasm/types" ) -const ( - receiverVMID = "receiver" - queueName = "http_headers" -) +const receiverVMID = "receiver" func main() { proxywasm.SetVMContext(&vmContext{}) } +// vmContext implements types.VMContext. type vmContext struct { // Embed the default VM context here, // so that we don't need to reimplement all the methods. types.DefaultVMContext } -// Override types.DefaultVMContext. +// NewPluginContext implements types.VMContext. func (*vmContext) NewPluginContext(contextID uint32) types.PluginContext { return &senderPluginContext{contextID: contextID} } +// senderPluginContext implements types.PluginContext. type senderPluginContext struct { // Embed the default plugin context here, // so that we don't need to reimplement all the methods. @@ -51,11 +50,7 @@ type senderPluginContext struct { contextID uint32 } -func newPluginContext(uint32) types.PluginContext { - return &senderPluginContext{} -} - -// Override types.DefaultPluginContext. +// OnPluginStart implements types.PluginContext. func (ctx *senderPluginContext) OnPluginStart(pluginConfigurationSize int) types.OnPluginStartStatus { // Get Plugin configuration. config, err := proxywasm.GetPluginConfiguration() @@ -67,7 +62,7 @@ func (ctx *senderPluginContext) OnPluginStart(pluginConfigurationSize int) types return types.OnPluginStartStatusOK } -// Override types.DefaultPluginContext. +// NewHttpContext implements types.PluginContext. func (ctx *senderPluginContext) NewHttpContext(contextID uint32) types.HttpContext { // If this PluginContext is not configured for Http, then return nil. if ctx.config != "http" { @@ -93,6 +88,7 @@ func (ctx *senderPluginContext) NewHttpContext(contextID uint32) types.HttpConte } } +// senderHttpContext implements types.HttpContext. type senderHttpContext struct { // Embed the default http context here, // so that we don't need to reimplement all the methods. @@ -100,7 +96,7 @@ type senderHttpContext struct { contextID, requestHeadersQueueID, responseHeadersQueueID uint32 } -// Override types.DefaultHttpContext. +// OnHttpRequestHeaders implements types.HttpContext. func (ctx *senderHttpContext) OnHttpRequestHeaders(int, bool) types.Action { headers, err := proxywasm.GetHttpRequestHeaders() if err != nil { @@ -117,7 +113,7 @@ func (ctx *senderHttpContext) OnHttpRequestHeaders(int, bool) types.Action { return types.ActionContinue } -// Override types.DefaultHttpContext. +// OnHttpResponseHeaders implements types.HttpContext. func (ctx *senderHttpContext) OnHttpResponseHeaders(int, bool) types.Action { headers, err := proxywasm.GetHttpResponseHeaders() if err != nil { @@ -134,6 +130,7 @@ func (ctx *senderHttpContext) OnHttpResponseHeaders(int, bool) types.Action { return types.ActionContinue } +// NewTcpContext implements types.PluginContext. func (ctx *senderPluginContext) NewTcpContext(contextID uint32) types.TcpContext { // If this PluginContext is not configured for Tcp, then return nil. if ctx.config != "tcp" { @@ -153,6 +150,7 @@ func (ctx *senderPluginContext) NewTcpContext(contextID uint32) types.TcpContext } } +// senderTcpContext implements types.TcpContext. type senderTcpContext struct { types.DefaultTcpContext // Embed the default http context here, @@ -161,6 +159,7 @@ type senderTcpContext struct { contextID uint32 } +// OnUpstreamData implements types.TcpContext. func (ctx *senderTcpContext) OnUpstreamData(dataSize int, endOfStream bool) types.Action { if dataSize == 0 { return types.ActionContinue diff --git a/examples/vm_plugin_configuration/main.go b/examples/vm_plugin_configuration/main.go index 4a31c11e..3ce2adb4 100644 --- a/examples/vm_plugin_configuration/main.go +++ b/examples/vm_plugin_configuration/main.go @@ -1,4 +1,4 @@ -// Copyright 2020-2021 Tetrate +// Copyright 2020-2024 Tetrate // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. @@ -23,8 +23,10 @@ func main() { proxywasm.SetVMContext(&vmContext{}) } +// vmContext implements types.VMContext. type vmContext struct{} +// OnVMStart implements types.VMContext. func (*vmContext) OnVMStart(vmConfigurationSize int) types.OnVMStartStatus { data, err := proxywasm.GetVMConfiguration() if err != nil { @@ -35,18 +37,19 @@ func (*vmContext) OnVMStart(vmConfigurationSize int) types.OnVMStartStatus { return types.OnVMStartStatusOK } -// Implement types.VMContext. +// NewPluginContext implements types.VMContext. func (*vmContext) NewPluginContext(uint32) types.PluginContext { return &pluginContext{} } +// pluginContext implements types.PluginContext. type pluginContext struct { // Embed the default plugin context here, // so that we don't need to reimplement all the methods. types.DefaultPluginContext } -// Override types.DefaultPluginContext. +// OnPluginStart implements types.PluginContext. func (ctx pluginContext) OnPluginStart(pluginConfigurationSize int) types.OnPluginStartStatus { data, err := proxywasm.GetPluginConfiguration() if err != nil { diff --git a/proxywasm/entrypoint.go b/proxywasm/entrypoint.go index 8f8cf2ca..758d47e5 100644 --- a/proxywasm/entrypoint.go +++ b/proxywasm/entrypoint.go @@ -1,4 +1,4 @@ -// Copyright 2020-2021 Tetrate +// Copyright 2020-2024 Tetrate // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. diff --git a/proxywasm/hostcall.go b/proxywasm/hostcall.go index 5dc14ad9..04013a86 100644 --- a/proxywasm/hostcall.go +++ b/proxywasm/hostcall.go @@ -1,4 +1,4 @@ -// Copyright 2020-2021 Tetrate +// Copyright 2020-2024 Tetrate // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. diff --git a/proxywasm/hostcall_test.go b/proxywasm/hostcall_test.go index 5dddce5a..ebc9de4b 100644 --- a/proxywasm/hostcall_test.go +++ b/proxywasm/hostcall_test.go @@ -1,4 +1,4 @@ -// Copyright 2021 Tetrate +// Copyright 2024 Tetrate // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. diff --git a/proxywasm/internal/abi_callback_alloc.go b/proxywasm/internal/abi_callback_alloc.go index c9566cb0..1c5ec830 100644 --- a/proxywasm/internal/abi_callback_alloc.go +++ b/proxywasm/internal/abi_callback_alloc.go @@ -1,4 +1,4 @@ -// Copyright 2020-2021 Tetrate +// Copyright 2020-2024 Tetrate // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. diff --git a/proxywasm/internal/abi_callback_configuration.go b/proxywasm/internal/abi_callback_configuration.go index 60da346f..16071c91 100644 --- a/proxywasm/internal/abi_callback_configuration.go +++ b/proxywasm/internal/abi_callback_configuration.go @@ -1,4 +1,4 @@ -// Copyright 2020-2021 Tetrate +// Copyright 2020-2024 Tetrate // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. diff --git a/proxywasm/internal/abi_callback_configuration_test.go b/proxywasm/internal/abi_callback_configuration_test.go index 903bac43..f56547e4 100644 --- a/proxywasm/internal/abi_callback_configuration_test.go +++ b/proxywasm/internal/abi_callback_configuration_test.go @@ -1,4 +1,4 @@ -// Copyright 2021 Tetrate +// Copyright 2024 Tetrate // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. diff --git a/proxywasm/internal/abi_callback_l4.go b/proxywasm/internal/abi_callback_l4.go index ea3c526a..f9cc9d57 100644 --- a/proxywasm/internal/abi_callback_l4.go +++ b/proxywasm/internal/abi_callback_l4.go @@ -1,4 +1,4 @@ -// Copyright 2020-2021 Tetrate +// Copyright 2020-2024 Tetrate // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. diff --git a/proxywasm/internal/abi_callback_l4_test.go b/proxywasm/internal/abi_callback_l4_test.go index c6eff1b9..af57666c 100644 --- a/proxywasm/internal/abi_callback_l4_test.go +++ b/proxywasm/internal/abi_callback_l4_test.go @@ -1,4 +1,4 @@ -// Copyright 2021 Tetrate +// Copyright 2024 Tetrate // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. diff --git a/proxywasm/internal/abi_callback_l7.go b/proxywasm/internal/abi_callback_l7.go index 43d000d3..83960d68 100644 --- a/proxywasm/internal/abi_callback_l7.go +++ b/proxywasm/internal/abi_callback_l7.go @@ -1,4 +1,4 @@ -// Copyright 2020-2021 Tetrate +// Copyright 2020-2024 Tetrate // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. diff --git a/proxywasm/internal/abi_callback_l7_test.go b/proxywasm/internal/abi_callback_l7_test.go index 02a0969c..241c7cca 100644 --- a/proxywasm/internal/abi_callback_l7_test.go +++ b/proxywasm/internal/abi_callback_l7_test.go @@ -1,4 +1,4 @@ -// Copyright 2021 Tetrate +// Copyright 2024 Tetrate // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. diff --git a/proxywasm/internal/abi_callback_lifecycle.go b/proxywasm/internal/abi_callback_lifecycle.go index 3f2ebac8..c28fee5a 100644 --- a/proxywasm/internal/abi_callback_lifecycle.go +++ b/proxywasm/internal/abi_callback_lifecycle.go @@ -1,4 +1,4 @@ -// Copyright 2020-2021 Tetrate +// Copyright 2020-2024 Tetrate // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. diff --git a/proxywasm/internal/abi_callback_lifecycle_test.go b/proxywasm/internal/abi_callback_lifecycle_test.go index 2c2d652c..18688a7c 100644 --- a/proxywasm/internal/abi_callback_lifecycle_test.go +++ b/proxywasm/internal/abi_callback_lifecycle_test.go @@ -1,4 +1,4 @@ -// Copyright 2021 Tetrate +// Copyright 2024 Tetrate // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. diff --git a/proxywasm/internal/abi_callback_queue.go b/proxywasm/internal/abi_callback_queue.go index a5b82313..6b8021ab 100644 --- a/proxywasm/internal/abi_callback_queue.go +++ b/proxywasm/internal/abi_callback_queue.go @@ -1,4 +1,4 @@ -// Copyright 2020-2021 Tetrate +// Copyright 2020-2024 Tetrate // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. diff --git a/proxywasm/internal/abi_callback_queue_test.go b/proxywasm/internal/abi_callback_queue_test.go index 9135f212..50520277 100644 --- a/proxywasm/internal/abi_callback_queue_test.go +++ b/proxywasm/internal/abi_callback_queue_test.go @@ -1,4 +1,4 @@ -// Copyright 2021 Tetrate +// Copyright 2024 Tetrate // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. diff --git a/proxywasm/internal/abi_callback_test_export.go b/proxywasm/internal/abi_callback_test_export.go index 51983d16..1c18d4ee 100644 --- a/proxywasm/internal/abi_callback_test_export.go +++ b/proxywasm/internal/abi_callback_test_export.go @@ -1,4 +1,4 @@ -// Copyright 2020-2021 Tetrate +// Copyright 2020-2024 Tetrate // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. diff --git a/proxywasm/internal/abi_callback_timers.go b/proxywasm/internal/abi_callback_timers.go index 6c00ac46..2f4061e2 100644 --- a/proxywasm/internal/abi_callback_timers.go +++ b/proxywasm/internal/abi_callback_timers.go @@ -1,4 +1,4 @@ -// Copyright 2020-2021 Tetrate +// Copyright 2020-2024 Tetrate // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. diff --git a/proxywasm/internal/abi_callback_timers_test.go b/proxywasm/internal/abi_callback_timers_test.go index b6fe6644..80f37aac 100644 --- a/proxywasm/internal/abi_callback_timers_test.go +++ b/proxywasm/internal/abi_callback_timers_test.go @@ -1,4 +1,4 @@ -// Copyright 2021 Tetrate +// Copyright 2024 Tetrate // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. diff --git a/proxywasm/internal/abi_callback_version.go b/proxywasm/internal/abi_callback_version.go index 82461dc3..ade6b8fe 100644 --- a/proxywasm/internal/abi_callback_version.go +++ b/proxywasm/internal/abi_callback_version.go @@ -1,4 +1,4 @@ -// Copyright 2020-2021 Tetrate +// Copyright 2020-2024 Tetrate // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. diff --git a/proxywasm/internal/abi_hostcalls.go b/proxywasm/internal/abi_hostcalls.go index 152c401a..eb5d4ea4 100644 --- a/proxywasm/internal/abi_hostcalls.go +++ b/proxywasm/internal/abi_hostcalls.go @@ -1,4 +1,4 @@ -// Copyright 2020-2021 Tetrate +// Copyright 2020-2024 Tetrate // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. diff --git a/proxywasm/internal/abi_hostcalls_mock.go b/proxywasm/internal/abi_hostcalls_mock.go index 68c151de..edc9a54a 100644 --- a/proxywasm/internal/abi_hostcalls_mock.go +++ b/proxywasm/internal/abi_hostcalls_mock.go @@ -1,4 +1,4 @@ -// Copyright 2020-2021 Tetrate +// Copyright 2020-2024 Tetrate // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. diff --git a/proxywasm/internal/hostcall_utils_go.go b/proxywasm/internal/hostcall_utils_go.go index 3e8f611f..8d2c48a1 100644 --- a/proxywasm/internal/hostcall_utils_go.go +++ b/proxywasm/internal/hostcall_utils_go.go @@ -1,4 +1,4 @@ -// Copyright 2020-2021 Tetrate +// Copyright 2020-2024 Tetrate // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. diff --git a/proxywasm/internal/hostcall_utils_test.go b/proxywasm/internal/hostcall_utils_test.go index 7a08eb06..6d1de3a4 100644 --- a/proxywasm/internal/hostcall_utils_test.go +++ b/proxywasm/internal/hostcall_utils_test.go @@ -1,4 +1,4 @@ -// Copyright 2021 Tetrate +// Copyright 2024 Tetrate // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. diff --git a/proxywasm/internal/hostcall_utils_tinygo.go b/proxywasm/internal/hostcall_utils_tinygo.go index 229e658d..b3b8fd96 100644 --- a/proxywasm/internal/hostcall_utils_tinygo.go +++ b/proxywasm/internal/hostcall_utils_tinygo.go @@ -1,4 +1,4 @@ -// Copyright 2020-2021 Tetrate +// Copyright 2020-2024 Tetrate // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. diff --git a/proxywasm/internal/hostcall_utls.go b/proxywasm/internal/hostcall_utls.go index b853fd39..b960ca02 100644 --- a/proxywasm/internal/hostcall_utls.go +++ b/proxywasm/internal/hostcall_utls.go @@ -1,4 +1,4 @@ -// Copyright 2021 Tetrate +// Copyright 2024 Tetrate // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. diff --git a/proxywasm/internal/serde.go b/proxywasm/internal/serde.go index 8355ce44..cb0cd023 100644 --- a/proxywasm/internal/serde.go +++ b/proxywasm/internal/serde.go @@ -1,4 +1,4 @@ -// Copyright 2020-2021 Tetrate +// Copyright 2020-2024 Tetrate // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. diff --git a/proxywasm/internal/serde_test.go b/proxywasm/internal/serde_test.go index f60ba6fd..0339b173 100644 --- a/proxywasm/internal/serde_test.go +++ b/proxywasm/internal/serde_test.go @@ -1,4 +1,4 @@ -// Copyright 2021 Tetrate +// Copyright 2024 Tetrate // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. diff --git a/proxywasm/internal/vmstate.go b/proxywasm/internal/vmstate.go index d9a062ac..b7680ebe 100644 --- a/proxywasm/internal/vmstate.go +++ b/proxywasm/internal/vmstate.go @@ -1,4 +1,4 @@ -// Copyright 2020-2021 Tetrate +// Copyright 2020-2024 Tetrate // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. diff --git a/proxywasm/internal/vmstate_test.go b/proxywasm/internal/vmstate_test.go index 0bec0d65..034183bc 100644 --- a/proxywasm/internal/vmstate_test.go +++ b/proxywasm/internal/vmstate_test.go @@ -1,4 +1,4 @@ -// Copyright 2021 Tetrate +// Copyright 2024 Tetrate // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. diff --git a/proxywasm/internal/vmstate_test_export.go b/proxywasm/internal/vmstate_test_export.go index 7ca78be8..3ef1e933 100644 --- a/proxywasm/internal/vmstate_test_export.go +++ b/proxywasm/internal/vmstate_test_export.go @@ -1,4 +1,4 @@ -// Copyright 2020-2021 Tetrate +// Copyright 2020-2024 Tetrate // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. diff --git a/proxywasm/proxytest/http.go b/proxywasm/proxytest/http.go index 49c66391..ed4eee3d 100644 --- a/proxywasm/proxytest/http.go +++ b/proxywasm/proxytest/http.go @@ -1,4 +1,4 @@ -// Copyright 2020-2021 Tetrate +// Copyright 2020-2024 Tetrate // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. diff --git a/proxywasm/proxytest/network.go b/proxywasm/proxytest/network.go index 6254cee6..f9884ac4 100644 --- a/proxywasm/proxytest/network.go +++ b/proxywasm/proxytest/network.go @@ -1,4 +1,4 @@ -// Copyright 2020-2021 Tetrate +// Copyright 2020-2024 Tetrate // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. diff --git a/proxywasm/proxytest/option.go b/proxywasm/proxytest/option.go index 7aaaf724..1ab53962 100644 --- a/proxywasm/proxytest/option.go +++ b/proxywasm/proxytest/option.go @@ -1,4 +1,4 @@ -// Copyright 2021 Tetrate +// Copyright 2024 Tetrate // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. diff --git a/proxywasm/proxytest/proxytest.go b/proxywasm/proxytest/proxytest.go index 93a7afdf..bb397db7 100644 --- a/proxywasm/proxytest/proxytest.go +++ b/proxywasm/proxytest/proxytest.go @@ -1,4 +1,4 @@ -// Copyright 2021 Tetrate +// Copyright 2024 Tetrate // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. diff --git a/proxywasm/proxytest/root.go b/proxywasm/proxytest/root.go index f4797ada..c07d8288 100644 --- a/proxywasm/proxytest/root.go +++ b/proxywasm/proxytest/root.go @@ -1,4 +1,4 @@ -// Copyright 2020-2021 Tetrate +// Copyright 2020-2024 Tetrate // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. diff --git a/proxywasm/types/context.go b/proxywasm/types/context.go index a70b234d..577749ff 100644 --- a/proxywasm/types/context.go +++ b/proxywasm/types/context.go @@ -1,4 +1,4 @@ -// Copyright 2020-2021 Tetrate +// Copyright 2020-2024 Tetrate // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. diff --git a/proxywasm/types/types.go b/proxywasm/types/types.go index d4e86e4b..c94095a8 100644 --- a/proxywasm/types/types.go +++ b/proxywasm/types/types.go @@ -1,4 +1,4 @@ -// Copyright 2020-2021 Tetrate +// Copyright 2020-2024 Tetrate // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License.