router

package
v2.0.1+incompatible Latest Latest
Warning

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

Go to latest
Published: Aug 28, 2019 License: Apache-2.0 Imports: 16 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	//ErrInvalidRange when range syntax is invalid
	ErrInvalidRange = fmt.Errorf("invalid range")

	//ErrNotRoutable when pool can't find a match for this hash
	ErrNotRoutable = fmt.Errorf("not routable")

	//ErrNotFound when key can't be found in a pool
	ErrNotFound = fmt.Errorf("not found")

	//ErrPoolNotFound when table reference a pool that is not configured
	ErrPoolNotFound = fmt.Errorf("pool not found")

	//ErrInvalidDestination
	ErrUnknownScheme = fmt.Errorf("unknown scheme")
)
View Source
var (
	//SupportedScheme list of supported url scheme
	SupportedScheme = []string{
		"ardb", "zdb", "redis",
	}
)

Functions

func HexToBytes

func HexToBytes(s string) []byte

HexToBytes converts a hexstring to byte array

Types

type Config

type Config struct {
	Pools map[string]PoolConfig `yaml:"pools"`

	Lookup []string `yaml:"lookup"`
	Cache  []string `yaml:"cache"`
}

Config defines config file format

func NewConfig

func NewConfig(in io.Reader) (*Config, error)

NewConfig loads config from reader, expecting yaml formatted config

func NewConfigFromFile

func NewConfigFromFile(name string) (*Config, error)

NewConfigFromFile loads config from yaml file

func (*Config) Router

func (c *Config) Router(factory PoolFactory) (*Router, error)

Router returns a router that corresponds to configuration object

func (*Config) Valid

func (c *Config) Valid() error

Valid validate config structure

type Destination

type Destination *url.URL

Destination defines a route destination

func NewDestination

func NewDestination(dest string) (Destination, error)

NewDestination parse and validate destination

type Errors

type Errors []error

Errors holds many errors at once, suitable for config validation

func (Errors) Add

func (e Errors) Add(err error) Errors

Add add error to errors

func (Errors) Error

func (e Errors) Error() string

func (Errors) HasErrors

func (e Errors) HasErrors() bool

type Pool

type Pool interface {
	Range
	Route(h []byte) Destination
	Get(key []byte) ([]byte, error)
	Set(key []byte, data []byte) error
}

Pool defines a pool interface

func NewScanPool

func NewScanPool(rules ...Rule) Pool

NewScanPool initialize a new scan pool

type PoolConfig

type PoolConfig map[string]string

PoolConfig is a map from hash-range to destination

type PoolFactory

type PoolFactory func(...Rule) Pool

PoolFactory defines a pool factory method

var (
	//DefaultPoolFactory default pool implementation
	DefaultPoolFactory PoolFactory = NewScanPool
)

type Range

type Range interface {
	In(h []byte) bool
}

Range defines a hash range matcher

func NewRange

func NewRange(r string) (Range, error)

NewRange parse range from string

type Router

type Router struct {
	// contains filtered or unexported fields
}

Router defines a router engine. The router will try the pools in the same order defined by the table. And write to cache if wasn't retrieved from the cache pool

func Merge

func Merge(routers ...*Router) *Router

Merge merge multiple routers

func (*Router) Get

func (r *Router) Get(key []byte) (io.ReadCloser, error)

Get gets key from table

func (*Router) String

func (r *Router) String() string

type Rule

type Rule struct {
	Range
	Destination Destination
}

Rule defines a hash routing rule

type ScanPool

type ScanPool struct {
	Rules []Rule
	// contains filtered or unexported fields
}

ScanPool defines a set of routing rules This implementation of pool does a sequential scan of the rules. That's not very efficient usually plus it always returns the first match.

More sophisticated implementation of the pool should balance the routing if more than rule matches the hash.

func (*ScanPool) Get

func (p *ScanPool) Get(key []byte) ([]byte, error)

Get key from pool

func (*ScanPool) In

func (p *ScanPool) In(h []byte) bool

In checks if hash is in pool

func (*ScanPool) Route

func (p *ScanPool) Route(h []byte) Destination

Route matches hash against the pool and return the first matched destination

func (*ScanPool) Routes

func (p *ScanPool) Routes(h []byte) []Destination

Routes returns all possible destinations for hash h.

func (*ScanPool) Set

func (p *ScanPool) Set(key, data []byte) error

Set key to data

func (*ScanPool) String

func (p *ScanPool) String() string

Jump to

Keyboard shortcuts

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