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

Commit d88237a

Browse files
committed
refactor api
Signed-off-by: mathetake <[email protected]>
1 parent d91a09b commit d88237a

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

45 files changed

+1397
-1191
lines changed

examples/helloworld/main.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ func main() {
2626

2727
type helloWorld struct {
2828
// you must embed the default context so that you need not to reimplement all the methods by yourself
29-
proxywasm.DefaultContext
29+
proxywasm.DefaultRootContext
3030
contextID uint32
3131
}
3232

@@ -37,14 +37,14 @@ func newHelloWorld(contextID uint32) proxywasm.RootContext {
3737
// override
3838
func (ctx *helloWorld) OnVMStart(int) bool {
3939
proxywasm.LogInfo("proxy_on_vm_start from Go!")
40-
if err := proxywasm.HostCallSetTickPeriodMilliSeconds(tickMilliseconds); err != nil {
40+
if err := proxywasm.SetTickPeriodMilliSeconds(tickMilliseconds); err != nil {
4141
proxywasm.LogCriticalf("failed to set tick period: %v", err)
4242
}
4343
return true
4444
}
4545

4646
// override
4747
func (ctx *helloWorld) OnTick() {
48-
t := proxywasm.HostCallGetCurrentTime()
48+
t := proxywasm.GetCurrentTime()
4949
proxywasm.LogInfof("OnTick on %d, it's %d", ctx.contextID, t)
5050
}

examples/helloworld/main_test.go

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ import (
1212

1313
func TestHelloWorld_OnTick(t *testing.T) {
1414
ctx := newHelloWorld(100)
15-
host, done := proxytest.NewRootFilterHost(ctx, nil, nil)
16-
defer done() // release the host emulation lock so that other test cases can insert their own host emulation
15+
host := proxytest.NewHostEmulator(nil, nil, newHelloWorld, nil, nil)
16+
defer host.Done() // release the host emulation lock so that other test cases can insert their own host emulation
1717
ctx.OnTick()
1818

1919
logs := host.GetLogs(types.LogLevelInfo)
@@ -23,9 +23,8 @@ func TestHelloWorld_OnTick(t *testing.T) {
2323
}
2424

2525
func TestHelloWorld_OnVMStart(t *testing.T) {
26-
ctx := newHelloWorld(0)
27-
host, done := proxytest.NewRootFilterHost(ctx, nil, nil)
28-
defer done() // release the host emulation lock so that other test cases can insert their own host emulation
26+
host := proxytest.NewHostEmulator(nil, nil, newHelloWorld, nil, nil)
27+
defer host.Done() // release the host emulation lock so that other test cases can insert their own host emulation
2928

3029
host.StartVM()
3130
logs := host.GetLogs(types.LogLevelInfo)

examples/http_auth_random/main.go

Lines changed: 12 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ func main() {
2929

3030
type httpAuthRandom struct {
3131
// you must embed the default context so that you need not to reimplement all the methods by yourself
32-
proxywasm.DefaultContext
32+
proxywasm.DefaultHttpContext
3333
contextID uint32
3434
}
3535

@@ -39,7 +39,7 @@ func newContext(contextID uint32) proxywasm.HttpContext {
3939

4040
// override default
4141
func (ctx *httpAuthRandom) OnHttpRequestHeaders(int, bool) types.Action {
42-
hs, err := proxywasm.HostCallGetHttpRequestHeaders()
42+
hs, err := proxywasm.GetHttpRequestHeaders()
4343
if err != nil {
4444
proxywasm.LogCriticalf("failed to get request headers: %v", err)
4545
return types.ActionContinue
@@ -48,19 +48,18 @@ func (ctx *httpAuthRandom) OnHttpRequestHeaders(int, bool) types.Action {
4848
proxywasm.LogInfof("request header: %s: %s", h[0], h[1])
4949
}
5050

51-
if _, err := proxywasm.HostCallDispatchHttpCall(
52-
clusterName, hs, "", [][2]string{}, 50000); err != nil {
51+
if _, err := proxywasm.DispatchHttpCall(clusterName, hs, "", [][2]string{},
52+
50000, httpCallResponseCallback); err != nil {
5353
proxywasm.LogCriticalf("dipatch httpcall failed: %v", err)
54+
return types.ActionContinue
5455
}
5556

5657
proxywasm.LogInfof("http call dispatched to %s", clusterName)
57-
5858
return types.ActionPause
5959
}
6060

61-
// override default
62-
func (ctx *httpAuthRandom) OnHttpCallResponse(_ int, bodySize int, _ int) {
63-
hs, err := proxywasm.HostCallGetHttpCallResponseHeaders()
61+
func httpCallResponseCallback(_ int, bodySize int, _ int) {
62+
hs, err := proxywasm.GetHttpCallResponseHeaders()
6463
if err != nil {
6564

6665
proxywasm.LogCriticalf("failed to get response body: %v", err)
@@ -71,34 +70,29 @@ func (ctx *httpAuthRandom) OnHttpCallResponse(_ int, bodySize int, _ int) {
7170
proxywasm.LogInfof("response header from %s: %s: %s", clusterName, h[0], h[1])
7271
}
7372

74-
b, err := proxywasm.HostCallGetHttpCallResponseBody(0, bodySize)
73+
b, err := proxywasm.GetHttpCallResponseBody(0, bodySize)
7574
if err != nil {
7675
proxywasm.LogCriticalf("failed to get response body: %v", err)
77-
proxywasm.HostCallResumeHttpRequest()
76+
proxywasm.ResumeHttpRequest()
7877
return
7978
}
8079

8180
s := fnv.New32a()
8281
if _, err := s.Write(b); err != nil {
8382
proxywasm.LogCriticalf("failed to calculate hash: %v", err)
84-
proxywasm.HostCallResumeHttpRequest()
83+
proxywasm.ResumeHttpRequest()
8584
return
8685
}
8786

8887
if s.Sum32()%2 == 0 {
8988
proxywasm.LogInfo("access granted")
90-
proxywasm.HostCallResumeHttpRequest()
89+
proxywasm.ResumeHttpRequest()
9190
return
9291
}
9392

9493
msg := "access forbidden"
9594
proxywasm.LogInfo(msg)
96-
proxywasm.HostCallSendHttpResponse(403, [][2]string{
95+
proxywasm.SendHttpResponse(403, [][2]string{
9796
{"powered-by", "proxy-wasm-go-sdk!!"},
9897
}, msg)
9998
}
100-
101-
// override default
102-
func (ctx *httpAuthRandom) OnLog() {
103-
proxywasm.LogInfof("%d finished", ctx.contextID)
104-
}

examples/http_auth_random/main_test.go

Lines changed: 28 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,18 @@ import (
1111
)
1212

1313
func TestHttpAuthRandom_OnHttpRequestHeaders(t *testing.T) {
14-
host, done := proxytest.NewHttpFilterHost(newContext)
15-
defer done()
14+
host := proxytest.NewHostEmulator(nil, nil, nil, nil, newContext)
15+
defer host.Done()
1616

17-
id := host.InitContext()
18-
host.PutRequestHeaders(id, [][2]string{{"key", "value"}}) // OnHttpRequestHeaders called
17+
contextID := host.HttpFilterInitContext()
18+
host.HttpFilterPutRequestHeaders(contextID, [][2]string{{"key", "value"}}) // OnHttpRequestHeaders called
1919

20-
require.True(t, host.IsDispatchCalled(id)) // check if http call is dispatched
21-
require.Equal(t, types.ActionPause, host.GetCurrentAction(id)) // check if the current action is pause
20+
attrs := host.GetCalloutAttributesFromContext(contextID)
21+
require.Equal(t, len(attrs), 1) // verify DispatchHttpCall is called
22+
23+
require.Equal(t, "httpbin", attrs[0].Upstream)
24+
require.Equal(t, types.ActionPause,
25+
host.HttpFilterGetCurrentStreamAction(contextID)) // check if the current action is pause
2226

2327
logs := host.GetLogs(types.LogLevelInfo)
2428
require.GreaterOrEqual(t, len(logs), 2)
@@ -28,8 +32,8 @@ func TestHttpAuthRandom_OnHttpRequestHeaders(t *testing.T) {
2832
}
2933

3034
func TestHttpAuthRandom_OnHttpCallResponse(t *testing.T) {
31-
host, done := proxytest.NewHttpFilterHost(newContext)
32-
defer done()
35+
host := proxytest.NewHostEmulator(nil, nil, nil, nil, newContext)
36+
defer host.Done()
3337

3438
// http://httpbin.org/uuid
3539
headers := [][2]string{
@@ -40,20 +44,30 @@ func TestHttpAuthRandom_OnHttpCallResponse(t *testing.T) {
4044
}
4145

4246
// access granted body
43-
id := host.InitContext()
47+
contextID := host.HttpFilterInitContext()
48+
host.HttpFilterPutRequestHeaders(contextID, nil) // OnHttpRequestHeaders called
49+
4450
body := []byte(`{"uuid": "7b10a67a-1c67-4199-835b-cbefcd4a63d4"}`)
45-
host.PutCalloutResponse(id, headers, nil, body)
46-
assert.Nil(t, host.GetSentLocalResponse(id))
51+
attrs := host.GetCalloutAttributesFromContext(contextID)
52+
require.Equal(t, len(attrs), 1) // verify DispatchHttpCall is called
53+
54+
host.PutCalloutResponse(attrs[0].CalloutID, headers, nil, body)
55+
assert.Nil(t, host.HttpFilterGetSentLocalResponse(contextID))
4756

4857
logs := host.GetLogs(types.LogLevelInfo)
4958
require.Greater(t, len(logs), 1)
5059
assert.Equal(t, "access granted", logs[len(logs)-1])
5160

5261
// access denied body
53-
id = host.InitContext()
62+
contextID = host.HttpFilterInitContext()
63+
host.HttpFilterPutRequestHeaders(contextID, nil) // OnHttpRequestHeaders called
64+
5465
body = []byte(`{"uuid": "aaaaaaaa-1c67-4199-835b-cbefcd4a63d4"}`)
55-
host.PutCalloutResponse(id, headers, nil, body)
56-
localResponse := host.GetSentLocalResponse(id) // check local responses
66+
attrs = host.GetCalloutAttributesFromContext(contextID)
67+
require.Equal(t, len(attrs), 1) // verify DispatchHttpCall is called
68+
69+
host.PutCalloutResponse(attrs[0].CalloutID, headers, nil, body)
70+
localResponse := host.HttpFilterGetSentLocalResponse(contextID) // check local responses
5771
assert.NotNil(t, localResponse)
5872
logs = host.GetLogs(types.LogLevelInfo)
5973
assert.Equal(t, "access forbidden", logs[len(logs)-1])

examples/http_headers/main.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ func main() {
2525

2626
type httpHeaders struct {
2727
// you must embed the default context so that you need not to reimplement all the methods by yourself
28-
proxywasm.DefaultContext
28+
proxywasm.DefaultHttpContext
2929
contextID uint32
3030
}
3131

@@ -35,7 +35,7 @@ func newContext(contextID uint32) proxywasm.HttpContext {
3535

3636
// override
3737
func (ctx *httpHeaders) OnHttpRequestHeaders(int, bool) types.Action {
38-
hs, err := proxywasm.HostCallGetHttpRequestHeaders()
38+
hs, err := proxywasm.GetHttpRequestHeaders()
3939
if err != nil {
4040
proxywasm.LogCriticalf("failed to get request headers: %v", err)
4141
}
@@ -48,7 +48,7 @@ func (ctx *httpHeaders) OnHttpRequestHeaders(int, bool) types.Action {
4848

4949
// override
5050
func (ctx *httpHeaders) OnHttpResponseHeaders(int, bool) types.Action {
51-
hs, err := proxywasm.HostCallGetHttpResponseHeaders()
51+
hs, err := proxywasm.GetHttpResponseHeaders()
5252
if err != nil {
5353
proxywasm.LogCriticalf("failed to get request headers: %v", err)
5454
}
@@ -60,6 +60,6 @@ func (ctx *httpHeaders) OnHttpResponseHeaders(int, bool) types.Action {
6060
}
6161

6262
// override
63-
func (ctx *httpHeaders) OnLog() {
63+
func (ctx *httpHeaders) OnHttpStreamDone() {
6464
proxywasm.LogInfof("%d finished", ctx.contextID)
6565
}

examples/http_headers/main_test.go

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package main
22

33
import (
4+
"fmt"
45
"testing"
56

67
"github.com/stretchr/testify/assert"
@@ -11,31 +12,36 @@ import (
1112
)
1213

1314
func TestHttpHeaders_OnHttpRequestHeaders(t *testing.T) {
14-
host, done := proxytest.NewHttpFilterHost(newContext)
15-
defer done()
16-
id := host.InitContext()
15+
host := proxytest.NewHostEmulator(nil, nil, nil, nil, newContext)
16+
defer host.Done()
17+
id := host.HttpFilterInitContext()
1718

1819
hs := [][2]string{{"key1", "value1"}, {"key2", "value2"}}
19-
host.PutRequestHeaders(id, hs) // call OnHttpRequestHeaders
20+
host.HttpFilterPutRequestHeaders(id, hs) // call OnHttpRequestHeaders
21+
22+
host.HttpFilterCompleteHttpStream(id)
2023

2124
logs := host.GetLogs(types.LogLevelInfo)
2225
require.Greater(t, len(logs), 1)
2326

24-
assert.Equal(t, "request header --> key2: value2", logs[len(logs)-1])
25-
assert.Equal(t, "request header --> key1: value1", logs[len(logs)-2])
27+
assert.Equal(t, fmt.Sprintf("%d finished", id), logs[len(logs)-1])
28+
assert.Equal(t, "request header --> key2: value2", logs[len(logs)-2])
29+
assert.Equal(t, "request header --> key1: value1", logs[len(logs)-3])
2630
}
2731

2832
func TestHttpHeaders_OnHttpResponseHeaders(t *testing.T) {
29-
host, done := proxytest.NewHttpFilterHost(newContext)
30-
defer done()
31-
id := host.InitContext()
33+
host := proxytest.NewHostEmulator(nil, nil, nil, nil, newContext)
34+
defer host.Done()
35+
id := host.HttpFilterInitContext()
3236

3337
hs := [][2]string{{"key1", "value1"}, {"key2", "value2"}}
34-
host.PutResponseHeaders(id, hs) // call OnHttpResponseHeaders
38+
host.HttpFilterPutResponseHeaders(id, hs) // call OnHttpRequestHeaders
39+
host.HttpFilterCompleteHttpStream(id) // call OnHttpStreamDone
3540

3641
logs := host.GetLogs(types.LogLevelInfo)
3742
require.Greater(t, len(logs), 1)
3843

39-
assert.Equal(t, "response header <-- key2: value2", logs[len(logs)-1])
40-
assert.Equal(t, "response header <-- key1: value1", logs[len(logs)-2])
44+
assert.Equal(t, fmt.Sprintf("%d finished", id), logs[len(logs)-1])
45+
assert.Equal(t, "response header <-- key2: value2", logs[len(logs)-2])
46+
assert.Equal(t, "response header <-- key1: value1", logs[len(logs)-3])
4147
}

examples/metrics/main.go

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,18 +20,25 @@ import (
2020
)
2121

2222
func main() {
23-
proxywasm.SetNewRootContext(func(uint32) proxywasm.RootContext { return metric{} })
24-
proxywasm.SetNewHttpContext(func(uint32) proxywasm.HttpContext { return metric{} })
23+
proxywasm.SetNewRootContext(newRootContext)
24+
proxywasm.SetNewHttpContext(newHttpContext)
2525
}
2626

2727
var counter proxywasm.MetricCounter
2828

2929
const metricsName = "proxy_wasm_go.request_counter"
3030

31-
type metric struct{ proxywasm.DefaultContext }
31+
type metricRootContext struct {
32+
// you must embed the default context so that you need not to reimplement all the methods by yourself
33+
proxywasm.DefaultRootContext
34+
}
35+
36+
func newRootContext(uint32) proxywasm.RootContext {
37+
return &metricRootContext{}
38+
}
3239

3340
// override
34-
func (ctx metric) OnVMStart(int) bool {
41+
func (ctx *metricRootContext) OnVMStart(int) bool {
3542
ct, err := proxywasm.DefineCounterMetric(metricsName)
3643
if err != nil {
3744
proxywasm.LogCriticalf("error defining metrics: %v", err)
@@ -40,8 +47,17 @@ func (ctx metric) OnVMStart(int) bool {
4047
return true
4148
}
4249

50+
type metricHttpContext struct {
51+
// you must embed the default context so that you need not to reimplement all the methods by yourself
52+
proxywasm.DefaultHttpContext
53+
}
54+
55+
func newHttpContext(uint32) proxywasm.HttpContext {
56+
return &metricHttpContext{}
57+
}
58+
4359
// override
44-
func (ctx metric) OnHttpRequestHeaders(int, bool) types.Action {
60+
func (ctx *metricHttpContext) OnHttpRequestHeaders(int, bool) types.Action {
4561
prev, err := counter.Get()
4662
if err != nil {
4763
proxywasm.LogCriticalf("error retrieving previous metric: %v", err)

examples/metrics/main_test.go

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,17 @@ import (
1111
)
1212

1313
func TestMetric(t *testing.T) {
14+
host := proxytest.NewHostEmulator(nil, nil,
15+
newRootContext, nil, newHttpContext,
16+
)
17+
defer host.Done() // release the host emulation lock so that other test cases can insert their own host emulation
1418

15-
ctx := metric{}
16-
host, done := proxytest.NewRootFilterHost(ctx, nil, nil)
17-
defer done() // release the host emulation lock so that other test cases can insert their own host emulation
18-
19-
host.StartVM() // define metric
19+
host.StartVM() // call OnVMStart: define metric
2020

21+
contextID := host.HttpFilterInitContext()
2122
exp := uint64(3)
2223
for i := uint64(0); i < exp; i++ {
23-
ctx.OnHttpRequestHeaders(0, false) // increment
24+
host.HttpFilterPutRequestHeaders(contextID, nil)
2425
}
2526

2627
logs := host.GetLogs(types.LogLevelInfo)

0 commit comments

Comments
 (0)