-
Notifications
You must be signed in to change notification settings - Fork 0
/
discovery.go
94 lines (76 loc) · 1.94 KB
/
discovery.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
package discovery
import (
"errors"
"fmt"
"net"
"strings"
log "github.com/Sirupsen/logrus"
)
type Entry struct {
Host string
Port string
}
func NewEntry(url string) (*Entry, error) {
host, port, err := net.SplitHostPort(url)
if err != nil {
return nil, err
}
return &Entry{host, port}, nil
}
func (m Entry) String() string {
return fmt.Sprintf("%s:%s", m.Host, m.Port)
}
type WatchCallback func(entries []*Entry)
type DiscoveryService interface {
Initialize(string, int) error
Fetch() ([]*Entry, error)
Watch(WatchCallback)
Register(string) error
}
var (
discoveries map[string]DiscoveryService
ErrNotSupported = errors.New("discovery service not supported")
ErrNotImplemented = errors.New("not implemented in this discovery service")
)
func init() {
discoveries = make(map[string]DiscoveryService)
}
func Register(scheme string, d DiscoveryService) error {
if _, exists := discoveries[scheme]; exists {
return fmt.Errorf("scheme already registered %s", scheme)
}
log.WithField("name", scheme).Debug("Registering discovery service")
discoveries[scheme] = d
return nil
}
func parse(rawurl string) (string, string) {
parts := strings.SplitN(rawurl, "://", 2)
// nodes:port,node2:port => nodes://node1:port,node2:port
if len(parts) == 1 {
return "nodes", parts[0]
}
return parts[0], parts[1]
}
func New(rawurl string, heartbeat int) (DiscoveryService, error) {
scheme, uri := parse(rawurl)
if discovery, exists := discoveries[scheme]; exists {
log.WithFields(log.Fields{"name": scheme, "uri": uri}).Debug("Initializing discovery service")
err := discovery.Initialize(uri, heartbeat)
return discovery, err
}
return nil, ErrNotSupported
}
func CreateEntries(addrs []string) ([]*Entry, error) {
entries := []*Entry{}
if addrs == nil {
return entries, nil
}
for _, addr := range addrs {
entry, err := NewEntry(addr)
if err != nil {
return nil, err
}
entries = append(entries, entry)
}
return entries, nil
}