Documentation ¶
Index ¶
- type Config
- type HandlerOpts
- type InmemStore
- type RaftCtrl
- type Server
- func (s *Server) Close() error
- func (s *Server) HandleRO(name string, opt *HandlerOpts, h redeo.Handler)
- func (s *Server) HandleRW(name string, opt *HandlerOpts, h redeo.Handler)
- func (s *Server) Info() *redeo.ServerInfo
- func (s *Server) ListenAndServe() error
- func (s *Server) Raft() RaftCtrl
- func (s *Server) Serve(lis net.Listener) error
- type Store
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Config ¶
type Config struct { // Raft configuration options Raft *raft.Config // Transport configuration options Transport *redeoraft.Config // Sentinel configuration Sentinel struct { // MasterName must be set to enable sentinel support MasterName string } }
Config contains server config directives
type HandlerOpts ¶
type HandlerOpts struct { // Timeout is an optional timeout for mutating commands. It indicates // the maximum duration the server is willing to wait for the application // of the command. Minimum: 1s, default: 10s. Timeout time.Duration }
HandlerOpts contain options for handler execution.
type InmemStore ¶
type InmemStore struct {
// contains filtered or unexported fields
}
func NewInmemStore ¶
func NewInmemStore() *InmemStore
NewInmemStore opens a new simplistic, non-transactional in-memory KVStore
type RaftCtrl ¶
type RaftCtrl interface { // AppliedIndex returns the last index applied to the FSM. AppliedIndex() uint64 // GetConfiguration returns the latest configuration and its associated index currently in use. GetConfiguration() raft.ConfigurationFuture // LastContact returns the time of last contact by a leader. LastContact() time.Time // LastIndex returns the last index in stable storage, either from the last log or from the last snapshot. LastIndex() uint64 // Leader is used to return the current leader of the cluster. It may return empty string if there is no current leader or the leader is unknown. Leader() raft.ServerAddress // State is used to return the current raft state. State() raft.RaftState // Stats is used to return a map of various internal stats. Stats() map[string]string // String returns a string representation of this Raft node. String() string }
RaftCtrl is an interface to the underlying raft node controller
type Server ¶
type Server struct {
// contains filtered or unexported fields
}
Server implements a peer
Example ¶
package main import ( "github.com/bsm/planb" "github.com/bsm/redeo" "github.com/bsm/redeo/resp" "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) } }
Output:
func NewServer ¶
func NewServer(advertise raft.ServerAddress, dir string, store Store, logs raft.LogStore, stable raft.StableStore, conf *Config) (*Server, error)
NewServer initializes a new server instance. Each server must advertise an address and use a local dir location and a key-value store for persistence. It also accepts a log and a stable store.
func (*Server) HandleRO ¶
func (s *Server) HandleRO(name string, opt *HandlerOpts, h redeo.Handler)
HandleRO handles readonly commands
func (*Server) HandleRW ¶
func (s *Server) HandleRW(name string, opt *HandlerOpts, h redeo.Handler)
HandleRW handles commands that may result in modifications. These can only be applied to the master node and are then replicated to slaves.
func (*Server) ListenAndServe ¶
ListenAndServe starts listening and serving on the advertised address.
type Store ¶
type Store interface { // Restore restores the store from a data stream. Restore(r io.Reader) error // Snapshot writes a snapshot to the provided writer. Snapshot(w io.Writer) error }
Store is an abstraction of an underlying store implementation. It must have snapshot and restore capabilities.