Skip to content

Commit 9456a1b

Browse files
authored
Clean ups example codes (tetratelabs#445)
While there, this also updates the formatter/linter dependencies as well as the year in the license. Signed-off-by: Takeshi Yoneda <[email protected]>
1 parent 021aa9c commit 9456a1b

Some content is hidden

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

59 files changed

+212
-166
lines changed

Makefile

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
goimports := golang.org/x/tools/cmd/goimports@v0.7.0
2-
golangci_lint := github.com/golangci/golangci-lint/cmd/golangci-lint@v1.52.2
1+
goimports := golang.org/x/tools/cmd/goimports@v0.21.0
2+
golangci_lint := github.com/golangci/golangci-lint/cmd/golangci-lint@v1.59.0
33

44

55
.PHONY: build.example
@@ -40,7 +40,10 @@ run:
4040

4141
.PHONY: lint
4242
lint:
43-
@go run $(golangci_lint) run
43+
@find . -name "go.mod" \
44+
| grep go.mod \
45+
| xargs -I {} bash -c 'dirname {}' \
46+
| xargs -I {} bash -c 'echo "=> {}"; cd {}; go run $(golangci_lint) run; '
4447

4548
.PHONY: format
4649
format:

NOTICE

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
proxy-wasm-go-sdk
2-
Copyright 2020-2021 Tetrate
2+
Copyright 2020-2024 Tetrate

e2e/e2e_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright 2020-2021 Tetrate
1+
// Copyright 2020-2024 Tetrate
22
//
33
// Licensed under the Apache License, Version 2.0 (the "License");
44
// you may not use this file except in compliance with the License.

e2e/main_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright 2021 Tetrate
1+
// Copyright 2024 Tetrate
22
//
33
// Licensed under the Apache License, Version 2.0 (the "License");
44
// you may not use this file except in compliance with the License.

examples/dispatch_call_on_tick/main.go

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright 2020-2021 Tetrate
1+
// Copyright 2020-2024 Tetrate
22
//
33
// Licensed under the Apache License, Version 2.0 (the "License");
44
// you may not use this file except in compliance with the License.
@@ -27,17 +27,19 @@ func main() {
2727
proxywasm.SetVMContext(&vmContext{})
2828
}
2929

30+
// vmContext implements types.VMContext.
3031
type vmContext struct {
3132
// Embed the default VM context here,
3233
// so that we don't need to reimplement all the methods.
3334
types.DefaultVMContext
3435
}
3536

36-
// Override types.DefaultVMContext.
37+
// NewPluginContext implements types.VMContext.
3738
func (*vmContext) NewPluginContext(contextID uint32) types.PluginContext {
3839
return &pluginContext{contextID: contextID}
3940
}
4041

42+
// pluginContext implements types.PluginContext.
4143
type pluginContext struct {
4244
// Embed the default plugin context here,
4345
// so that we don't need to reimplement all the methods.
@@ -47,7 +49,7 @@ type pluginContext struct {
4749
cnt int
4850
}
4951

50-
// Override types.DefaultPluginContext.
52+
// OnPluginStart implements types.PluginContext.
5153
func (ctx *pluginContext) OnPluginStart(pluginConfigurationSize int) types.OnPluginStartStatus {
5254
if err := proxywasm.SetTickPeriodMilliSeconds(tickMilliseconds); err != nil {
5355
proxywasm.LogCriticalf("failed to set tick period: %v", err)
@@ -75,7 +77,7 @@ func (ctx *pluginContext) OnPluginStart(pluginConfigurationSize int) types.OnPlu
7577
return types.OnPluginStartStatusOK
7678
}
7779

78-
// Override types.DefaultPluginContext.
80+
// OnTick implements types.PluginContext.
7981
func (ctx *pluginContext) OnTick() {
8082
headers := [][2]string{
8183
{":method", "GET"}, {":authority", "some_authority"}, {"accept", "*/*"},

examples/foreign_call_on_tick/main.go

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright 2020-2021 Tetrate
1+
// Copyright 2020-2024 Tetrate
22
//
33
// Licensed under the Apache License, Version 2.0 (the "License");
44
// you may not use this file except in compliance with the License.
@@ -27,26 +27,27 @@ func main() {
2727
proxywasm.SetVMContext(&vmContext{})
2828
}
2929

30+
// vmContext implements types.VMContext.
3031
type vmContext struct {
3132
// Embed the default VM context here,
3233
// so that we don't need to reimplement all the methods.
3334
types.DefaultVMContext
3435
}
3536

36-
// Override types.DefaultVMContext.
37+
// NewPluginContext implements types.VMContext.
3738
func (*vmContext) NewPluginContext(contextID uint32) types.PluginContext {
3839
return &pluginContext{}
3940
}
4041

42+
// pluginContext implements types.PluginContext.
4143
type pluginContext struct {
4244
// Embed the default plugin context here,
4345
// so that we don't need to reimplement all the methods.
4446
types.DefaultPluginContext
45-
contextID uint32
46-
callNum uint32
47+
callNum uint32
4748
}
4849

49-
// Override types.DefaultPluginContext.
50+
// OnPluginStart implements types.PluginContext.
5051
func (ctx *pluginContext) OnPluginStart(pluginConfigurationSize int) types.OnPluginStartStatus {
5152
if err := proxywasm.SetTickPeriodMilliSeconds(tickMilliseconds); err != nil {
5253
proxywasm.LogCriticalf("failed to set tick period: %v", err)
@@ -56,7 +57,7 @@ func (ctx *pluginContext) OnPluginStart(pluginConfigurationSize int) types.OnPlu
5657
return types.OnPluginStartStatusOK
5758
}
5859

59-
// Override types.DefaultPluginContext.
60+
// OnTick implements types.PluginContext.
6061
func (ctx *pluginContext) OnTick() {
6162
ctx.callNum++
6263
ret, err := proxywasm.CallForeignFunction("compress", []byte("hello world!"))

examples/helloworld/main.go

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright 2020-2021 Tetrate
1+
// Copyright 2020-2024 Tetrate
22
//
33
// Licensed under the Apache License, Version 2.0 (the "License");
44
// you may not use this file except in compliance with the License.
@@ -28,25 +28,26 @@ func main() {
2828
proxywasm.SetVMContext(&vmContext{})
2929
}
3030

31+
// vmContext implements types.VMContext.
3132
type vmContext struct {
3233
// Embed the default VM context here,
3334
// so that we don't need to reimplement all the methods.
3435
types.DefaultVMContext
3536
}
3637

37-
// Override types.DefaultVMContext.
38+
// NewPluginContext implements types.VMContext.
3839
func (*vmContext) NewPluginContext(contextID uint32) types.PluginContext {
3940
return &helloWorld{}
4041
}
4142

43+
// helloWorld implements types.PluginContext.
4244
type helloWorld struct {
4345
// Embed the default plugin context here,
4446
// so that we don't need to reimplement all the methods.
4547
types.DefaultPluginContext
46-
contextID uint32
4748
}
4849

49-
// Override types.DefaultPluginContext.
50+
// OnPluginStart implements types.PluginContext.
5051
func (ctx *helloWorld) OnPluginStart(pluginConfigurationSize int) types.OnPluginStartStatus {
5152
rand.Seed(time.Now().UnixNano())
5253

@@ -58,12 +59,12 @@ func (ctx *helloWorld) OnPluginStart(pluginConfigurationSize int) types.OnPlugin
5859
return types.OnPluginStartStatusOK
5960
}
6061

61-
// Override types.DefaultPluginContext.
62+
// OnTick implements types.PluginContext.
6263
func (ctx *helloWorld) OnTick() {
6364
t := time.Now().UnixNano()
6465
proxywasm.LogInfof("It's %d: random value: %d", t, rand.Uint64())
6566
proxywasm.LogInfof("OnTick called")
6667
}
6768

68-
// Override types.DefaultPluginContext.
69+
// NewHttpContext implements types.PluginContext.
6970
func (*helloWorld) NewHttpContext(uint32) types.HttpContext { return &types.DefaultHttpContext{} }

examples/http_auth_random/main.go

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright 2020-2021 Tetrate
1+
// Copyright 2020-2024 Tetrate
22
//
33
// Licensed under the Apache License, Version 2.0 (the "License");
44
// you may not use this file except in compliance with the License.
@@ -27,36 +27,39 @@ func main() {
2727
proxywasm.SetVMContext(&vmContext{})
2828
}
2929

30+
// vmContext implements types.VMContext.
3031
type vmContext struct {
3132
// Embed the default VM context here,
3233
// so that we don't need to reimplement all the methods.
3334
types.DefaultVMContext
3435
}
3536

36-
// Override types.DefaultVMContext.
37+
// NewPluginContext implements types.VMContext.
3738
func (*vmContext) NewPluginContext(contextID uint32) types.PluginContext {
3839
return &pluginContext{}
3940
}
4041

42+
// pluginContext implements types.PluginContext.
4143
type pluginContext struct {
4244
// Embed the default plugin context here,
4345
// so that we don't need to reimplement all the methods.
4446
types.DefaultPluginContext
4547
}
4648

47-
// Override types.DefaultPluginContext.
49+
// NewHttpContext implements types.PluginContext.
4850
func (*pluginContext) NewHttpContext(contextID uint32) types.HttpContext {
4951
return &httpAuthRandom{contextID: contextID}
5052
}
5153

54+
// httpAuthRandom implements types.HttpContext.
5255
type httpAuthRandom struct {
5356
// Embed the default http context here,
5457
// so that we don't need to reimplement all the methods.
5558
types.DefaultHttpContext
5659
contextID uint32
5760
}
5861

59-
// Override types.DefaultHttpContext.
62+
// OnHttpRequestHeaders implements types.HttpContext.
6063
func (ctx *httpAuthRandom) OnHttpRequestHeaders(numHeaders int, endOfStream bool) types.Action {
6164
hs, err := proxywasm.GetHttpRequestHeaders()
6265
if err != nil {
@@ -77,6 +80,7 @@ func (ctx *httpAuthRandom) OnHttpRequestHeaders(numHeaders int, endOfStream bool
7780
return types.ActionPause
7881
}
7982

83+
// httpCallResponseCallback is a callback function when the http call response is received after dispatching.
8084
func httpCallResponseCallback(numHeaders, bodySize, numTrailers int) {
8185
hs, err := proxywasm.GetHttpCallResponseHeaders()
8286
if err != nil {
@@ -91,20 +95,20 @@ func httpCallResponseCallback(numHeaders, bodySize, numTrailers int) {
9195
b, err := proxywasm.GetHttpCallResponseBody(0, bodySize)
9296
if err != nil {
9397
proxywasm.LogCriticalf("failed to get response body: %v", err)
94-
proxywasm.ResumeHttpRequest()
98+
_ = proxywasm.ResumeHttpRequest()
9599
return
96100
}
97101

98102
s := fnv.New32a()
99103
if _, err := s.Write(b); err != nil {
100104
proxywasm.LogCriticalf("failed to calculate hash: %v", err)
101-
proxywasm.ResumeHttpRequest()
105+
_ = proxywasm.ResumeHttpRequest()
102106
return
103107
}
104108

105109
if s.Sum32()%2 == 0 {
106110
proxywasm.LogInfo("access granted")
107-
proxywasm.ResumeHttpRequest()
111+
_ = proxywasm.ResumeHttpRequest()
108112
return
109113
}
110114

@@ -114,6 +118,6 @@ func httpCallResponseCallback(numHeaders, bodySize, numTrailers int) {
114118
{"powered-by", "proxy-wasm-go-sdk!!"},
115119
}, []byte(body), -1); err != nil {
116120
proxywasm.LogErrorf("failed to send local response: %v", err)
117-
proxywasm.ResumeHttpRequest()
121+
_ = proxywasm.ResumeHttpRequest()
118122
}
119123
}

examples/http_body/main.go

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright 2020-2021 Tetrate
1+
// Copyright 2020-2024 Tetrate
22
//
33
// Licensed under the Apache License, Version 2.0 (the "License");
44
// you may not use this file except in compliance with the License.
@@ -29,33 +29,35 @@ func main() {
2929
proxywasm.SetVMContext(&vmContext{})
3030
}
3131

32+
// vmContext implements types.VMContext.
3233
type vmContext struct {
3334
// Embed the default VM context here,
3435
// so that we don't need to reimplement all the methods.
3536
types.DefaultVMContext
3637
}
3738

38-
// Override types.DefaultVMContext.
39+
// NewPluginContext implements types.VMContext.
3940
func (*vmContext) NewPluginContext(contextID uint32) types.PluginContext {
4041
return &pluginContext{}
4142
}
4243

44+
// pluginContext implements types.PluginContext.
4345
type pluginContext struct {
4446
// Embed the default plugin context here,
4547
// so that we don't need to reimplement all the methods.
4648
types.DefaultPluginContext
4749
shouldEchoBody bool
4850
}
4951

50-
// Override types.DefaultPluginContext.
52+
// NewHttpContext implements types.PluginContext.
5153
func (ctx *pluginContext) NewHttpContext(contextID uint32) types.HttpContext {
5254
if ctx.shouldEchoBody {
5355
return &echoBodyContext{}
5456
}
5557
return &setBodyContext{}
5658
}
5759

58-
// Override types.DefaultPluginContext.
60+
// OnPluginStart implements types.PluginContext.
5961
func (ctx *pluginContext) OnPluginStart(pluginConfigurationSize int) types.OnPluginStartStatus {
6062
data, err := proxywasm.GetPluginConfiguration()
6163
if err != nil {
@@ -65,6 +67,7 @@ func (ctx *pluginContext) OnPluginStart(pluginConfigurationSize int) types.OnPlu
6567
return types.OnPluginStartStatusOK
6668
}
6769

70+
// setBodyContext implements types.HttpContext.
6871
type setBodyContext struct {
6972
// Embed the default root http context here,
7073
// so that we don't need to reimplement all the methods.
@@ -73,7 +76,7 @@ type setBodyContext struct {
7376
bufferOperation string
7477
}
7578

76-
// Override types.DefaultHttpContext.
79+
// OnHttpRequestHeaders implements types.HttpContext.
7780
func (ctx *setBodyContext) OnHttpRequestHeaders(numHeaders int, endOfStream bool) types.Action {
7881
mode, err := proxywasm.GetHttpRequestHeader("buffer-replace-at")
7982
if err == nil && mode == "response" {
@@ -104,7 +107,7 @@ func (ctx *setBodyContext) OnHttpRequestHeaders(numHeaders int, endOfStream bool
104107
return types.ActionContinue
105108
}
106109

107-
// Override types.DefaultHttpContext.
110+
// OnHttpRequestBody implements types.HttpContext.
108111
func (ctx *setBodyContext) OnHttpRequestBody(bodySize int, endOfStream bool) types.Action {
109112
if ctx.modifyResponse {
110113
return types.ActionContinue
@@ -137,7 +140,7 @@ func (ctx *setBodyContext) OnHttpRequestBody(bodySize int, endOfStream bool) typ
137140
return types.ActionContinue
138141
}
139142

140-
// Override types.DefaultHttpContext.
143+
// OnHttpResponseHeaders implements types.HttpContext.
141144
func (ctx *setBodyContext) OnHttpResponseHeaders(numHeaders int, endOfStream bool) types.Action {
142145
if !ctx.modifyResponse {
143146
return types.ActionContinue
@@ -151,7 +154,7 @@ func (ctx *setBodyContext) OnHttpResponseHeaders(numHeaders int, endOfStream boo
151154
return types.ActionContinue
152155
}
153156

154-
// Override types.DefaultHttpContext.
157+
// OnHttpResponseBody implements types.HttpContext.
155158
func (ctx *setBodyContext) OnHttpResponseBody(bodySize int, endOfStream bool) types.Action {
156159
if !ctx.modifyResponse {
157160
return types.ActionContinue
@@ -184,14 +187,15 @@ func (ctx *setBodyContext) OnHttpResponseBody(bodySize int, endOfStream bool) ty
184187
return types.ActionContinue
185188
}
186189

190+
// echoBodyContext implements types.HttpContext.
187191
type echoBodyContext struct {
188192
// Embed the default plugin context
189193
// so that you don't need to reimplement all the methods by yourself.
190194
types.DefaultHttpContext
191195
totalRequestBodySize int
192196
}
193197

194-
// Override types.DefaultHttpContext.
198+
// OnHttpRequestBody implements types.HttpContext.
195199
func (ctx *echoBodyContext) OnHttpRequestBody(bodySize int, endOfStream bool) types.Action {
196200
ctx.totalRequestBodySize = bodySize
197201
if !endOfStream {

0 commit comments

Comments
 (0)