Skip to content
This repository has been archived by the owner on Jul 28, 2024. It is now read-only.

Commit

Permalink
fix: call setActiveContext in onTick. (#202)
Browse files Browse the repository at this point in the history
Signed-off-by: Takeshi Yoneda <[email protected]>
  • Loading branch information
mathetake authored Jul 20, 2021
1 parent 9f11457 commit 91d182b
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 12 deletions.
5 changes: 4 additions & 1 deletion e2e/e2e_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,10 @@ func Test_dispatch_call_on_tick(t *testing.T) {
defer kill()
var count int = 1
require.Eventually(t, func() bool {
if strings.Contains(stdErr.String(), fmt.Sprintf("called! %d", count)) {
if checkMessage(stdErr.String(), []string{
fmt.Sprintf("called %d for contextID=1", count),
fmt.Sprintf("called %d for contextID=2", count),
}, nil) {
count++
}
return count == 6
Expand Down
17 changes: 17 additions & 0 deletions examples/dispatch_call_on_tick/envoy.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,23 @@ static_resources:
type_url: type.googleapis.com/envoy.extensions.filters.http.wasm.v3.Wasm
value:
config:
configuration:
"@type": type.googleapis.com/google.protobuf.StringValue
value: "plugin1"
vm_config:
runtime: "envoy.wasm.runtime.v8"
code:
local:
filename: "./examples/dispatch_call_on_tick/main.go.wasm"
- name: envoy.filters.http.wasm
typed_config:
"@type": type.googleapis.com/udpa.type.v1.TypedStruct
type_url: type.googleapis.com/envoy.extensions.filters.http.wasm.v3.Wasm
value:
config:
configuration:
"@type": type.googleapis.com/google.protobuf.StringValue
value: "plugin2"
vm_config:
runtime: "envoy.wasm.runtime.v8"
code:
Expand Down
18 changes: 8 additions & 10 deletions examples/dispatch_call_on_tick/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import (
"github.com/tetratelabs/proxy-wasm-go-sdk/proxywasm/types"
)

const tickMilliseconds uint32 = 1
const tickMilliseconds uint32 = 100

func main() {
proxywasm.SetVMContext(&vmContext{})
Expand All @@ -41,6 +41,8 @@ type pluginContext struct {
// so that we don't need to reimplement all the methods.
types.DefaultPluginContext
contextID uint32
callBack func(numHeaders, bodySize, numTrailers int)
cnt int
}

// Override types.DefaultPluginContext.
Expand All @@ -50,6 +52,10 @@ func (ctx *pluginContext) OnPluginStart(pluginConfigurationSize int) types.OnPlu
return types.OnPluginStartStatusFailed
}
proxywasm.LogInfof("set tick period milliseconds: %d", tickMilliseconds)
ctx.callBack = func(numHeaders, bodySize, numTrailers int) {
ctx.cnt++
proxywasm.LogInfof("called %d for contextID=%d", ctx.cnt, ctx.contextID)
}
return types.OnPluginStartStatusOK
}

Expand All @@ -58,15 +64,7 @@ func (ctx *pluginContext) OnTick() {
hs := [][2]string{
{":method", "GET"}, {":authority", "some_authority"}, {":path", "/path/to/service"}, {"accept", "*/*"},
}
if _, err := proxywasm.DispatchHttpCall("web_service", hs, nil, nil,
5000, callback); err != nil {
if _, err := proxywasm.DispatchHttpCall("web_service", hs, nil, nil, 5000, ctx.callBack); err != nil {
proxywasm.LogCriticalf("dispatch httpcall failed: %v", err)
}
}

var cnt int

func callback(numHeaders, bodySize, numTrailers int) {
cnt++
proxywasm.LogInfof("called! %d", cnt)
}
2 changes: 1 addition & 1 deletion examples/dispatch_call_on_tick/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func TestPluginContext_OnTick(t *testing.T) {
host.CallOnHttpCallResponse(attrs[0].CalloutID, nil, nil, nil)
// Check Envoy logs.
logs := host.GetInfoLogs()
require.Contains(t, logs, fmt.Sprintf("called! %d", i))
require.Contains(t, logs, fmt.Sprintf("called %d for contextID=%d", i, proxytest.PluginContextID))
}

}
Expand Down
1 change: 1 addition & 0 deletions proxywasm/internal/abi_callback_timers.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,6 @@ func proxyOnTick(pluginContextID uint32) {
if !ok {
panic("invalid root_context_id")
}
currentState.setActiveContextID(pluginContextID)
ctx.context.OnTick()
}

0 comments on commit 91d182b

Please sign in to comment.