forked from coreos/torus
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ring_main.go
59 lines (46 loc) · 1.15 KB
/
ring_main.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
49
50
51
52
53
54
55
56
57
58
59
package ring
import (
"github.com/coreos/torus"
"github.com/coreos/torus/models"
)
const (
Empty torus.RingType = iota
Single
Mod
Union
Ketama
)
func Unmarshal(b []byte) (torus.Ring, error) {
var a models.Ring
err := a.Unmarshal(b)
if err != nil {
return nil, err
}
return CreateRing(&a)
}
type createRingFunc func(r *models.Ring) (torus.Ring, error)
var ringRegistry map[torus.RingType]createRingFunc
var ringNames map[string]torus.RingType
func registerRing(t torus.RingType, name string, newFunc createRingFunc) {
if ringRegistry == nil {
ringRegistry = make(map[torus.RingType]createRingFunc)
}
if _, ok := ringRegistry[t]; ok {
panic("torus: attempted to register ring type " + string(t) + " twice")
}
ringRegistry[t] = newFunc
if ringNames == nil {
ringNames = make(map[string]torus.RingType)
}
if _, ok := ringNames[name]; ok {
panic("torus: attempted to register ring name " + name + " twice")
}
ringNames[name] = t
}
func CreateRing(r *models.Ring) (torus.Ring, error) {
return ringRegistry[torus.RingType(r.Type)](r)
}
func RingTypeFromString(s string) (torus.RingType, bool) {
v, ok := ringNames[s]
return v, ok
}