UNPKG

ketama

Version:

A hash ring implementation using libketama-like hashing.

49 lines (48 loc) 1.93 kB
/// <reference types="node" /> /** * Function that returns an int32 hash of the input (a number between * -2147483648 and 2147483647). If your hashing library gives you a Buffer * back, a convienent way to get this is `buf.readInt32BE()`. */ export declare type HashFunction = (input: Buffer) => number; export declare class HashRing<TNode extends string | { key: string; } = string> { /** * Base weight of each node in the hash ring. Having a base weight of 1 is * not very desirable, since then, due to the ketama-style "clock", it's * possible to end up with a clock that's very skewed when dealing with a * small number of nodes. Setting to 50 nodes seems to give a better * distrubtion, so that load is spread roughly evenly to +/- 5%. */ static baseWeight: number; private readonly hashFn; private clock; private nodes; constructor(initialNodes?: ReadonlyArray<TNode | { weight: number; node: TNode; }>, hashFn?: string | HashFunction); /** * Add a new node to the ring. If the node already exists in the ring, it * will be updated. For example, you can use this to update the node's weight. */ addNode(node: TNode, weight?: number): void; /** * Removes th enode from the ring. No-op's if the node does not exist. */ removeNode(node: TNode): void; /** * Gets the node which should handle the given input. Returns undefined if * the hashring has no elements with weight. */ getNode(input: string | Buffer): TNode | undefined; private getIndexForInput; /** * Gets the "replicas" number of nodes that should handle the input. The * returned array length wiill equal the number of replicas, except if * there are fewer nodes available than replicas requested. */ getNodes(input: string, replicas: number): TNode[]; private addNodeToClock; }