Giter Site home page Giter Site logo

raft-grpc-leader-rpc's Introduction

raft-grpc-leader-rpc

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 there will temporarily be no leader and you'll see errors, make sure your client can handle those and retries them.

Server side

Godoc

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 enable health checking.

Add this to your client:

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

c := `{"healthCheckConfig": {"serviceName": "quis.RaftLeader"}, "loadBalancingConfig": [ { "round_robin": {} } ]}`
target := "dns://all-your-raft-nodes.example.com"
conn, err := grpc.Dial(target, grpc.WithDefaultServiceConfig(c))

Instead of quis.RaftLeader you can also pick any of the service names you've registered with leaderhealth.Setup().

You can also configure the ServiceConfig in DNS if you want to.

You'll need to create a DNS entry that points to all your Raft nodes.

No DNS entry?

If you don't have a DNS entry, check out https://github.com/Jille/grpc-multi-resolver. Usage is easy.

import _ "github.com/Jille/grpc-multi-resolver"

target := "multi:///127.0.0.1:50051,127.0.0.1:50052,127.0.0.1:50053"

Wait for Ready

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

If you get errors like connection active but health check failed., this is what you want to enable.

Automatic retries

You can use https://godoc.org/github.com/grpc-ecosystem/go-grpc-middleware/retry to transparently retry failures without the client code knowing it.

You should enable Wait for Ready, otherwise it might burn through all the retries before there is a new leader.

Add this to your client:

import "github.com/grpc-ecosystem/go-grpc-middleware/retry"

retryOpts := []grpc_retry.CallOption{
	grpc_retry.WithBackoff(grpc_retry.BackoffExponential(100 * time.Millisecond)),
	grpc_retry.WithMax(5), // Give up after 5 retries.
}
grpc.Dial(..., grpc.WithUnaryInterceptor(grpc_retry.UnaryClientInterceptor(retryOpts...)))

Your server will need some modifications too. Each of your RPCs needs to return an appropriate status code.

Godoc

Make sure to read rafterrors' documentation to know when to use MarkRetriable vs MarkUnretriable, there's a pitfall.

raft-grpc-leader-rpc's People

Contributors

jille avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.