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

Commit

Permalink
Have NewStreamContext and NewHttpContext in RootContext (#128)
Browse files Browse the repository at this point in the history
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 takeshi@tetrate.io
  • Loading branch information
mathetake authored Mar 9, 2021
1 parent cfa904a commit 6b29a49
Show file tree
Hide file tree
Showing 24 changed files with 178 additions and 186 deletions.
4 changes: 2 additions & 2 deletions e2e/e2e_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -299,12 +299,12 @@ func sharedData(t *testing.T, ps envoyPorts, stdErr *bytes.Buffer) {

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

count := 10
for i := 0; i < count; i++ {
r, err := http.DefaultClient.Do(req)
require.NoError(t, err, stdErr.String())
require.NoError(t, err)
r.Body.Close()
}

Expand Down
29 changes: 6 additions & 23 deletions examples/configuration_from_root/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,11 @@ import (

func main() {
proxywasm.SetNewRootContext(newRootContext)
proxywasm.SetNewHttpContext(newHttpContext)
}

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

config []byte
}

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

func (ctx *rootContext) NewHttpContext(contextID uint32) proxywasm.HttpContext {
ret := &httpContext{config: ctx.config}
proxywasm.LogInfof("read plugin config from root context: %s\n", string(ret.config))
return ret
}

type httpContext struct {
proxywasm.DefaultHttpContext

config []byte
}

func newHttpContext(rootContextID, contextID uint32) proxywasm.HttpContext {
ctx := &httpContext{}

rootCtx, err := proxywasm.GetRootContextByID(rootContextID)
if err != nil {
proxywasm.LogErrorf("unable to get root context: %v", err)

return ctx
}

exampleRootCtx, ok := rootCtx.(*rootContext)
if !ok {
proxywasm.LogError("could not cast root context")
}

ctx.config = exampleRootCtx.config

proxywasm.LogInfof("plugin config from root context: %s\n", string(ctx.config))
return ctx
}
1 change: 0 additions & 1 deletion examples/configuration_from_root/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ func TestContext_OnPluginStart(t *testing.T) {

opt := proxytest.NewEmulatorOption().
WithNewRootContext(newRootContext).
WithNewHttpContext(newHttpContext).
WithPluginConfiguration([]byte(pluginConfigData))
host := proxytest.NewHostEmulator(opt)
defer host.Done() // release the emulation lock so that other test cases can insert their own host emulation
Expand Down
16 changes: 11 additions & 5 deletions examples/http_auth_random/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,17 @@ import (
const clusterName = "httpbin"

func main() {
proxywasm.SetNewHttpContext(newContext)
proxywasm.SetNewRootContext(newRootContext)
}

type rootContext struct {
proxywasm.DefaultRootContext
}

func newRootContext(uint32) proxywasm.RootContext { return &rootContext{} }

func (*rootContext) NewHttpContext(contextID uint32) proxywasm.HttpContext {
return &httpAuthRandom{contextID: contextID}
}

type httpAuthRandom struct {
Expand All @@ -33,10 +43,6 @@ type httpAuthRandom struct {
contextID uint32
}

func newContext(rootContextID, contextID uint32) proxywasm.HttpContext {
return &httpAuthRandom{contextID: contextID}
}

// override default
func (ctx *httpAuthRandom) OnHttpRequestHeaders(numHeaders int, endOfStream bool) types.Action {
hs, err := proxywasm.GetHttpRequestHeaders()
Expand Down
4 changes: 2 additions & 2 deletions examples/http_auth_random/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (

func TestHttpAuthRandom_OnHttpRequestHeaders(t *testing.T) {
opt := proxytest.NewEmulatorOption().
WithNewHttpContext(newContext)
WithNewRootContext(newRootContext)
host := proxytest.NewHostEmulator(opt)
defer host.Done()

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

func TestHttpAuthRandom_OnHttpCallResponse(t *testing.T) {
opt := proxytest.NewEmulatorOption().
WithNewHttpContext(newContext)
WithNewRootContext(newRootContext)
host := proxytest.NewHostEmulator(opt)
defer host.Done()

Expand Down
16 changes: 11 additions & 5 deletions examples/http_body/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,17 @@ import (
)

func main() {
proxywasm.SetNewHttpContext(newContext)
proxywasm.SetNewRootContext(newContext)
}

type rootContext struct {
proxywasm.DefaultRootContext
}

func newContext(uint32) proxywasm.RootContext { return &rootContext{} }

func (*rootContext) NewHttpContext(contextID uint32) proxywasm.HttpContext {
return &httpBody{contextID: contextID}
}

type httpBody struct {
Expand All @@ -29,10 +39,6 @@ type httpBody struct {
contextID uint32
}

func newContext(rootContextID, contextID uint32) proxywasm.HttpContext {
return &httpBody{contextID: contextID}
}

// override
func (ctx *httpBody) OnHttpRequestBody(bodySize int, endOfStream bool) types.Action {
proxywasm.LogInfof("body size: %d", bodySize)
Expand Down
2 changes: 1 addition & 1 deletion examples/http_body/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (

func TestHttpBody_OnHttpRequestBody(t *testing.T) {
opt := proxytest.NewEmulatorOption().
WithNewHttpContext(newContext)
WithNewRootContext(newContext)
host := proxytest.NewHostEmulator(opt)
defer host.Done()

Expand Down
16 changes: 11 additions & 5 deletions examples/http_headers/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,17 @@ import (
)

func main() {
proxywasm.SetNewHttpContext(newContext)
proxywasm.SetNewRootContext(newRootContext)
}

type rootContext struct {
proxywasm.DefaultRootContext
}

func newRootContext(uint32) proxywasm.RootContext { return &rootContext{} }

func (*rootContext) NewHttpContext(contextID uint32) proxywasm.HttpContext {
return &httpHeaders{contextID: contextID}
}

type httpHeaders struct {
Expand All @@ -29,10 +39,6 @@ type httpHeaders struct {
contextID uint32
}

func newContext(rootContextID, contextID uint32) proxywasm.HttpContext {
return &httpHeaders{contextID: contextID}
}

// override
func (ctx *httpHeaders) OnHttpRequestHeaders(numHeaders int, endOfStream bool) types.Action {
hs, err := proxywasm.GetHttpRequestHeaders()
Expand Down
4 changes: 2 additions & 2 deletions examples/http_headers/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (

func TestHttpHeaders_OnHttpRequestHeaders(t *testing.T) {
opt := proxytest.NewEmulatorOption().
WithNewHttpContext(newContext)
WithNewRootContext(newRootContext)
host := proxytest.NewHostEmulator(opt)
defer host.Done()
id := host.HttpFilterInitContext()
Expand All @@ -33,7 +33,7 @@ func TestHttpHeaders_OnHttpRequestHeaders(t *testing.T) {

func TestHttpHeaders_OnHttpResponseHeaders(t *testing.T) {
opt := proxytest.NewEmulatorOption().
WithNewHttpContext(newContext)
WithNewRootContext(newRootContext)
host := proxytest.NewHostEmulator(opt)
defer host.Done()
id := host.HttpFilterInitContext()
Expand Down
9 changes: 4 additions & 5 deletions examples/metrics/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import (

func main() {
proxywasm.SetNewRootContext(newRootContext)
proxywasm.SetNewHttpContext(newHttpContext)
}

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

func (*metricRootContext) NewHttpContext(contextID uint32) proxywasm.HttpContext {
return &metricHttpContext{}
}

type metricHttpContext struct {
// you must embed the default context so that you need not to reimplement all the methods by yourself
proxywasm.DefaultHttpContext
}

func newHttpContext(uint32, uint32) proxywasm.HttpContext {
return &metricHttpContext{}
}

// override
func (ctx *metricHttpContext) OnHttpRequestHeaders(numHeaders int, endOfStream bool) types.Action {
prev := counter.Get()
Expand Down
1 change: 0 additions & 1 deletion examples/metrics/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import (

func TestMetric(t *testing.T) {
opt := proxytest.NewEmulatorOption().
WithNewHttpContext(newHttpContext).
WithNewRootContext(newRootContext)
host := proxytest.NewHostEmulator(opt)
defer host.Done() // release the host emulation lock so that other test cases can insert their own host emulation
Expand Down
17 changes: 8 additions & 9 deletions examples/network/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,32 +26,31 @@ var (

func main() {
proxywasm.SetNewRootContext(newRootContext)
proxywasm.SetNewStreamContext(newNetworkContext)
}

func newRootContext(contextID uint32) proxywasm.RootContext {
return &rootContext{}
}

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

func newRootContext(contextID uint32) proxywasm.RootContext {
return &rootContext{}
}

func (ctx *rootContext) OnVMStart(vmConfigurationSize int) bool {
counter = proxywasm.DefineCounterMetric(connectionCounterName)
return true
}

func (ctx *rootContext) NewStreamContext(contextID uint32) proxywasm.StreamContext {
return &networkContext{}
}

type networkContext struct {
// you must embed the default context so that you need not to reimplement all the methods by yourself
proxywasm.DefaultStreamContext
}

func newNetworkContext(rootContextID, contextID uint32) proxywasm.StreamContext {
return &networkContext{}
}

func (ctx *networkContext) OnNewConnection() types.Action {
proxywasm.LogInfo("new connection!")
return types.ActionContinue
Expand Down
5 changes: 0 additions & 5 deletions examples/network/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ import (

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

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

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

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

func TestNetwork_counter(t *testing.T) {
opt := proxytest.NewEmulatorOption().
WithNewStreamContext(newNetworkContext).
WithNewRootContext(newRootContext)
host := proxytest.NewHostEmulator(opt)
defer host.Done() // release the host emulation lock so that other test cases can insert their own host emulation
Expand Down
10 changes: 5 additions & 5 deletions examples/shared_data/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import (

func main() {
proxywasm.SetNewRootContext(newRootContext)
proxywasm.SetNewHttpContext(newHttpContext)
}

type (
Expand All @@ -40,10 +39,6 @@ func newRootContext(contextID uint32) proxywasm.RootContext {
return &sharedDataRootContext{}
}

func newHttpContext(rootContextID, contextID uint32) proxywasm.HttpContext {
return &sharedDataHttpContext{}
}

const sharedDataKey = "shared_data_key"

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

// override
func (*sharedDataRootContext) NewHttpContext(contextID uint32) proxywasm.HttpContext {
return &sharedDataHttpContext{}
}

// override
func (ctx *sharedDataHttpContext) OnHttpRequestHeaders(numHeaders int, endOfStream bool) types.Action {
value, cas, err := proxywasm.GetSharedData(sharedDataKey)
Expand Down
1 change: 0 additions & 1 deletion examples/shared_data/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ import (

func TestData(t *testing.T) {
opt := proxytest.NewEmulatorOption().
WithNewHttpContext(newHttpContext).
WithNewRootContext(newRootContext)
host := proxytest.NewHostEmulator(opt)
defer host.Done() // release the host emulation lock so that other test cases can insert their own host emulation
Expand Down
10 changes: 5 additions & 5 deletions examples/shared_queue/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ const (

func main() {
proxywasm.SetNewRootContext(newRootContext)
proxywasm.SetNewHttpContext(newHttpContext)
}

type queueRootContext struct {
Expand Down Expand Up @@ -70,15 +69,16 @@ func (ctx *queueRootContext) OnTick() {
}
}

// override
func (*queueRootContext) NewHttpContext(contextID uint32) proxywasm.HttpContext {
return &queueHttpContext{}
}

type queueHttpContext struct {
// you must embed the default context so that you need not to reimplement all the methods by yourself
proxywasm.DefaultHttpContext
}

func newHttpContext(rootContextID, contextID uint32) proxywasm.HttpContext {
return &queueHttpContext{}
}

// override
func (ctx *queueHttpContext) OnHttpRequestHeaders(int, bool) types.Action {
for _, msg := range []string{"hello", "world", "hello", "proxy-wasm"} {
Expand Down
Loading

0 comments on commit 6b29a49

Please sign in to comment.