Skip to content

Add wasm extension #4

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
491 changes: 489 additions & 2 deletions README.md

Large diffs are not rendered by default.

456 changes: 456 additions & 0 deletions core/sentinel.go

Large diffs are not rendered by default.

86 changes: 86 additions & 0 deletions envoy.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
static_resources:
listeners:
- name: tcp
address:
socket_address:
address: 0.0.0.0
port_value: 18000
filter_chains:
- filters:
- name: envoy.filters.network.wasm
typed_config:
"@type": type.googleapis.com/envoy.extensions.filters.network.wasm.v3.Wasm
config:
# Instruct that the PluginContext should behave as a Tcp filter.
configuration:
"@type": type.googleapis.com/google.protobuf.StringValue
value: |
{
"config_path": "./sentinel.yml",
"resource_name": "some-test"
}
# Use the same vm_config as above, so we can reuse the same VM for multiple queues.
vm_config:
vm_id: "sender"
runtime: "envoy.wasm.runtime.v8"
code:
local:
filename: "main.wasm"

- name: envoy.tcp_proxy
typed_config:
"@type": type.googleapis.com/envoy.extensions.filters.network.tcp_proxy.v3.TcpProxy
stat_prefix: ingress
cluster: web_service

- name: http
address:
socket_address:
address: 127.0.0.1
port_value: 8099
filter_chains:
- filters:
- name: envoy.http_connection_manager
typed_config:
"@type": type.googleapis.com/envoy.extensions.filters.network.http_connection_manager.v3.HttpConnectionManager
stat_prefix: ingress_http
codec_type: auto
route_config:
name: local_route
virtual_hosts:
- name: local_service
domains:
- "*"
routes:
- match:
prefix: "/"
direct_response:
status: 200
body:
inline_string: "example body\n"
http_filters:
- name: envoy.filters.http.router
typed_config:
"@type": type.googleapis.com/envoy.extensions.filters.http.router.v3.Router

clusters:
- name: web_service
connect_timeout: 0.25s
type: STATIC
lb_policy: ROUND_ROBIN
load_assignment:
cluster_name: mock_service
endpoints:
- lb_endpoints:
- endpoint:
address:
socket_address:
address: 127.0.0.1
port_value: 8099

admin:
access_log_path: "/dev/null"
address:
socket_address:
address: 0.0.0.0
port_value: 8001
96 changes: 96 additions & 0 deletions example/client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
package main

import (
"context"
"flag"
"fmt"
"io/ioutil"
"net/http"
"os"
"os/signal"
"strconv"
"sync/atomic"
"syscall"
"time"
)

var (
qps int
path string
port int
totalNum int64
)

func init() {
flag.IntVar(&qps, "q", 0, "qps settings")
flag.StringVar(&path, "u", "", "url path")
flag.IntVar(&port, "p", 8080, "port")
flag.Parse()
}

func listenSysSignals(cancel context.CancelFunc) {
signalChan := make(chan os.Signal, 1)
ignoreChan := make(chan os.Signal, 1)

signal.Notify(ignoreChan, syscall.SIGHUP)
signal.Notify(signalChan, syscall.SIGINT, syscall.SIGQUIT, syscall.SIGTERM)

select {
case sig := <-signalChan:
fmt.Printf("main exit due to system signal occur: %s\n", sig)
cancel()
case sig := <-ignoreChan:
fmt.Printf("ignore system signal: %s\n", sig)
}
}

func main() {
ctx, cancel := context.WithCancel(context.Background())
for i := 0; i < 200*qps; i++ {
go startQPSTest(ctx)
}
go startStatistics(ctx)
listenSysSignals(cancel)
}

func startStatistics(ctx context.Context) {
ticker := time.NewTicker(time.Second * 1)
defer ticker.Stop()

var counter int64
for {
select {
case <-ticker.C:
fmt.Printf("QPS: %v | totalNum: %v\n", totalNum-counter, totalNum)
counter = totalNum
case <-ctx.Done():
fmt.Printf("Child startStatistics goroutine exit.\n")
return
}
}
}

func startQPSTest(ctx context.Context) {
ticker := time.NewTicker(time.Second * 1)
defer ticker.Stop()

for {
select {
case <-ticker.C:
resp, err := http.Get("http://localhost:" + strconv.Itoa(port) + "/" + path)
if err != nil {
fmt.Println(err)
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
fmt.Println(err)
}
fmt.Printf("body = %v | statusCode = %v\n", string(body), resp.StatusCode)
atomic.AddInt64(&totalNum, 1)
case <-ctx.Done():
fmt.Printf("Child startQPSTest goroutine exit.\n")
return
}
}
}
29 changes: 29 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
module sentinel-go-proxy-wasm-extension

go 1.19

require (
github.com/alibaba/sentinel-golang v1.0.4
github.com/tetratelabs/proxy-wasm-go-sdk v0.20.0
gopkg.in/yaml.v2 v2.4.0
)

require (
github.com/StackExchange/wmi v0.0.0-20190523213315-cbe66965904d // indirect
github.com/beorn7/perks v1.0.1 // indirect
github.com/cespare/xxhash/v2 v2.1.1 // indirect
github.com/go-ole/go-ole v1.2.4 // indirect
github.com/golang/protobuf v1.4.3 // indirect
github.com/google/uuid v1.1.1 // indirect
github.com/matttproud/golang_protobuf_extensions v1.0.1 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/prometheus/client_golang v1.9.0 // indirect
github.com/prometheus/client_model v0.2.0 // indirect
github.com/prometheus/common v0.15.0 // indirect
github.com/prometheus/procfs v0.2.0 // indirect
github.com/shirou/gopsutil/v3 v3.21.6 // indirect
github.com/tklauser/go-sysconf v0.3.6 // indirect
github.com/tklauser/numcpus v0.2.2 // indirect
golang.org/x/sys v0.0.0-20210316164454-77fc1eacc6aa // indirect
google.golang.org/protobuf v1.23.0 // indirect
)
Loading