-
Notifications
You must be signed in to change notification settings - Fork 42
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
update tinygo sdk, add network filter category for tinygosdk
Signed-off-by: mathetake <takeshi@tetrate.io>
- Loading branch information
Showing
8 changed files
with
126 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
8 changes: 8 additions & 0 deletions
8
data/extension/init/templates/tinygo/envoy.filters.network/default/go.mod
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
module envoy.filters.http | ||
|
||
go 1.15 | ||
|
||
require ( | ||
github.com/stretchr/testify v1.6.1 | ||
github.com/tetratelabs/proxy-wasm-go-sdk v0.0.2 | ||
) |
55 changes: 55 additions & 0 deletions
55
data/extension/init/templates/tinygo/envoy.filters.network/default/main.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
// Copyright 2020 Tetrate | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package main | ||
|
||
import ( | ||
"github.com/tetratelabs/proxy-wasm-go-sdk/proxywasm" | ||
"github.com/tetratelabs/proxy-wasm-go-sdk/proxywasm/types" | ||
) | ||
|
||
var ( | ||
connectionCounterName = "proxy_wasm_go.connection_counter" | ||
counter proxywasm.MetricCounter | ||
) | ||
|
||
type context struct{ proxywasm.DefaultContext } | ||
|
||
func main() { | ||
proxywasm.SetNewRootContext(func(contextID uint32) proxywasm.RootContext { return context{} }) | ||
proxywasm.SetNewStreamContext(func(contextID uint32) proxywasm.StreamContext { return context{} }) | ||
} | ||
|
||
func (ctx context) OnVMStart(int) bool { | ||
var err error | ||
counter, err = proxywasm.DefineCounterMetric(connectionCounterName) | ||
if err != nil { | ||
proxywasm.LogCritical("failed to initialize connection counter: ", err.Error()) | ||
} | ||
return true | ||
} | ||
|
||
func (ctx context) OnNewConnection() types.Action { | ||
proxywasm.LogInfo("new connection!") | ||
return types.ActionContinue | ||
} | ||
|
||
func (ctx context) OnDone() bool { | ||
err := counter.Increment(1) | ||
if err != nil { | ||
proxywasm.LogCritical("failed to increment connection counter: ", err.Error()) | ||
} | ||
proxywasm.LogInfo("connection complete!") | ||
return true | ||
} |
57 changes: 57 additions & 0 deletions
57
data/extension/init/templates/tinygo/envoy.filters.network/default/main_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
// Copyright 2020 Tetrate | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package main | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
"github.com/tetratelabs/proxy-wasm-go-sdk/proxytest" | ||
"github.com/tetratelabs/proxy-wasm-go-sdk/proxywasm" | ||
"github.com/tetratelabs/proxy-wasm-go-sdk/proxywasm/types" | ||
) | ||
|
||
func newStreamContext(uint32) proxywasm.StreamContext { | ||
return context{} | ||
} | ||
|
||
func TestNetwork_OnNewConnection(t *testing.T) { | ||
host, done := proxytest.NewNetworkFilterHost(newStreamContext) | ||
defer done() // release the host emulation lock so that other test cases can insert their own host emulation | ||
|
||
_ = host.InitConnection() // OnNewConnection is called | ||
|
||
logs := host.GetLogs(types.LogLevelInfo) // retrieve logs emitted to Envoy | ||
assert.Equal(t, logs[0], "new connection!") | ||
} | ||
|
||
func TestNetwork_counter(t *testing.T) { | ||
host, done := proxytest.NewNetworkFilterHost(newStreamContext) | ||
defer done() // release the host emulation lock so that other test cases can insert their own host emulation | ||
|
||
context{}.OnVMStart(0) // init metric | ||
|
||
contextID := host.InitConnection() | ||
host.CompleteConnection(contextID) // call OnDone on contextID -> increment the connection counter | ||
|
||
logs := host.GetLogs(types.LogLevelInfo) | ||
require.Greater(t, len(logs), 0) | ||
|
||
assert.Equal(t, "connection complete!", logs[len(logs)-1]) | ||
actual, err := counter.Get() | ||
require.NoError(t, err) | ||
assert.Equal(t, uint64(1), actual) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters