hashring

package module
v0.0.0-...-b1a4246 Latest Latest
Warning

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

Go to latest
Published: Jul 11, 2020 License: Apache-2.0 Imports: 6 Imported by: 0

README

hashring

一致性 Hash 环

package main

import (
    "hashring"
    "fmt"
)

func main() {
    hashRing := hashring.NewHashRing(150)
    hashRing.AddNodes(map[string]int{
        "192.168.1.101": 1,
        "192.168.1.101": 2,
        "192.168.1.101": 3
    })
    nodeName, _ := hashRing.NodeLoadBalance(key)
    logger.Infof("node load balance , node name %s \n", nodeName)
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type HashRing

type HashRing struct {
	VirtualNodeNum int
	RealNodeMap    map[string]*Node
	// contains filtered or unexported fields
}

HashRing 哈希环

func NewHashRing

func NewHashRing(virtualNodeNum int) (*HashRing, error)

NewHashRing 新建哈希环

func (*HashRing) AddNode

func (h *HashRing) AddNode(nodeName string, nodeWeight int)

AddNode 增加节点

func (*HashRing) AddNodes

func (h *HashRing) AddNodes(nodeWeightMap map[string]int)

AddNodes 批量增加节点

func (*HashRing) NodeLoadBalance

func (h *HashRing) NodeLoadBalance(key string) (string, error)

NodeLoadBalance 节点负载均衡

func (*HashRing) RemoveNode

func (h *HashRing) RemoveNode(nodeName string)

RemoveNode 删除节点

type Node

type Node struct {
	Name   string
	Weight int
}

Node 节点

type VirtualNode

type VirtualNode struct {
	Node
	VirtualName string
	HashValue   uint32
}

VirtualNode 虚拟节点

Jump to

Keyboard shortcuts

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