higress icon indicating copy to clipboard operation
higress copied to clipboard

插件返回 ActionPause 不生效

Open gyy-bisontech opened this issue 1 month ago • 13 comments

我写了一个插件, 在解析请求头的时候返回ActionPause, 这种情况下http请求应该会卡住才对, 但是我直接获取到了后端服务的返回

插件代码:

package main

import (
	"github.com/alibaba/higress/plugins/wasm-go/pkg/wrapper"
	"github.com/higress-group/proxy-wasm-go-sdk/proxywasm/types"
	"github.com/tidwall/gjson"
)

func main() {
	wrapper.SetCtx(
		"checker", // 配置插件名称
		wrapper.ParseConfigBy(parseConfig),
		wrapper.ProcessRequestHeadersBy(onHttpRequestHeaders),
	)
}

type Config struct {
}

// parseConfig 解析配置, 获取可用设备及请求路径
func parseConfig(json gjson.Result, config *Config, log wrapper.Log) error {
	return nil
}

// onHttpRequestHeaders 解析请求头, 获取 token
func onHttpRequestHeaders(ctx wrapper.HttpContext, config Config, log wrapper.Log) types.Action {
	log.Debugf("checker get request")
	defer func() { log.Debugf("checker end process") }()
	return types.ActionPause
}

Dockerfile:

ARG BUILDER=higress-registry.cn-hangzhou.cr.aliyuncs.com/plugins/wasm-go-builder:go1.19-tinygo0.28.1-oras1.0.0
FROM $BUILDER as builder

ARG GOPROXY
ENV GOPROXY=${GOPROXY}

ARG EXTRA_TAGS=proxy_wasm_version_0_2_100
ENV EXTRA_TAGS=${EXTRA_TAGS}

WORKDIR /workspace
COPY . .
RUN go mod tidy
RUN tinygo build -o /main.wasm -scheduler=none -gc=custom -tags="custommalloc nottinygc_finalizer $EXTRA_TAGS" -target=wasi ./...

FROM scratch as output
COPY --from=builder /main.wasm plugin.wasm

docker-compose.yml

version: '3.7'
services:
  envoy:
    image: higress-registry.cn-hangzhou.cr.aliyuncs.com/higress/gateway:v2.0.4
    entrypoint: /usr/local/bin/envoy
    # 注意这里对wasm开启了debug级别日志,正式部署时则默认info级别
    command: -c /etc/envoy/envoy.yaml --component-log-level wasm:debug
    depends_on:
      - httpbin
      - redis
    networks:
      - wasmtest
    ports:
      - "10000:10000"
      - "9901:9901"
    volumes:
      - ./envoy.yaml:/etc/envoy/envoy.yaml
      - ./build/plugin.wasm:/etc/envoy/plugin.wasm
      - ./envoy.log:/etc/envoy/envoy.log
  httpbin:
    image: kennethreitz/httpbin:latest
    networks:
      - wasmtest
    ports:
      - "12345:80"
  redis:
    image: redis
    environment:
      - ALLOW_EMPTY_PASSWORD=yes
    networks:
      wasmtest:
        ipv4_address: 172.20.0.100
    ports:
      - 6379:6379

networks:
  wasmtest:
    ipam:
      config:
        - subnet: 172.20.0.0/24

Makefile

PLUGIN_NAME ?= checker-dev
BUILDER_REGISTRY ?= higress-registry.cn-hangzhou.cr.aliyuncs.com/plugins/
REGISTRY ?= myregistry/
GO_VERSION ?= 1.19
TINYGO_VERSION ?= 0.28.1
ORAS_VERSION ?= 1.0.0
HIGRESS_VERSION ?= 1.4.1
USE_HIGRESS_TINYGO ?= true
BUILDER ?= ${BUILDER_REGISTRY}wasm-go-builder:go${GO_VERSION}-tinygo${TINYGO_VERSION}-oras${ORAS_VERSION}
BUILD_TIME := $(shell date "+%Y%m%d-%H%M%S")
COMMIT_ID := $(shell git rev-parse --short HEAD 2>/dev/null)
IMAGE_TAG = $(if $(strip $(PLUGIN_VERSION)),${PLUGIN_VERSION},${BUILD_TIME}-${COMMIT_ID})
IMG ?= ${REGISTRY}${PLUGIN_NAME}:${IMAGE_TAG}
GOPROXY := $(shell go env GOPROXY)
EXTRA_TAGS ?= proxy_wasm_version_0_2_100

.DEFAULT:
local-docker-build:
	DOCKER_BUILDKIT=1 docker build --build-arg BUILDER=${BUILDER}  \
								--build-arg GOPROXY=$(GOPROXY) \
								--build-arg EXTRA_TAGS=$(EXTRA_TAGS) \
								-t ${IMG} \
								--output build \
								.
	@echo ""
	@echo "output wasm file: ./build/plugin.wasm"

build-image:
	DOCKER_BUILDKIT=1 docker build --build-arg BUILDER=${BUILDER}  \
								--build-arg GOPROXY=$(GOPROXY) \
								--build-arg EXTRA_TAGS=$(EXTRA_TAGS) \
								-t ${IMG} \
								.
	@echo ""
	@echo "image:            ${IMG}"

build-push: build-image
	docker push ${IMG}

local-build:
	tinygo build -scheduler=none -target=wasi -gc=custom -tags='custommalloc nottinygc_finalizer $(EXTRA_TAGS)' \
		-o ./build/plugin.wasm main.go
	@echo ""
	@echo "wasm: ./build/plugin.wasm"

local-run:
	echo  > ./envoy.log
	docker compose down
	docker compose up -d

local-all: local-build local-run
local-docker-all: local-docker-build local-run

执行命令 make local-docker-all 后本地请求 http://127.0.0.1:10000/get 接口会直接返回 请求没有卡住

gyy-bisontech avatar Jan 15 '25 12:01 gyy-bisontech