dubbo-go icon indicating copy to clipboard operation
dubbo-go copied to clipboard

fix: 解决cli.cliOpts.overallReference对象会被污染,而影响其他service

Open ziyyun opened this issue 8 months ago • 1 comments


package main

import (
	"context"
	"fmt"

	"dubbo.apache.org/dubbo-go/v3/client"
	"dubbo.apache.org/dubbo-go/v3/common/extension"
	"dubbo.apache.org/dubbo-go/v3/filter"
	_ "dubbo.apache.org/dubbo-go/v3/imports"
	"dubbo.apache.org/dubbo-go/v3/protocol"
	greet "github.com/apache/dubbo-go-samples/helloworld/proto"
	"github.com/dubbogo/gost/log/logger"
)

func init() {
	extension.SetFilter("myClientFilter", NewMyClientFilter)
}

func NewMyClientFilter() filter.Filter {
	return &MyClientFilter{}
}

type MyClientFilter struct {
}

func (f *MyClientFilter) Invoke(ctx context.Context, invoker protocol.Invoker, invocation protocol.Invocation) protocol.Result {
	fmt.Println("MyClientFilter Invoke is called, method Name = ", invocation.MethodName())
	invocation.SetAttachment("request-key1", "request-value1")
	invocation.SetAttachment("request-key2", []string{"request-value2.1", "request-value2.2"})
	return invoker.Invoke(ctx, invocation)
}
func (f *MyClientFilter) OnResponse(ctx context.Context, result protocol.Result, invoker protocol.Invoker, protocol protocol.Invocation) protocol.Result {
	fmt.Println("MyClientFilter OnResponse is called")
	fmt.Println("result attachment = ", result.Attachments())
	return result
}


func main() {
	cli, err := client.NewClient(
		client.WithClientURL("127.0.0.1:20000"),
		client.WithClientFilter("myClientFilter"),
	)
	if err != nil {
		panic(err)
	}

	svc, err := greet.NewGreetService(cli, client.WithFilter("myClientFilter22"))
	if err != nil {
		panic(err)
	}

	svc2, err := greet.NewGreetService(cli)
	if err != nil {
		panic(err)
	}

	resp, err := svc2.Greet(context.Background(), &greet.GreetRequest{Name: "hello world"})
	if err != nil {
		logger.Error(err)
	} else {
		logger.Infof("Greet response: %s", resp.Greeting)
	}

	resp, err = svc.Greet(context.Background(), &greet.GreetRequest{Name: "hello world"})
	if err != nil {
		logger.Error(err)
	} else {
		logger.Infof("Greet response: %s", resp.Greeting)
	}
}

cli.cliOpts.overallReference被svc污染,导致svc2对象也有myClientFilter22(不存在的)的filter,调用svc2.Greet崩溃

ziyyun avatar Jun 27 '25 02:06 ziyyun

这里需要我之后有时间梳理一下这里的流程,应该是隔离没做好,可能需要重构。

marsevilspirit avatar Jun 29 '25 10:06 marsevilspirit