Route and Balance Requests with Pickers

Distributed Services with Go — by Travis Jeffery (66 / 84)

The Pragmatic Programmers
The Pragmatic Programmers

--

👈 Resolve the Servers | TOC | Test Discovery and Balancing End-to-End 👉

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:

ClientSideServiceDiscovery/internal/loadbalance/picker.go

​ ​package​ loadbalance

​ ​import​ (
​ ​"strings"​
​ ​"sync"​
​ ​"sync/atomic"​

​ ​"google.golang.org/grpc/balancer"​
​ ​"google.golang.org/grpc/balancer/base"​
​ )

​ ​var​ _ base.PickerBuilder = (*Picker)(nil)

​ ​type​ Picker ​struct​ {
​ mu sync.RWMutex
​ leader balancer.SubConn
​ followers []balancer.SubConn
​ current ​uint64​
​ }

​ ​func​ (p *Picker) Build(buildInfo base.PickerBuildInfo) balancer.Picker {
​ p.mu.Lock()
​ ​defer​ p.mu.Unlock()
​ ​var​ followers []balancer.SubConn
​ ​for​ sc, scInfo := ​range​ buildInfo.ReadySCs {
​ isLeader := scInfo.
​ Address.
​ Attributes.
​…

--

--

The Pragmatic Programmers
The Pragmatic Programmers

We create timely, practical books and learning resources on classic and cutting-edge topics to help you practice your craft and accelerate your career.