-
Notifications
You must be signed in to change notification settings - Fork 3
/
discovery.go
66 lines (55 loc) · 1.54 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
/*
package discovery defines common structs and interfaces for discovering and announcing
RPC services.
*/
package discovery
import (
"fmt"
"github.com/pborman/uuid"
)
// Resolver describes something that can resolve services from name.
type Resolver interface {
Resolve(string) (*Service, error)
}
// Announcer describes something that can announce a service instance to make it
// available for discovery.
type Announcer interface {
Announce(string, Instance) error
}
// AnnounceResolver groups Announcer and Resolver interfaces.
type AnnounceResolver interface {
Resolver
Announcer
}
// Service represents an RPC service, which is an abstract definition of
// RPC instances announced under the same name.
type Service struct {
Name string
Instances []Instance
}
// String returns a representation string of service.
func (s *Service) String() string {
return fmt.Sprintf("%s [%d]", s.Name, len(s.Instances))
}
// TODO: Add possibility to update announced instance metadata.
// Instance represents an RPC instance, usually this map to one process.
type Instance struct {
ID string
Endpoint string
Meta InstanceMeta
}
// NewInstance creates a new Instance.
func NewInstance(endpoint string, meta InstanceMeta) Instance {
// FIXME: How about if meta have "endpoint" ?
if meta == nil {
meta = make(InstanceMeta)
}
meta["endpoint"] = endpoint
return Instance{
ID: uuid.New(),
Endpoint: endpoint,
Meta: meta,
}
}
// InstanceMeta represents metadata associated with an instance.
type InstanceMeta map[string]interface{}