ring

package module
v0.0.0-...-981b071 Latest Latest
Warning

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

Go to latest
Published: Sep 14, 2015 License: MIT Imports: 2 Imported by: 0

README

goring

A simple consistent hashing ring implementation in Golang

#Usage

  import(
    "fmt"
    "github.com/alash3al/goring"
  )
  
  func main(){
    // init
    ring := new ring.NewRing()
    
    // add node with its weight in the ring
    ring.Add("127.0.0.1:8000", 2)

    // add more nodes
    ring.Add("127.0.0.1:8001", 1).Add("127.0.0.1:8002", 5) // ...

    // find a node by anonoymous key
    node := ring.Get("usr:alash3al:name")

    // print it
    fmt.Println(node)

    // remove a node
    node.Remove("127.0.0.1:8002")
  }

Documentation

Overview

ring is a simple consistent hashing package

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Node

type Node struct {
	Value  string
	Weight int
}

Node is a frame that contain the real-node value and weight

type Ring

type Ring struct {
	Nodes map[uint32]Node
	Ring  []Vnode
}

Ring is our main ring that contains the real/virtual nodes

func NewRing

func NewRing() *Ring

Create a new ring

func (*Ring) Add

func (this *Ring) Add(node string, weight int) *Ring

Add new node with a {weight "how many replicas ?"}

func (*Ring) Get

func (this *Ring) Get(key string) string

Return the key's node

func (*Ring) Hash

func (this *Ring) Hash(key string) uint32

Return the hash of the specified key

func (*Ring) Remove

func (this *Ring) Remove(node string) *Ring

Remove a node from the ring

type Vnode

type Vnode struct {
	Real    uint32
	Virtual uint32
}

Vnode is frame that contains the real-node "hash" and its new virtual hash

Jump to

Keyboard shortcuts

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