Skip to content
This repository was archived by the owner on Apr 24, 2025. It is now read-only.

Commit 6b29a49

Browse files
authored
Have NewStreamContext and NewHttpContext in RootContext (#128)
This is kind of an improvement from #93 in favor of proxy-wasm/proxy-wasm-rust-sdk#34 and proxy-wasm/proxy-wasm-rust-sdk#67 Note that this is a breaking change, though the fix is trivial. Signed-off-by: Takeshi Yoneda [email protected]
1 parent cfa904a commit 6b29a49

File tree

24 files changed

+178
-186
lines changed

24 files changed

+178
-186
lines changed

e2e/e2e_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -299,12 +299,12 @@ func sharedData(t *testing.T, ps envoyPorts, stdErr *bytes.Buffer) {
299299

300300
func sharedQueue(t *testing.T, ps envoyPorts, stdErr *bytes.Buffer) {
301301
req, err := http.NewRequest("GET", fmt.Sprintf("http://localhost:%d", ps.endpoint), nil)
302-
require.NoError(t, err, stdErr.String())
302+
require.NoError(t, err)
303303

304304
count := 10
305305
for i := 0; i < count; i++ {
306306
r, err := http.DefaultClient.Do(req)
307-
require.NoError(t, err, stdErr.String())
307+
require.NoError(t, err)
308308
r.Body.Close()
309309
}
310310

examples/configuration_from_root/main.go

Lines changed: 6 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,11 @@ import (
2020

2121
func main() {
2222
proxywasm.SetNewRootContext(newRootContext)
23-
proxywasm.SetNewHttpContext(newHttpContext)
2423
}
2524

2625
type rootContext struct {
2726
// you must embed the default context so that you need not to reimplement all the methods by yourself
2827
proxywasm.DefaultRootContext
29-
3028
config []byte
3129
}
3230

@@ -44,29 +42,14 @@ func (ctx *rootContext) OnPluginStart(pluginConfigurationSize int) bool {
4442
return true
4543
}
4644

45+
func (ctx *rootContext) NewHttpContext(contextID uint32) proxywasm.HttpContext {
46+
ret := &httpContext{config: ctx.config}
47+
proxywasm.LogInfof("read plugin config from root context: %s\n", string(ret.config))
48+
return ret
49+
}
50+
4751
type httpContext struct {
4852
proxywasm.DefaultHttpContext
4953

5054
config []byte
5155
}
52-
53-
func newHttpContext(rootContextID, contextID uint32) proxywasm.HttpContext {
54-
ctx := &httpContext{}
55-
56-
rootCtx, err := proxywasm.GetRootContextByID(rootContextID)
57-
if err != nil {
58-
proxywasm.LogErrorf("unable to get root context: %v", err)
59-
60-
return ctx
61-
}
62-
63-
exampleRootCtx, ok := rootCtx.(*rootContext)
64-
if !ok {
65-
proxywasm.LogError("could not cast root context")
66-
}
67-
68-
ctx.config = exampleRootCtx.config
69-
70-
proxywasm.LogInfof("plugin config from root context: %s\n", string(ctx.config))
71-
return ctx
72-
}

examples/configuration_from_root/main_test.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@ func TestContext_OnPluginStart(t *testing.T) {
3131

3232
opt := proxytest.NewEmulatorOption().
3333
WithNewRootContext(newRootContext).
34-
WithNewHttpContext(newHttpContext).
3534
WithPluginConfiguration([]byte(pluginConfigData))
3635
host := proxytest.NewHostEmulator(opt)
3736
defer host.Done() // release the emulation lock so that other test cases can insert their own host emulation

examples/http_auth_random/main.go

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,17 @@ import (
2424
const clusterName = "httpbin"
2525

2626
func main() {
27-
proxywasm.SetNewHttpContext(newContext)
27+
proxywasm.SetNewRootContext(newRootContext)
28+
}
29+
30+
type rootContext struct {
31+
proxywasm.DefaultRootContext
32+
}
33+
34+
func newRootContext(uint32) proxywasm.RootContext { return &rootContext{} }
35+
36+
func (*rootContext) NewHttpContext(contextID uint32) proxywasm.HttpContext {
37+
return &httpAuthRandom{contextID: contextID}
2838
}
2939

3040
type httpAuthRandom struct {
@@ -33,10 +43,6 @@ type httpAuthRandom struct {
3343
contextID uint32
3444
}
3545

36-
func newContext(rootContextID, contextID uint32) proxywasm.HttpContext {
37-
return &httpAuthRandom{contextID: contextID}
38-
}
39-
4046
// override default
4147
func (ctx *httpAuthRandom) OnHttpRequestHeaders(numHeaders int, endOfStream bool) types.Action {
4248
hs, err := proxywasm.GetHttpRequestHeaders()

examples/http_auth_random/main_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import (
1212

1313
func TestHttpAuthRandom_OnHttpRequestHeaders(t *testing.T) {
1414
opt := proxytest.NewEmulatorOption().
15-
WithNewHttpContext(newContext)
15+
WithNewRootContext(newRootContext)
1616
host := proxytest.NewHostEmulator(opt)
1717
defer host.Done()
1818

@@ -35,7 +35,7 @@ func TestHttpAuthRandom_OnHttpRequestHeaders(t *testing.T) {
3535

3636
func TestHttpAuthRandom_OnHttpCallResponse(t *testing.T) {
3737
opt := proxytest.NewEmulatorOption().
38-
WithNewHttpContext(newContext)
38+
WithNewRootContext(newRootContext)
3939
host := proxytest.NewHostEmulator(opt)
4040
defer host.Done()
4141

examples/http_body/main.go

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,17 @@ import (
2020
)
2121

2222
func main() {
23-
proxywasm.SetNewHttpContext(newContext)
23+
proxywasm.SetNewRootContext(newContext)
24+
}
25+
26+
type rootContext struct {
27+
proxywasm.DefaultRootContext
28+
}
29+
30+
func newContext(uint32) proxywasm.RootContext { return &rootContext{} }
31+
32+
func (*rootContext) NewHttpContext(contextID uint32) proxywasm.HttpContext {
33+
return &httpBody{contextID: contextID}
2434
}
2535

2636
type httpBody struct {
@@ -29,10 +39,6 @@ type httpBody struct {
2939
contextID uint32
3040
}
3141

32-
func newContext(rootContextID, contextID uint32) proxywasm.HttpContext {
33-
return &httpBody{contextID: contextID}
34-
}
35-
3642
// override
3743
func (ctx *httpBody) OnHttpRequestBody(bodySize int, endOfStream bool) types.Action {
3844
proxywasm.LogInfof("body size: %d", bodySize)

examples/http_body/main_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import (
1111

1212
func TestHttpBody_OnHttpRequestBody(t *testing.T) {
1313
opt := proxytest.NewEmulatorOption().
14-
WithNewHttpContext(newContext)
14+
WithNewRootContext(newContext)
1515
host := proxytest.NewHostEmulator(opt)
1616
defer host.Done()
1717

examples/http_headers/main.go

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,17 @@ import (
2020
)
2121

2222
func main() {
23-
proxywasm.SetNewHttpContext(newContext)
23+
proxywasm.SetNewRootContext(newRootContext)
24+
}
25+
26+
type rootContext struct {
27+
proxywasm.DefaultRootContext
28+
}
29+
30+
func newRootContext(uint32) proxywasm.RootContext { return &rootContext{} }
31+
32+
func (*rootContext) NewHttpContext(contextID uint32) proxywasm.HttpContext {
33+
return &httpHeaders{contextID: contextID}
2434
}
2535

2636
type httpHeaders struct {
@@ -29,10 +39,6 @@ type httpHeaders struct {
2939
contextID uint32
3040
}
3141

32-
func newContext(rootContextID, contextID uint32) proxywasm.HttpContext {
33-
return &httpHeaders{contextID: contextID}
34-
}
35-
3642
// override
3743
func (ctx *httpHeaders) OnHttpRequestHeaders(numHeaders int, endOfStream bool) types.Action {
3844
hs, err := proxywasm.GetHttpRequestHeaders()

examples/http_headers/main_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import (
1313

1414
func TestHttpHeaders_OnHttpRequestHeaders(t *testing.T) {
1515
opt := proxytest.NewEmulatorOption().
16-
WithNewHttpContext(newContext)
16+
WithNewRootContext(newRootContext)
1717
host := proxytest.NewHostEmulator(opt)
1818
defer host.Done()
1919
id := host.HttpFilterInitContext()
@@ -33,7 +33,7 @@ func TestHttpHeaders_OnHttpRequestHeaders(t *testing.T) {
3333

3434
func TestHttpHeaders_OnHttpResponseHeaders(t *testing.T) {
3535
opt := proxytest.NewEmulatorOption().
36-
WithNewHttpContext(newContext)
36+
WithNewRootContext(newRootContext)
3737
host := proxytest.NewHostEmulator(opt)
3838
defer host.Done()
3939
id := host.HttpFilterInitContext()

examples/metrics/main.go

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ import (
2121

2222
func main() {
2323
proxywasm.SetNewRootContext(newRootContext)
24-
proxywasm.SetNewHttpContext(newHttpContext)
2524
}
2625

2726
var counter proxywasm.MetricCounter
@@ -43,15 +42,15 @@ func (ctx *metricRootContext) OnVMStart(vmConfigurationSize int) bool {
4342
return true
4443
}
4544

45+
func (*metricRootContext) NewHttpContext(contextID uint32) proxywasm.HttpContext {
46+
return &metricHttpContext{}
47+
}
48+
4649
type metricHttpContext struct {
4750
// you must embed the default context so that you need not to reimplement all the methods by yourself
4851
proxywasm.DefaultHttpContext
4952
}
5053

51-
func newHttpContext(uint32, uint32) proxywasm.HttpContext {
52-
return &metricHttpContext{}
53-
}
54-
5554
// override
5655
func (ctx *metricHttpContext) OnHttpRequestHeaders(numHeaders int, endOfStream bool) types.Action {
5756
prev := counter.Get()

examples/metrics/main_test.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ import (
1212

1313
func TestMetric(t *testing.T) {
1414
opt := proxytest.NewEmulatorOption().
15-
WithNewHttpContext(newHttpContext).
1615
WithNewRootContext(newRootContext)
1716
host := proxytest.NewHostEmulator(opt)
1817
defer host.Done() // release the host emulation lock so that other test cases can insert their own host emulation

examples/network/main.go

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -26,32 +26,31 @@ var (
2626

2727
func main() {
2828
proxywasm.SetNewRootContext(newRootContext)
29-
proxywasm.SetNewStreamContext(newNetworkContext)
29+
}
30+
31+
func newRootContext(contextID uint32) proxywasm.RootContext {
32+
return &rootContext{}
3033
}
3134

3235
type rootContext struct {
3336
// you must embed the default context so that you need not to reimplement all the methods by yourself
3437
proxywasm.DefaultRootContext
3538
}
3639

37-
func newRootContext(contextID uint32) proxywasm.RootContext {
38-
return &rootContext{}
39-
}
40-
4140
func (ctx *rootContext) OnVMStart(vmConfigurationSize int) bool {
4241
counter = proxywasm.DefineCounterMetric(connectionCounterName)
4342
return true
4443
}
4544

45+
func (ctx *rootContext) NewStreamContext(contextID uint32) proxywasm.StreamContext {
46+
return &networkContext{}
47+
}
48+
4649
type networkContext struct {
4750
// you must embed the default context so that you need not to reimplement all the methods by yourself
4851
proxywasm.DefaultStreamContext
4952
}
5053

51-
func newNetworkContext(rootContextID, contextID uint32) proxywasm.StreamContext {
52-
return &networkContext{}
53-
}
54-
5554
func (ctx *networkContext) OnNewConnection() types.Action {
5655
proxywasm.LogInfo("new connection!")
5756
return types.ActionContinue

examples/network/main_test.go

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ import (
2626

2727
func TestNetwork_OnNewConnection(t *testing.T) {
2828
opt := proxytest.NewEmulatorOption().
29-
WithNewStreamContext(newNetworkContext).
3029
WithNewRootContext(newRootContext)
3130
host := proxytest.NewHostEmulator(opt)
3231
defer host.Done() // release the host emulation lock so that other test cases can insert their own host emulation
@@ -41,7 +40,6 @@ func TestNetwork_OnNewConnection(t *testing.T) {
4140

4241
func TestNetwork_OnDownstreamClose(t *testing.T) {
4342
opt := proxytest.NewEmulatorOption().
44-
WithNewStreamContext(newNetworkContext).
4543
WithNewRootContext(newRootContext)
4644
host := proxytest.NewHostEmulator(opt)
4745
defer host.Done() // release the host emulation lock so that other test cases can insert their own host emulation
@@ -56,7 +54,6 @@ func TestNetwork_OnDownstreamClose(t *testing.T) {
5654

5755
func TestNetwork_OnDownstreamData(t *testing.T) {
5856
opt := proxytest.NewEmulatorOption().
59-
WithNewStreamContext(newNetworkContext).
6057
WithNewRootContext(newRootContext)
6158
host := proxytest.NewHostEmulator(opt)
6259
defer host.Done() // release the host emulation lock so that other test cases can insert their own host emulation
@@ -73,7 +70,6 @@ func TestNetwork_OnDownstreamData(t *testing.T) {
7370

7471
func TestNetwork_OnUpstreamData(t *testing.T) {
7572
opt := proxytest.NewEmulatorOption().
76-
WithNewStreamContext(newNetworkContext).
7773
WithNewRootContext(newRootContext)
7874
host := proxytest.NewHostEmulator(opt)
7975
defer host.Done() // release the host emulation lock so that other test cases can insert their own host emulation
@@ -90,7 +86,6 @@ func TestNetwork_OnUpstreamData(t *testing.T) {
9086

9187
func TestNetwork_counter(t *testing.T) {
9288
opt := proxytest.NewEmulatorOption().
93-
WithNewStreamContext(newNetworkContext).
9489
WithNewRootContext(newRootContext)
9590
host := proxytest.NewHostEmulator(opt)
9691
defer host.Done() // release the host emulation lock so that other test cases can insert their own host emulation

examples/shared_data/main.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ import (
2121

2222
func main() {
2323
proxywasm.SetNewRootContext(newRootContext)
24-
proxywasm.SetNewHttpContext(newHttpContext)
2524
}
2625

2726
type (
@@ -40,10 +39,6 @@ func newRootContext(contextID uint32) proxywasm.RootContext {
4039
return &sharedDataRootContext{}
4140
}
4241

43-
func newHttpContext(rootContextID, contextID uint32) proxywasm.HttpContext {
44-
return &sharedDataHttpContext{}
45-
}
46-
4742
const sharedDataKey = "shared_data_key"
4843

4944
// override
@@ -54,6 +49,11 @@ func (ctx *sharedDataRootContext) OnVMStart(vmConfigurationSize int) bool {
5449
return true
5550
}
5651

52+
// override
53+
func (*sharedDataRootContext) NewHttpContext(contextID uint32) proxywasm.HttpContext {
54+
return &sharedDataHttpContext{}
55+
}
56+
5757
// override
5858
func (ctx *sharedDataHttpContext) OnHttpRequestHeaders(numHeaders int, endOfStream bool) types.Action {
5959
value, cas, err := proxywasm.GetSharedData(sharedDataKey)

examples/shared_data/main_test.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ import (
2626

2727
func TestData(t *testing.T) {
2828
opt := proxytest.NewEmulatorOption().
29-
WithNewHttpContext(newHttpContext).
3029
WithNewRootContext(newRootContext)
3130
host := proxytest.NewHostEmulator(opt)
3231
defer host.Done() // release the host emulation lock so that other test cases can insert their own host emulation

examples/shared_queue/main.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ const (
2626

2727
func main() {
2828
proxywasm.SetNewRootContext(newRootContext)
29-
proxywasm.SetNewHttpContext(newHttpContext)
3029
}
3130

3231
type queueRootContext struct {
@@ -70,15 +69,16 @@ func (ctx *queueRootContext) OnTick() {
7069
}
7170
}
7271

72+
// override
73+
func (*queueRootContext) NewHttpContext(contextID uint32) proxywasm.HttpContext {
74+
return &queueHttpContext{}
75+
}
76+
7377
type queueHttpContext struct {
7478
// you must embed the default context so that you need not to reimplement all the methods by yourself
7579
proxywasm.DefaultHttpContext
7680
}
7781

78-
func newHttpContext(rootContextID, contextID uint32) proxywasm.HttpContext {
79-
return &queueHttpContext{}
80-
}
81-
8282
// override
8383
func (ctx *queueHttpContext) OnHttpRequestHeaders(int, bool) types.Action {
8484
for _, msg := range []string{"hello", "world", "hello", "proxy-wasm"} {

0 commit comments

Comments
 (0)