grpc-go通过context传递额外数据

  • 使用 ctx.Value 从 context 读取数据
// ValueFromIncomingContext returns the metadata value corresponding to the metadata
// key from the incoming metadata if it exists. Key must be lower-case.
//
// # Experimental
//
// Notice: This API is EXPERIMENTAL and may be changed or removed in a
// later release.
func ValueFromIncomingContext(ctx context.Context, key string) []string {
   
	md, ok := ctx.Value(mdIncomingKey{
   }).(MD)
	if !ok {
   
		return nil
	}

	if v, ok := md[key]; ok {
   
		return copyOf(v)
	}
	for k, v := range md {
   
		// We need to manually convert all keys to lower case, because MD is a
		// map, and there's no guarantee that the MD attached to the context is
		// created using our helper functions.
		if strings.ToLower(k) == key {
   
			return copyOf(v)
		}
	}
	return nil
}
  • 使用 ctx.Value 往 context 写入数据
// AppendToOutgoingContext returns a new context with the provided kv merged
// with any existing metadata in the context. Please refer to the documentation
// of Pairs for a description of kv.
func AppendToOutgoingContext(ctx context.Context, kv ...string) context.Context {
   
	if len(kv)%2 == 1 {
   
		panic(fmt.Sprintf("metadata: AppendToOutgoingContext got an odd number of input pairs for metadata: %d", len(kv)))
	}
	md, _ := ctx.Value(mdOutgoingKey{
   }).(rawMD)
	added := make([][]string, len(md.added)+1)
	copy(added, md.added)
	kvCopy := make([]string, 0, len(kv))
	for i := 0; i < len(kv); i += 2 {
   
		kvCopy = append(kvCopy, strings.ToLower(kv[i]), kv[i+1])
	}
	added[len(added)-1] = kvCopy
	return context.WithValue(ctx, mdOutgoingKey{
   }, rawMD{
   md: md.md, added: added})
}

metadata 是 grpc 内置的,用来往 RPC 服务传递 http 头数据,分 in 和 out 两种,对应的 key 都为一个空 struct,分别为:mdIncomingKey 和 mdOutgoingKey 。

服务端的 ctx 和 md 直接打印出来,如下样子:

fmt.Println(ctx)
fmt.Println(md)

context.Background.WithValue(type transport.connectionKey, val <not Stringer>).WithValue(type peer.peerKey, val <not Stringer>).WithDeadline(2024-02-19 10:02:43.212614653 +0800 CST m=+41018.106555206 [1.999790196s]).WithValue(type metadata.mdIncomingKey, val <not Stringer>).WithValue(type grpc.streamKey, val <not Stringer>).WithValue(type baggage.baggageContextKeyType, val <not Stringer>).WithValue(type trace.traceContextKeyType, val <not Stringer>).WithValue(type trace.traceContextKeyType, val <not Stringer>).WithCancel

map[:authority:[add.rpc] append:[append-value] content-type:[application/grpc] extra:[extra-value] grpc-accept-encoding:[gzip] noncestr:[abc] signature:[0123456789] timestamp:[2021-07-01 00:00:00] traceparent:[00-89415f99d44e6f8f6e14e3fe8f13ad20-bf33b29c4362ca6a-00] user-agent:[grpc-go/1.59.0]]
signature: [0123456789]

注意 md 中的值会被加上中括号“[]”。

相关推荐

  1. grpc-go通过context传递额外数据

    2024-02-21 08:34:05       56 阅读
  2. go-zero/grpc的rpc服务间传递额外数据

    2024-02-21 08:34:05       62 阅读
  3. GO——context

    2024-02-21 08:34:05       59 阅读
  4. GoContext

    2024-02-21 08:34:05       38 阅读
  5. 【react】使用context进行跨级组件数据传递

    2024-02-21 08:34:05       55 阅读
  6. gogrpc的三种流模式通信

    2024-02-21 08:34:05       32 阅读
  7. go grpc安装protobuf

    2024-02-21 08:34:05       29 阅读
  8. go context详解

    2024-02-21 08:34:05       32 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-02-21 08:34:05       98 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-02-21 08:34:05       106 阅读
  3. 在Django里面运行非项目文件

    2024-02-21 08:34:05       87 阅读
  4. Python语言-面向对象

    2024-02-21 08:34:05       96 阅读

热门阅读

  1. 最大加权矩阵(洛谷)

    2024-02-21 08:34:05       63 阅读
  2. 木马植入方式及防范手段

    2024-02-21 08:34:05       56 阅读
  3. c++ pimpl

    2024-02-21 08:34:05       52 阅读
  4. 数据分析Pandas专栏---第一章<数据清洗>

    2024-02-21 08:34:05       49 阅读
  5. c++栈(stack)的介绍

    2024-02-21 08:34:05       49 阅读
  6. Jenkins 编译脚本

    2024-02-21 08:34:05       49 阅读
  7. Compose的gradle配置以及与Kotlin的兼容对应

    2024-02-21 08:34:05       66 阅读
  8. HTTP CURL

    2024-02-21 08:34:05       58 阅读