1kasa opened a new pull request, #2854:
URL: https://github.com/apache/dubbo-go/pull/2854

   fix: https://github.com/apache/dubbo-go/issues/2641
   
   Modified code example demonstration:
   server:
   ```
   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"
        "fmt"
        greet "github.com/apache/dubbo-go-samples/context/proto"
        "github.com/dubbogo/gost/log/logger"
   )
   
   type GreetTripleServer struct {
   }
   
   func (srv *GreetTripleServer) Greet(ctx context.Context, req 
*greet.GreetRequest) (*greet.GreetResponse, error) {
        // map must be assert to map[string]interface, because of dubbo 
limitation
        attachments := 
ctx.Value(constant.AttachmentKey).(map[string]interface{})
        // value must be assert to []string[0], because of http2 header 
limitation
        var value1, value2 string
        if v, ok := attachments["key1"]; ok {
                value1 = v.([]string)[0]
                logger.Infof("Dubbo attachment key1 = %s", value1)
        }
        if v, ok := attachments["key2"]; ok {
                value2 = v.([]string)[0]
                logger.Infof("Dubbo attachment key2 = %s", value2)
        }
   
        serverAttachments := 
ctx.Value(constant.AttachmentServerKey).(map[string]interface{})
        serverAttachments["myKey"] = []string{"myVal"}
        respStr := fmt.Sprintf("name: %s, key1: %s, key2: %s", req.Name, 
value1, value2)
        resp := &greet.GreetResponse{Greeting: respStr}
        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)
        }
   }
   ```
   client:
   ```
   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/context/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()
        serverAttachments := make(map[string]interface{})
        ctx = context.WithValue(ctx, constant.AttachmentServerKey, 
serverAttachments)
        ctx = context.WithValue(ctx, constant.AttachmentKey, 
map[string]interface{}{
                "key1": "user defined value 1",
                "key2": "user defined value 2",
        })
   
        resp, err := svc.Greet(ctx, &greet.GreetRequest{Name: "hello world"})
        if err != nil {
                logger.Error(err)
        }
   
        attachments1 := make(map[string]interface{})
        if ctx.Value(constant.AttachmentServerKey) != nil {
                attachments1 = 
ctx.Value(constant.AttachmentServerKey).(map[string]interface{})
        }
   
        logger.Infof("Greet response: %s", resp.Greeting)
        logger.Infof("attachments: %s", attachments1)
   
        ctx = context.WithValue(ctx, constant.AttachmentKey, 
map[string]interface{}{
                "key3": "user defined value 3",
                "key4": "user defined value 4",
        })
   
        resp1, err := svc.Greet(ctx, &greet.GreetRequest{Name: "hello world1"})
        if err != nil {
                logger.Error(err)
        }
   
        attachments2 := make(map[string]interface{})
        if ctx.Value(constant.AttachmentServerKey) != nil {
                attachments2 = 
ctx.Value(constant.AttachmentServerKey).(map[string]interface{})
        }
        logger.Infof("Greet response1: %s", resp1.Greeting)
        logger.Infof("attachments: %s", attachments2)
   }
   ```
   
   Results demonstration:
   client:
   <img width="1242" alt="image" 
src="https://github.com/user-attachments/assets/58442c06-cbe0-4064-ad17-3f7fdd1eabfb";
 />
   
   server:
   <img width="1219" alt="image" 
src="https://github.com/user-attachments/assets/e13ae97f-7529-43df-961b-e2091fa0b4b7";
 />
   
   Special explanation: 
   The set KV pair will be cleared after completing the next remote call, that 
is, multiple remote calls need to be set multiple times. 
   this is normal


-- 
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: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to