apache / apache/dubbo-go

grpc Context failed to transfer attachment data

Open
#1,410 3 comments 0 reactions 0 assignees View on GitHub
good first issue
Dominant language
Go
Stars
5k
Forks
1k
Avg merge
2d 8h
Merged PRs (30d)
31

Description

**What happened**:
当使用grpc的Context时, 无法将值从客户端传递到服务端

**What you expected to happen**:

**How to reproduce it (as minimally and precisely as possible)**:
dubbo-go@v1.5.7-rc2
```proto
syntax = "proto3";

option java_multiple_files = true;
option java_package = "demo.micro.server.proto";
option java_outer_classname = "sc";
option objc_class_prefix = "demo";

package proto;
option go_package = ".;demo";

// 编译命令 protoc -I ./ demo.proto --dubbo_out=plugins=grpc+dubbo:.
service SContext {
rpc FContext(Req) returns (Res){}
}

message Req{
string data = 1;
}

message Res{
string data = 1;
}
```

```golang
//客户端:
req := &demo.Req{}
res := &demo.Res{}
err := consumer.GetContextImpl().FContext(context.WithValue(context.Background(), constant.AttachmentKey, map[string]interface{}{
"socket.addr": "127.0.0.1:12788",
}), req, res)

//服务端:
func (g *Provider) FContext(ctx context.Context, req *demo.Req) (res *demo.Res, err error) {
addr := ""
attachment := ctx.Value(constant.AttachmentKey).(map[string]interface{})
if v, ok := attachment["socket.addr"]; ok {
addr = v.(string)
}
fmt.Println("test context, addr:", addr)
return
}
```

**发现这个attachment是nil ,如下图 **

![image](https://user-images.githubusercontent.com/46220496/130427730-4e02444a-0cd5-4edf-b8ad-04390c26b8ed.png)

**Anything else we need to know?**:
完整示例请参考: https://gitee.com/lethexixin/grpc-context-error.git

Contributor guide

Open the contributing guide

Research direction

Start by reproducing the failure with the provided proto definition and the client call to consumer.GetContextImpl().FContext. Compare the client context value with what the provider’s FContext receives, using the linked complete example as additional context. Done means the attachment data is transferred and can be read on the server.

Written by the indexing model from the issue text.

Assessment

Tech stack
go, grpc
Domain
api, backend
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.