raft-grpc-leader-rpc

module
v1.0.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 9, 2020 License: BSD-2-Clause

README

raft-grpc-leader-rpc

Godoc

Send gRPCs to your Raft leader.

It connects to all your Raft nodes and uses client-side health checks to only send RPCs to the master.

During leader elections you'll see errors, make sure your client can handle those and retries them.

Server side

Add this to your server:

import "github.com/Jille/raft-grpc-leader-rpc/leaderhealth"

r, err := raft.NewRaft(...)
s := grpc.NewServer()

services := []string{""}
leaderhealth.Setup(r, s, services)

Use "" to mark all gRPC services as unhealthy if you aren't the master. Otherwise pass only the service names that you want to control healthiness for.

If you don't know what to choose, consider using the (hereby) standardized quis.RaftLeader service name.

Want to read more about health checking?

Client side

You need to explicitly configure your clients to look at health checks.

Add this to your client:

import _ "google.golang.org/grpc/health"

c := `{"healthCheckConfig": {"serviceName": "your-service-name-or-an-empty-string"}, "loadBalancingConfig": [ { "round_robin": {} } ]}`
conn, err := grpc.Dial("dns://all-your-raft-nodes.example.com", grpc.WithDefaultServiceConfig(c))

Pick any of the service names you registered on the server (possibly the empty string if you used that).

You'll need to create a DNS entry that points to all your Raft nodes. If you don't feel like doing that, you can use this instead:

import _ "github.com/Jille/grpc-multi-resolver"
import _ "google.golang.org/grpc/health"

c := `{"healthCheckConfig": {"serviceName": "your-service-name-or-an-empty-string"}, "loadBalancingConfig": [ { "round_robin": {} } ]}`
conn, err := grpc.Dial("multi:///127.0.0.1:50051,127.0.0.1:50052,127.0.0.1:50053", grpc.WithDefaultServiceConfig(c))
Wait for Ready

I recommend enabling Wait for Ready by adding grpc.WithDefaultCallOption(grpc.WithWaitForReady(true)) to your grpc.Dial(). This lets gRPC wait for a connection to the leader rather than immediately failing it if the leader is currently unknown. The deadline is still honored.

Directories

Path Synopsis
Package leaderhealth is included in your Raft nodes to expose whether this node is the leader.
Package leaderhealth is included in your Raft nodes to expose whether this node is the leader.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL