marsevilspirit commented on PR #2928: URL: https://github.com/apache/dubbo-go/pull/2928#issuecomment-3050974106
@ziyyun I have tested using ctx to pass attachments directly, and the effect is very good. I think this PR can be merged directly. client: ```go package main import ( "context" "dubbo.apache.org/dubbo-go/v3/client" "dubbo.apache.org/dubbo-go/v3/common/constant" _ "dubbo.apache.org/dubbo-go/v3/imports" greet "github.com/apache/dubbo-go-samples/helloworld/proto" "github.com/dubbogo/gost/log/logger" ) func main() { cli, err := client.NewClient( client.WithClientURL("127.0.0.1:20000"), ) if err != nil { panic(err) } svc, err := greet.NewGreetService(cli) if err != nil { panic(err) } ctx := context.Background() ctx = context.WithValue(ctx, constant.AttachmentKey, map[string]any{"foo": "bar"}) resp, err := svc.Greet(ctx, &greet.GreetRequest{Name: "hello world"}) if err != nil { logger.Error(err) } logger.Infof("Greet response: %s", resp.Greeting) } ``` server: ```go package main import ( "context" "dubbo.apache.org/dubbo-go/v3/common/constant" _ "dubbo.apache.org/dubbo-go/v3/imports" "dubbo.apache.org/dubbo-go/v3/protocol" "dubbo.apache.org/dubbo-go/v3/server" greet "github.com/apache/dubbo-go-samples/helloworld/proto" "github.com/dubbogo/gost/log/logger" ) type GreetTripleServer struct { } func (srv *GreetTripleServer) Greet(ctx context.Context, req *greet.GreetRequest) (*greet.GreetResponse, error) { attachments := ctx.Value(constant.AttachmentKey).(map[string]any) logger.Infof("attachments : %v", attachments) logger.Infof("attachments[foo] : %v", attachments["foo"]) resp := &greet.GreetResponse{Greeting: req.Name} return resp, nil } func main() { srv, err := server.NewServer( server.WithServerProtocol( protocol.WithPort(20000), protocol.WithTriple(), ), ) if err != nil { panic(err) } if err := greet.RegisterGreetServiceHandler(srv, &GreetTripleServer{}); err != nil { panic(err) } if err := srv.Serve(); err != nil { logger.Error(err) } } ``` For the **context** you are worried about that there have an attachmentkey. Different protocols do indeed have different processing logic, but the user will not perceive it, so there is no need to worry. ``` mermaid --- config: look: handDrawn theme: neutral --- flowchart LR start[Start] --> protocol{Protocol} protocol -->|Triple| infoProxy[infoProxyInvoker] protocol -->|Dubbo| proxy[ProxyInvoker] infoProxy --> handleTriAttachments proxy --> handleDubboAttachments ``` -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: notifications-unsubscr...@dubbo.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org --------------------------------------------------------------------- To unsubscribe, e-mail: notifications-unsubscr...@dubbo.apache.org For additional commands, e-mail: notifications-h...@dubbo.apache.org