grpc-python06–headers与压缩与传输最大值

添加header信息

service -> client

service:
context.set_trailing_metadata((("x-envoy-decorator-operation", "test"), ("name", "donghao"), ("age", "23")))

client:
resp, call = client.HelloDonghao.with_call(pb2.HelloDonghaoReq(
    name='donghao', age=23
))
print(resp.result)
headers = call.trailing_metadata()
print(dir(headers[0]))
print(headers[0].key, headers[0].value)
print(headers[1])

client -> service

client:

resp, call = client.HelloDonghao.with_call(pb2.HelloDonghaoReq(
    name='donghao', age=23
), metadata=(("client_id", "andriod-alsdjfal"), ("platform", 'IOS')))

service:

headers = context.invocation_metadata()
print(headers) 
print(headers[0].key, headers[0].value)

数据压缩与解压缩

class Compression(enum.IntEnum):
    """Indicates the compression method to be used for an RPC.

       This enumeration is part of an EXPERIMENTAL API.

       Attributes:
        NoCompression: Do not use compression algorithm.
        Deflate: Use "Deflate" compression algorithm.
        Gzip: Use "Gzip" compression algorithm.
    """
    NoCompression = _compression.NoCompression
    Deflate = _compression.Deflate
    Gzip = _compression.Gzip

1. 单个service压缩

service:

context.set_compression(grpc.Compression.Gzip) # or Deflate

2. 全局压缩

grpc_server = grpc.server(
    futures.ThreadPoolExecutor(max_workers=4),
    compression=grpc.Compression.Gzip
)

with_call 有哪些参数?

with_call(request, timeout=None, metadata=None, credentials=None, wait_for_ready=None, compression=None) 

request,
timeout=None, # 超时时间
metadata=None, # 请求携带meta信息
credentials=None, # 证书相关
wait_for_ready=None, # 同步等到服务器有响应后再调用
compression=None # 数据压缩

发送数据大小限制

grpc_server = grpc.server(
    futures.ThreadPoolExecutor(max_workers=1),
    compression=grpc.Compression.Gzip,
    options=[
        ("grpc.max_send_message_length", 50 * 1024 * 1024),
        ("grpc.max_receive_message_length", 50 * 1024 * 1024),
    ]
)
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值