Skip to content
/ planb Public

Build distributed, low-latency services with a redis-compatible protocol and sentinel client support

License

Notifications You must be signed in to change notification settings

bsm/planb

Repository files navigation

Plan B

GoDoc Build Status Go Report Card License

Plan B is a toolkit for building distributed, low-latency services that speak RESP (REdis Serialization Protocol). Under the hood, it is wrapping Redeo and Raft to create a concise interface for custom commands.

Examples

A simple server example:

package main

import (
  "fmt"

  "github.com/bsm/planb"
  "github.com/hashicorp/raft"

)

func main() {
	// Open a store
	store := planb.NewInmemStore()

	// Init config
	conf := planb.NewConfig()
	conf.Sentinel.MasterName = "mymaster"	// handle SENTINEL commands

	// Init server
	srv, err := planb.NewServer("10.0.0.1:7230", ".", store, raft.NewInmemStore(), raft.NewInmemStore(), conf)
	if err != nil {
		panic(err)
	}

	// Setup SET handler
	srv.HandleRW("SET", nil, redeo.WrapperFunc(func(cmd *resp.Command) interface{} {
		if len(cmd.Args) != 2 {
			return redeo.ErrWrongNumberOfArgs(cmd.Name)
		}

		if err := store.Put(cmd.Args[0], cmd.Args[1]); err != nil {
			return err
		}
		return "OK"
	}))

	// Setup GET handler
	srv.HandleRO("GET", nil, redeo.WrapperFunc(func(cmd *resp.Command) interface{} {
		if len(cmd.Args) != 1 {
			return redeo.ErrWrongNumberOfArgs(cmd.Name)
		}

		val, err := store.Get(cmd.Args[0])
		if err != nil {
			return err
		}
		return val
	}))

	// Start serving
	if err := srv.ListenAndServe(); err != nil {
		panic(err)
	}
}

About

Build distributed, low-latency services with a redis-compatible protocol and sentinel client support

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published