-
Notifications
You must be signed in to change notification settings - Fork 0
/
member.go
176 lines (144 loc) · 4.13 KB
/
member.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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
package registry
import (
"encoding/json"
"sync"
)
// Service represents a service object.
type Service struct {
// The ID of the service.
Id string `json:"id"`
// The group name of this service.
Group string `json:"group"`
// The service address provided to the client.
Addr string `json:"addr"`
}
// NewService creates a new service object.
func NewService(id string, group string, addr string) *Service {
return &Service{
Id: id,
Group: group,
Addr: addr,
}
}
// Member is used for auto-discovery. When a service is discovered, a Member object is created.
type Member struct {
sync.Mutex
// The ID of the service.
Id string `json:"id"`
// The address used to register the service to the registry server.
Bind string `json:"bind"`
// The address that the service will advertise to the registry server.
Advertise string `json:"advertise"`
// The addresses of the registry servers. If there are more than one, separate them with commas, such as "192.168.1.101:7370,192.168.1.102:7370".
Registries string `json:"-"`
// The number of replicated elements of a service that need to be virtualized.
Replicas string `json:"replicas"`
// Service information.
Service Service `json:"service"`
// Tags for extra information.
tags map[string]string
}
// NewSimpleMember creates a simple Member object. It does not contain the address of the service.
func NewSimpleMember(id string, bind string, advertise string) *Member {
return &Member{
Id: id,
Bind: bind,
Advertise: advertise,
Registries: "",
Replicas: DefaultReplicas,
Service: Service{
Id: id,
},
}
}
// NewMember creates a new Member object with the given attributes.
func NewMember(id string, bind string, advertise string, registries string, group string, addr string) *Member {
return &Member{
Id: id,
Bind: bind,
Advertise: advertise,
Registries: registries,
Replicas: DefaultReplicas,
Service: Service{
Id: id,
Group: group,
Addr: addr,
},
}
}
// IsSelf returns true if the given Member object has the same ID as this Member object.
func (m *Member) IsSelf(b *Member) bool {
return m.Id == b.Id
}
// SetTag sets the extra information associated with the given tag for this Member object.
func (m *Member) SetTag(key string, val string) {
m.Lock()
defer m.Unlock()
// If tags are not initialized, initialize them.
if m.tags == nil {
m.tags = make(map[string]string)
}
// Set the tag's value.
m.tags[key] = val
// Update service attributes based on specific tags.
if key == TagAddr {
m.Service.Addr = val
} else if key == TagGroup {
m.Service.Group = val
} else if key == TagReplicas {
m.Replicas = val
}
}
// GetTag retrieves the value associated with the given tag for this Member object.
func (m *Member) GetTag(key string) (string, bool) {
m.Lock()
defer m.Unlock()
// If tags are not initialized, return false.
if m.tags == nil {
return "", false
}
// Retrieve the tag's value.
val, ok := m.tags[key]
return val, ok
}
// SetTags set tags for this service
func (m *Member) SetTags(tags map[string]string) {
if m.tags == nil {
m.tags = make(map[string]string)
}
// Set each tag using SetTag method.
for k, v := range tags {
m.SetTag(k, v)
}
// Update service attributes based on specific tags.
m.Service.Group, _ = m.GetTag(TagGroup)
m.Service.Addr, _ = m.GetTag(TagAddr)
m.Replicas, _ = m.GetTag(TagReplicas)
}
// GetTags retrieves all tags and their values for this Member object.
func (m *Member) GetTags() map[string]string {
// Update specific tags before retrieving all tags.
m.SetTag(TagAddr, m.Service.Addr)
m.SetTag(TagGroup, m.Service.Group)
m.SetTag(TagReplicas, m.Replicas)
m.Lock()
defer m.Unlock()
// Clone the tags to avoid data races.
clone := make(map[string]string)
for k, v := range m.tags {
clone[k] = v
}
return clone
}
// Marshal returns the JSON encoding of this Member object.
func (m *Member) Marshal() ([]byte, error) {
m.Lock()
defer m.Unlock()
return json.Marshal(m)
}
// Unmarshal parses the given JSON-encoded data and stores
func (m *Member) Unmarshal(paylaod []byte) error {
m.Lock()
defer m.Unlock()
return json.Unmarshal(paylaod, m)
}