Route and Balance Requests with Pickers

In the gRPC architecture, pickers handle the RPC balancing logic. They’re called pickers because they pick a server from the servers discovered by the resolver to handle each RPC. Pickers can route RPCs based on information about the RPC, client, and server, so their utility goes beyond balancing to any kind of request-routing logic.

To implement the picker builder, create a file named picker.go in internal/loadbalance that begins with this code:

 package​ loadbalance
 
 import​ (
 "strings"
 "sync"
 "sync/atomic"
 
 "google.golang.org/grpc/balancer"
 "google.golang.org/grpc/balancer/base"
 )
 
 var​ _ base.PickerBuilder = (*Picker)(nil) ...

Get Distributed Services with Go now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.