🚀 Launch Week Day 4:Introducing the Alert Details Page: A Better Way to Explore Alerts.Learn More →
Socket
Book a DemoInstallSign in
Socket

stvz.io/hashring

Package Overview
Dependencies
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

stvz.io/hashring

Go Modules
Version
v0.1.0
Version published
Created
Source

hashring

Provides a simple implementation of a consistent hash ring. By default it uses the 32-bit cyclic redundancy check (CRC-32) for calculating checksums, but can take any function that takes a byte slice and returns an unsigned 32bit integer. The current implementation, is not as efficient as it could be and can also be prone to hotspots though it should consistently distribute across the nodes +/- 3 percent (dependent on the number of replicates).

Install

go get stvz.io/hashring

Usage

import "stvz.io/hashring"

// Initialize the ring with 3 replicas, using the default checksum function and
// add some nodes to the ring.
ring := hashring.New(3, nil)
ring.Add("server1", "server2", "server3", "server4", "server5")

To get the node from the ring, use the Get(key string) function. If there are no nodes available, the method returns an empty string.

if node := ring.Get("id"); node != "" {
  sendFn(node, data)
}

For pull style distributed applications, the function Mine(name, key string) exists to determine whether or not the key that is being pulled belongs to the worker. It returns a boolean value. Assuming that the nodes use the hostname of the worker the following is an example:

hostname, _ := os.Hostname()
if ring.Mine(hostname, "id") {
  processFn(data)
}

To remove a node (or nodes) use the Remove(node string) function.

ring.Remove("server1", "server2")

FAQs

Package last updated on 05 Nov 2023

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts