You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

concurrency.md 1.5 KiB

123456789101112131415161718192021222324252627282930313233
  1. # Concurrency
  2. In general, gRPC-go provides a concurrency-friendly API. What follows are some
  3. guidelines.
  4. ## Clients
  5. A [ClientConn][client-conn] can safely be accessed concurrently. Using
  6. [helloworld][helloworld] as an example, one could share the `ClientConn` across
  7. multiple goroutines to create multiple `GreeterClient` types. In this case, RPCs
  8. would be sent in parallel.
  9. ## Streams
  10. When using streams, one must take care to avoid calling either `SendMsg` or
  11. `RecvMsg` multiple times against the same [Stream][stream] from different
  12. goroutines. In other words, it's safe to have a goroutine calling `SendMsg` and
  13. another goroutine calling `RecvMsg` on the same stream at the same time. But it
  14. is not safe to call `SendMsg` on the same stream in different goroutines, or to
  15. call `RecvMsg` on the same stream in different goroutines.
  16. ## Servers
  17. Each RPC handler attached to a registered server will be invoked in its own
  18. goroutine. For example, [SayHello][say-hello] will be invoked in its own
  19. goroutine. The same is true for service handlers for streaming RPCs, as seen
  20. in the route guide example [here][route-guide-stream].
  21. [helloworld]: https://github.com/grpc/grpc-go/blob/master/examples/helloworld/greeter_client/main.go#L43
  22. [client-conn]: https://godoc.org/google.golang.org/grpc#ClientConn
  23. [stream]: https://godoc.org/google.golang.org/grpc#Stream
  24. [say-hello]: https://github.com/grpc/grpc-go/blob/master/examples/helloworld/greeter_server/main.go#L41
  25. [route-guide-stream]: https://github.com/grpc/grpc-go/blob/master/examples/route_guide/server/server.go#L126