node

package module
v0.21.7 Latest Latest
Warning

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

Go to latest
Published: Aug 24, 2021 License: GPL-3.0 Imports: 11 Imported by: 7

README

node

a nodes ring for a key to get node

Build

  • GoDoc

Get Node Methods

Direct

dierct to get last added node, node's wight is unavailable

Consistent hashing

WIKI

Random

to get the node by random

Usage


// Node params for a node
type Node struct {
	// for recognize node with input id
	ID string
	// node's probability weight
	Weight uint32
	// node's value
	Value string
	// kvs for meta data
	Metadata config.Options

	number uint32
}

// Manager node manager functions defines.
type Manager interface {
	// adds a node to the node ring.
	Add(node *Node)
	// get the node responsible for the data key.
	NodeFor(keys ...string) (*Node, bool)
	// removes all nodes from the node ring.
	Remove()
	// removes a node from the node ring.
	RemoveByID(id string)
	// print all nodes
	PrintNodes()
	// is the node ring empty
	IsEmpty() bool
}
New a node manager
	directNode, _ := node.New(node.NodeTypeDirect, "direct")
	randomNode, _ := node.New(node.NodeTypeRandom, "random")
	consistentNode, _ := node.New(node.NodeTypeConsistent, "consistent")
	roundrobinNode, _ := node.New(node.NodeTypeRoundRobin, "roundrobin")

Or

	directNode, _ := node.NewDirect("direct")
	randomNode, _ := node.NewRandom("random")
	consistentNode, _ := node.NewConsistent("consistent")
	roundrobinNode, _ := node.NewRoundRobin("roundrobin")

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewNodes

func NewNodes(cfg config.Config) (ms map[string]Manager, err error)

NewNodes 增加Nodes节点

func NewNodesFromConfig

func NewNodesFromConfig(filepath string) (map[string]Manager, error)

NewNodesFromConfig 同步配置文件

Types

type Manager

type Manager interface {
	// adds a node to the node ring.
	Add(node *Node)
	// get the node responsible for the data key.
	NodeFor(keys ...string) (*Node, bool)
	// removes all nodes from the node ring.
	Remove()
	// removes a node from the node ring.
	RemoveByID(id string)
	// print all nodes
	PrintNodes()
	// is the node ring empty
	IsEmpty() bool
}

Manager node manager functions defines.

func New

func New(nt Type, name string) (Manager, error)

New new node manager by node type, it has no nodes

func NewConsistent

func NewConsistent(name string) (Manager, error)

NewConsistent get consistent node manager

func NewDirect

func NewDirect(name string) (Manager, error)

NewDirect get direct node manager

func NewRadmon

func NewRadmon(name string) (Manager, error)

NewRadmon get random node manager

func NewRoundRobin

func NewRoundRobin(name string) (Manager, error)

NewRoundRobin get roundrobin node manager

func NewWithNodes added in v0.21.0

func NewWithNodes(nt Type, name string, nodes []*Node) (Manager, error)

NewWithNodes new node manager by node type with nodes

type Node

type Node struct {
	// for recognize node with input id
	ID string `yaml:"id" json:"id"`
	// node's probability weight, roundrobin does not support
	Weight uint32 `yaml:"weight" json:"weight"`
	// node's value
	Value string `yaml:"value" json:"value"`
	// kvs for meta data
	Metadata config.Options `yaml:"options" json:"options"`
	// contains filtered or unexported fields
}

Node params for a node

func (*Node) Get added in v0.21.0

func (p *Node) Get(key string) (interface{}, bool)

Get get value from metadata

func (*Node) Set added in v0.21.0

func (p *Node) Set(key string, value interface{})

Set set kv pair from metadata

type Type

type Type uint8

Type define node type

const (
	NodeTypeDirect Type = iota
	NodeTypeRandom
	NodeTypeConsistent
	NodeTypeRoundRobin
)

NodeType

Jump to

Keyboard shortcuts

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