-
Notifications
You must be signed in to change notification settings - Fork 0
/
sample_test.go
48 lines (37 loc) · 1.25 KB
/
sample_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
package chash_test
import (
"chash"
"fmt"
"testing"
)
func TestSample(t *testing.T) {
// First we create our own implementation of node capable of carrying around the information we care about.
// this might be a handle to a connection pool, an open connection or just a pile of connection information
nodes := make([]chash.Node, 10)
// In this sample we're reusing the SqlConn object. In real-life each node would likely have it's own connection
var conn SqlConn
for i := 0; i < len(nodes); i++ {
nodes[i] = NewSqlNode(i, conn)
}
// Create a new ring out of the provided nodes.
// If you're previously sharded the ring, it's possible to create a ring directly out of RingNode objects using NewRing2.
ring := chash.NewRing(nodes)
// Hash a value, get a node.
// The returned value is of type Node, but can always be type asserted into the node type used when creating the ring
node := ring.GetNode([]byte("foo")).(*SqlNode)
// Yay! It worked
fmt.Printf("Found Node of type %T with values %#v\n", node, node)
}
type SqlConn interface {
Query(query string) (interface{}, error)
}
type SqlNode struct {
id int
conn SqlConn
}
func NewSqlNode(id int, conn SqlConn) *SqlNode {
return &SqlNode{id, conn}
}
func (me *SqlNode) Id() int {
return me.id
}