forked from TheThingsArchive/ttn
-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.go
248 lines (224 loc) · 6.88 KB
/
client.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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
// Copyright © 2017 The Things Network
// Use of this source code is governed by the MIT license that can be found in the LICENSE file.
package discovery
import (
"fmt"
"sync"
"time"
"github.com/TheThingsNetwork/ttn/api"
"github.com/TheThingsNetwork/ttn/core/types"
"github.com/bluele/gcache"
"golang.org/x/net/context" // See https://github.com/grpc/grpc-go/issues/711
"google.golang.org/grpc"
"google.golang.org/grpc/metadata"
)
// CacheSize indicates the number of components that are cached
var CacheSize = 1000
// CacheExpiration indicates the time a cached item is valid
var CacheExpiration = 5 * time.Minute
// Client is used as the main client to the Discovery server
type Client interface {
Announce(token string) error
GetAll(serviceName string) ([]*Announcement, error)
Get(serviceName, id string) (*Announcement, error)
AddDevAddrPrefix(prefix types.DevAddrPrefix) error
AddAppID(appID string, token string) error
RemoveDevAddrPrefix(prefix types.DevAddrPrefix) error
RemoveAppID(appID string, token string) error
GetAllBrokersForDevAddr(devAddr types.DevAddr) ([]*Announcement, error)
GetAllHandlersForAppID(appID string) ([]*Announcement, error)
Close() error
}
// NewClient returns a new Client
func NewClient(server string, announcement *Announcement, tokenFunc func() string) (Client, error) {
conn, err := api.Dial(server)
if err != nil {
return nil, err
}
client := &DefaultClient{
lists: make(map[string][]*Announcement),
listsUpdated: make(map[string]time.Time),
self: announcement,
tokenFunc: tokenFunc,
conn: conn,
client: NewDiscoveryClient(conn),
}
client.cache = gcache.
New(CacheSize).
Expiration(CacheExpiration).
LRU().
LoaderFunc(func(k interface{}) (interface{}, error) {
key, ok := k.(cacheKey)
if !ok {
return nil, fmt.Errorf("wrong type for cacheKey: %T", k)
}
return client.get(key.serviceName, key.id)
}).
Build()
return client, nil
}
// DefaultClient is a wrapper around DiscoveryClient
type DefaultClient struct {
sync.Mutex
cache gcache.Cache
listsUpdated map[string]time.Time
lists map[string][]*Announcement
self *Announcement
tokenFunc func() string
conn *grpc.ClientConn
client DiscoveryClient
}
type cacheKey struct {
serviceName string
id string
}
func (c *DefaultClient) getContext(token string) context.Context {
if token == "" {
token = c.tokenFunc()
}
md := metadata.Pairs(
"service-name", c.self.ServiceName,
"id", c.self.Id,
"token", token,
"net-address", c.self.NetAddress,
)
ctx := metadata.NewContext(context.Background(), md)
return ctx
}
func (c *DefaultClient) get(serviceName, id string) (*Announcement, error) {
res, err := c.client.Get(c.getContext(""), &GetRequest{
ServiceName: serviceName,
Id: id,
})
if err != nil {
return nil, err
}
return res, nil
}
func (c *DefaultClient) getAll(serviceName string) ([]*Announcement, error) {
res, err := c.client.GetAll(c.getContext(""), &GetServiceRequest{ServiceName: serviceName})
if err != nil {
return nil, err
}
c.lists[serviceName] = res.Services
c.listsUpdated[serviceName] = time.Now()
for _, announcement := range res.Services {
c.cache.Set(&cacheKey{serviceName: announcement.ServiceName, id: announcement.Id}, announcement)
}
return res.Services, nil
}
// Announce announces the configured announcement to the discovery server
func (c *DefaultClient) Announce(token string) error {
_, err := c.client.Announce(c.getContext(token), c.self)
return err
}
// GetAll returns all services of the given service type
func (c *DefaultClient) GetAll(serviceName string) ([]*Announcement, error) {
c.Lock()
defer c.Unlock()
// If list initialized, return cached version
if list, ok := c.lists[serviceName]; ok && len(list) > 0 {
// And update if expired
if c.listsUpdated[serviceName].Add(CacheExpiration).Before(time.Now()) {
go func() {
c.Lock()
defer c.Unlock()
c.getAll(serviceName)
}()
}
return list, nil
}
// If list not initialized, do request
return c.getAll(serviceName)
}
// Get returns the (cached) service annoucement for the given service type and id
func (c *DefaultClient) Get(serviceName, id string) (*Announcement, error) {
res, err := c.cache.Get(cacheKey{serviceName, id})
if err != nil {
return nil, err
}
return res.(*Announcement), nil
}
// AddDevAddrPrefix adds a DevAddrPrefix to the current component
func (c *DefaultClient) AddDevAddrPrefix(prefix types.DevAddrPrefix) error {
_, err := c.client.AddMetadata(c.getContext(""), &MetadataRequest{
ServiceName: c.self.ServiceName,
Id: c.self.Id,
Metadata: &Metadata{Metadata: &Metadata_DevAddrPrefix{
DevAddrPrefix: prefix.Bytes(),
}},
})
return err
}
// AddAppID adds an AppID to the current component
func (c *DefaultClient) AddAppID(appID string, token string) error {
_, err := c.client.AddMetadata(c.getContext(token), &MetadataRequest{
ServiceName: c.self.ServiceName,
Id: c.self.Id,
Metadata: &Metadata{Metadata: &Metadata_AppId{
AppId: appID,
}},
})
return err
}
// RemoveDevAddrPrefix removes a DevAddrPrefix from the current component
func (c *DefaultClient) RemoveDevAddrPrefix(prefix types.DevAddrPrefix) error {
_, err := c.client.DeleteMetadata(c.getContext(""), &MetadataRequest{
ServiceName: c.self.ServiceName,
Id: c.self.Id,
Metadata: &Metadata{Metadata: &Metadata_DevAddrPrefix{
DevAddrPrefix: prefix.Bytes(),
}},
})
return err
}
// RemoveAppID removes an AppID from the current component
func (c *DefaultClient) RemoveAppID(appID string, token string) error {
_, err := c.client.DeleteMetadata(c.getContext(token), &MetadataRequest{
ServiceName: c.self.ServiceName,
Id: c.self.Id,
Metadata: &Metadata{Metadata: &Metadata_AppId{
AppId: appID,
}},
})
return err
}
// GetAllBrokersForDevAddr returns all brokers that can handle the given DevAddr
func (c *DefaultClient) GetAllBrokersForDevAddr(devAddr types.DevAddr) (announcements []*Announcement, err error) {
brokers, err := c.GetAll("broker")
if err != nil {
return nil, err
}
next:
for _, broker := range brokers {
for _, prefix := range broker.DevAddrPrefixes() {
if devAddr.HasPrefix(prefix) {
announcements = append(announcements, broker)
continue next
}
}
}
return
}
// GetAllHandlersForAppID returns all handlers that can handle the given AppID
func (c *DefaultClient) GetAllHandlersForAppID(appID string) (announcements []*Announcement, err error) {
handlers, err := c.GetAll("handler")
if err != nil {
return nil, err
}
next:
for _, handler := range handlers {
for _, handlerAppID := range handler.AppIDs() {
if handlerAppID == appID {
announcements = append(announcements, handler)
continue next
}
}
}
return
}
// Close purges the cache and closes the connection with the Discovery server
func (c *DefaultClient) Close() error {
c.cache.Purge()
return c.conn.Close()
}