github.com/graybobo/golang.org-package-offline-cache@v0.0.0-20200626051047-6608995c132f/x/talks/2015/gotham-grpc.slide (about) 1 gRPC Go 2 GothamGo 2015 3 4 Sameer Ajmani 5 Tech Lead Manager, Go team, Google 6 @Sajma 7 sameer@golang.org 8 9 * Video 10 11 This talk was presented at GothamGo in New York City, October 2015. 12 13 .link https://www.youtube.com/watch?v=vTIyz2QfExc&index=7&list=PLeGxIOPLk9ELh9tsPZMzau6CzMjfMzp9- Watch the talk on YouTube 14 15 * RPC isn't just Remote Procedure Call 16 17 In Go, an RPC *starts*a*goroutine* running on the server and provides _message_passing_ between the client and server goroutines. 18 19 *Unary*RPC*: the client sends a _request_ to the server, then the server sends a _response_. 20 21 *Streaming*RPC:* the client and server may each send one or more messages. 22 23 An RPC ends when: 24 25 - both sides are done sending messages 26 - either side disconnects 27 - the RPC is canceled or times out 28 29 This talk will show how we connect RPCs and streams with goroutines and channels. 30 31 * Unary RPC: one request, one response 32 33 Example: a mobile Maps app requests a route from point A to point B. 34 35 On the client side, an RPC blocks until it's done or canceled. 36 37 A client uses multiple goroutines to run many RPCs simultaneously. 38 39 Each RPC is an exchange between a client goroutine and a server goroutine. 40 41 * Streaming RPC provides bidirectional message-passing 42 43 A client starts a stream with a server. 44 45 Messages sent on a stream are delivered FIFO. 46 47 Many streams can run simultaneously between the same client and server. 48 49 The transport provides buffering and flow control. 50 51 Examples: 52 53 - bidirectional stream: chat session 54 - server → client stream: stock ticker 55 - client → server stream: sensor aggregation 56 57 * gRPC is a new RPC system from Google 58 59 .link http://grpc.io 60 61 Provides RPC and streaming RPC 62 63 Ten languages: *C*, *Java*, *Go*, C++, Node.js, Python, Ruby, Objective-C, PHP, and C# 64 IDL: *Proto3* 65 Transport: *HTTP2* 66 67 [[http://golang.org/x/net/context][golang.org/x/net/context]] for deadlines, cancelation, and request-scoped values 68 [[http://golang.org/x/net/trace][golang.org/x/net/trace]] for real-time request traces and connection logging 69 70 * gRPC users 71 72 150+ imports of [[https://godoc.org/google.golang.org/grpc?importers][google.golang.org/grpc]] on [[http://godoc.org][godoc.org]] 73 74 - [[https://github.com/apcera/kurma][Apcera/Kurma]]: container OS 75 - [[http://bazil.org][Bazil]]: distributed file system 76 - [[http://coreos.com/etcd/][CoreOS/Etcd]]: distributed consistent key-value store 77 - [[https://godoc.org/google.golang.org/cloud/bigtable][Google Cloud Bigtable]]: sparse table storage 78 - [[https://github.com/monetas/bmd][Monetas/Bitmessage]]: transaction platform 79 - [[http://www.pachyderm.io/][Pachyderm]]: containerized data analytics 80 - [[http://vitess.io/][YouTube/Vitess]]: storage platform for scaling MySQL 81 82 * Demos and Code: Google search 83 84 * Protocol definition 85 86 .code gotham-grpc/search-only/search-only.proto 87 88 * Generated code 89 90 .code gotham-grpc/search/README.md /protoc/ 91 .code gotham-grpc/search-only/search-only.pb.go /type GoogleClient/,/^}/ 92 .code gotham-grpc/search-only/search-only.pb.go /type GoogleServer/,/^}/ 93 .code gotham-grpc/search-only/search-only.pb.go /type Request/,/^}/ 94 .code gotham-grpc/search-only/search-only.pb.go /type Result/,/^}/ 95 96 * System diagram 97 98 .image gotham-grpc/system.svg _ 1000 99 100 * Frontend runs Search on both backends and returns first result 101 102 .image gotham-grpc/search.svg _ 1000 103 104 * Demo client --mode=search 105 106 - Frontend request traces 107 - Backend request traces 108 - Connection event logs 109 110 * Client code 111 112 .image gotham-grpc/client.svg _ 1000 113 114 * Client code (main) 115 116 import pb "golang.org/x/talks/2015/gotham-grpc/search" 117 118 .code gotham-grpc/client/client.go /func main/,/^}/ 119 120 * Client code (search) 121 122 .code gotham-grpc/client/client.go /func search/,/^}/ 123 124 RPCs block but can be canceled using a Context. 125 126 gRPC propagates cancelation from client to server. 127 128 * Backend code 129 130 .image gotham-grpc/backend.svg _ 1000 131 132 * Backend code (main) 133 134 .code gotham-grpc/backend/backend.go /net.Listen/,/g.Serve/ 135 136 `new(server)` must implement the `GoogleServer` interface: 137 138 .code gotham-grpc/search/search.pb.go /type GoogleServer/,/^}/ 139 140 Each call to `Search` or `Watch` runs in its own goroutine. 141 142 * Backend code (search) 143 144 `ctx.Done` is closed when the RPC is canceled, times out, or returns: 145 146 .code gotham-grpc/backend/backend.go /[)] Search/,/^}/ 147 148 If tracing is enabled, log the sleep duration: 149 150 .code gotham-grpc/backend/backend.go /func logSleep/,/^}/ 151 152 * Frontend code 153 154 .image gotham-grpc/frontend.svg _ 1000 155 156 * Frontend code (search) 157 158 `Search` returns as soon as it gets the first result. 159 gRPC cancels the remaining `backend.Search` RPCs by via `ctx`: 160 161 .code gotham-grpc/frontend/frontend.go /[)] Search/,/^}/ 162 163 .code gotham-grpc/frontend/frontend.go /type result/,/^}/ 164 165 * Streaming RPC 166 167 * Add Watch to the Google service 168 169 .code gotham-grpc/search/search.proto 170 171 * Generated code 172 173 .code gotham-grpc/search/search.pb.go /type GoogleClient/,/^}/ 174 .code gotham-grpc/search/search.pb.go /type GoogleServer/,/^}/ 175 .code gotham-grpc/search/search.pb.go /type Google_WatchClient/,/^}/ 176 .code gotham-grpc/search/search.pb.go /type Google_WatchServer/,/^}/ 177 178 * Frontend runs Watch on both backends and merges results 179 180 .image gotham-grpc/watch.svg _ 1000 181 182 * Demo client --mode=watch 183 184 - Active stream traces 185 - Cancelation 186 187 * Client code 188 189 .image gotham-grpc/client.svg _ 1000 190 191 * Client code (watch) 192 193 .code gotham-grpc/client/client.go /func watch/,/^}/ 194 195 * Backend code 196 197 .image gotham-grpc/backend.svg _ 1000 198 199 * Backend code (watch) 200 201 .code gotham-grpc/backend/backend.go /[)] Watch/,/^}/ 202 203 * Frontend code 204 205 .image gotham-grpc/frontend.svg _ 1000 206 207 * Frontend code (watch) 208 209 .code gotham-grpc/frontend/frontend.go /[)] Watch/,/return nil/ 210 211 * Frontend code (watchBackend) 212 213 `Watch` returns on first error; this closes `ctx.Done` and signals `watchBackend` to exit. 214 215 .code gotham-grpc/frontend/frontend.go /func watch/,/^}/ 216 217 * gRPC extends the Go programming model over the network 218 219 Go gRPC works smoothly with goroutines, channels, and cancelation. 220 221 It is an excellent fit for building parallel, distributed, and streaming systems. 222 223 .image gotham-grpc/watch.svg _ 1000 224 225 * References 226 227 - [[http://grpc.io][grpc.io]] - gRPC reference and tutorials 228 - [[https://github.com/golang/protobuf][github.com/golang/protobuf]] - Protocol buffers 229 - [[http://golang.org/x/net/http2][golang.org/x/net/http2]] - HTTP2 230 - [[http://golang.org/x/net/trace][golang.org/x/net/trace]] - Request traces and event logs 231 - [[http://golang.org/x/net/context][golang.org/x/net/context]] - Cancelation and request-scoped data 232 - [[http://blog.golang.org/pipelines][blog.golang.org/pipelines]] - Streaming data pipelines 233 234 *Thanks*to* Qi Zhao, David Symonds, Brad Fitzpatrick, and the rest. 235 236 *Questions?* 237 238 Sameer Ajmani 239 Tech Lead Manager, Go team, Google 240 [[twitter.com/Sajma][@Sajma]] 241 [[mailto:sameer@golang.org][sameer@golang.org]]