grpc的那些事儿

  • 14 浏览

rfyiamcool

2020/11/20 发布于 技术 分类

gprc的实现原理, xiaorui.cc

grpc  http2  http3  golang grpc 

文字内容
1. grpc 的那些事儿 - xiaorui.cc - github.com/rfyiamcool
2. 1 http2 design 2 protocol buffer 3 grpc design 4 grpc optimize
3. what is grpc ? grpc-client in mobile grpc-web in chrome envoy grpc-client in backend nginx grpc-client grpc-client grpc-server grpc-client grpc-servergrpc-server grpc-server grpc-client
4. what is grpc ? feature High compatibility high performance verty simple rpc framework http2 protobuf code generated
5. 1 http 2.0
6. what is RTT ? RTT (Round Trip Time) Round trip time for a packet rtt = recvtime - sendtime http request cost N rtt dns http tcp handshake http redirect https https tcp handshake tls handshake ocsp (dns query + tcp handshake + req/resp )
7. select http2 ? (官⽅说法) 最⾼兼容性 多路复用 TLS 较好的性能 😅 more
8. http1.0 vs http1.1 http 1.1 http 1.0 古董 ... 持久化连接 缓存 accept-range more header
9. http 1.1 的缺点 Head-of-Line Blocking (No Pipelining) 一个连接同一时间只能处理一个请求 如果当前请求阻塞,那么该连接就无法复用 http 1.1 pipeline ?
10. http 1.1 pipeline client server 未完全解决head of line blocking fifo原则, 需要等待最后的响应 no pipeline 多数http proxy不支持 client server 多数浏览器默认关闭 h1.1 pipeline Google Report https://www.chromium.org/developers/design-documents/network-stack/http-pipelining pipeline
11. 优化时延 在浏览器多开连接,提高吞吐? 同域名下chrome的连接数限制在6个 资源合并 / nginx concat 时延 base64切图 (精灵图) 吞吐 多域名拆分 流量 压缩数据 (去除无用字符,gzip压缩) cookie free …
12. http1.1 vs http2.0 时延 ! 等一个是等, 多个一起等也是等 ! http 1.1 N个排队请求 ≈ N x latency http 2.0 N个并发请求 >≈ 1 x latency
14. if don’t limit conn ? It's still a problem tcp handshake server client ..... tls handshake slow start tcp/ip protocol stack
15. http 2.0 优点 多路复用 header压缩 流控 优先级 服务端推送 …
16. definition connection stream message frame
17. 二进制分帧层
18. 多路复用 并行交错地发送多个请求,请求之间互不影响。 并行交错地发送多个响应,响应之间互不干扰。 使用一个连接并行发送多个请求和响应。
19. 多路复用
20. hpack 硬编码静态表 (1-61) 两端协商扩充动态表 (62-n) 通过传递索引号节省Header头部空间 使用Huffman对字符串压缩编码 http://http2.github.io/http2-spec/compression.html#static.table.definition
21. wirshark hpack
22. frame Length ( payload size ) Length (24) Flags (8) R (1) Type (8) Type (类型) Flags (状态) R (保留) Stream identifier (31) Frame Payload (0 …) Stream Identifier Frame Payload
23. frame types Header PUSH_PROMISE 推送 Data PRIORITY 优先级 RST_STREAM 停⽌ (由于错误) SETTINGS 连接级参数 PING GOAWAY 停⽌ WINDOW_UPDATE 流量控制 CONTINUATION 扩展header数据块
24. header frame Pad length Pad length (8) E (1) E 依赖排他 Stream dep (31) weight (8) Header Block Fragment Stream dep weight 优先级 1-256 Header Block Fragment 数据 Padding 填充字节
25. data frame pad length Pad length (8) data (*) Pading (*) data padding
26. select protocol http2 without tls (h2c) use 101 upgrade http2 over tls client hello with APLN server hello with ALPN when client and server support h2; use h2
27. HTTP 3 http2 handshark cost http2 1 rtt from tcp handshake http3 2 rtt from tls TLS 1.3 TLS 1.2 quic TCP HOL Blocking control TCP UDP IP http3 use udp protocol only need 1 rtt 1 rtt from tls 1.3
28. 2 protobuf
29. what is protobuf ? protobuf idl define generate code high performance compress binary more
30. select protobuf ? select json ? waste space select thrift ? thrift is good, but … lost type grpc from google not safe protobuf from google low performance thrift from facebook
31. pb vs json
32. json vs pb (optimize size) json pb with keyname use {}, “” :, [], space … as delimiter 12345678 is text … tags replace keyname tlv replace delimiter 12345678 is varint …
33. protobuf optimize ⾼性能 空间优化 TLV存储 节省keyname TLV编码 不需要遍历解析边界 ⽣成解析代码 减少了分隔符 数据格式更紧凑, 空间利⽤率⾼ 对数字进⾏varint, zigzag编码 初始化各tab对应的解码器 减少反射 more …
34. varint实现 0-127 1 128-16383 1 1 1 1 1 1 1 1 1 1 1 1 16384-2097151 1 1 1 1 1 1 2097152-268435455 0 https://golang.org/src/encoding/binary/varint.go 1 1 1 1 1 1 1 1 1 1 1 1
35. wire type wire type encode encode length 存储⽅式 Used For 0 Varint 变⻓ (1-10个字 节) T-V int32, int64, uint32, uint64, sint32, sint64, bool, enum 1 64-bit 固定8个字节 T-V fixed64, sfixed64, double 2 Lengthdelimited 变⻓ T-L-V string, bytes, embedded messages, packed repeated fields 3 Start group 已经弃⽤ 4 End group 5 32-bit 固定4个字节 弃⽤ groups (deprecated) 弃⽤ groups (deprecated) T-V fixed32, sfixed32, float 变长 TLV 定长 TV
36. TLV tag length value tag length (option) value field = 1 type = 2 10 xiaorui.cc field = 2 type = 0 0 111 tag = (field_number << 3) wire_type 0 bit extend byte 1-5 bit field_number 6-8 bit wire_type length encode is varint too !!! value …
37. protobuf types
38. string not compress ! zstd lz4 snappy …
39. 3 grpc
40. grpc workflow Java Protobuf Definitions define protoc compiler compile grpc server Rust Golang generate code grpc client implement
41. how to reqeust ? hello.proto import hello.pb.rs import hello.pb.go proto request grpc stub protobuf over http2 grpc server Rust Golang proto response
42. deep request goroutine writer stream goroutine stream reader goroutine goroutine single worer ensure thread safe http2 connenct stream stream stream grpc server
43. 4 type of grpc unary client streaming server streaming bidi streaming
44. unary request request client server response 一去一回
45. server streaming request client server response response response response server单边推送数据
46. client streaming request request request client server response client单边推送数据
47. bidi streaming request request request client server response response response 双向推送流数据 response
48. Interceptor try catch recovery middleware (third party) rate limiter unary interceptor log wrapper stream interceptor circuit breaker auth wrapper ……
49. other feature grpc client metadata keepalive encryption reflection … WithBalancer WithBlock WithInsecure WaitForReady WithTransportCredentials … grpc server MaxConcurrentStreams InitialWindowSize InitialConnWindowSize UnknownServiceHandler CustomCodec …
50. hello world protoc --go_out=plugins=grpc:. route_guide.proto
52. grpc request header frame metadata in header service/method in header data frame protobuf
53. grpc response
54. end flags
55. grpc resp error
56. grpc hpack
57. grpc microservice dns etcd discovery resolver Balancer Grpc stub consul zk register server watcher invoke !!! server server
58. grpc in k8s internal balancer lib use service headless coredns kube-dns etcd etcd etcd list/watch apiserver if use service proxy apiserver grpc client pool dns query list/watch Pod balancer grpc client ipvs service server server server
59. Q tcp hol Blocking grpc server grpc client connection head of line blocking send buffer 当某个tcp packet丢包, 触发重传定时器, 继而触发“拥塞发生” 其拥塞窗口降为1, 对丢包进行重传, 后进入慢启动。
60. Q futex syscall
61. optimize open multi connction tcp hol blocking single LoopyWriter lock race grpc client pool ! grpc client pool !! grpc client pool !!! bbr vs cubic use bbr abroad
62. grpc client pool stream 1 connection grpc stub stream 2 stream 3 grpc server connection head of line blocking round robin connection
63. easy test ? benchmark ghz like postman, curl fullstorydev/grpcurl uw-labs/bloomrpc ktr0731/evans more
64. rest to grpc grpc-gateway envoy custom code grpcall (only lib)
65. grpc proxy nginx, kong, apisix envoy traefik tyk …
67. Q&A - xiaorui.cc - github.com/rfyiamcool