This repository has been archived by the owner on Dec 14, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 276
/
discovery.go
167 lines (143 loc) · 4.76 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
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
// 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 implements TTN Service Discovery.
package discovery
import (
"sync"
pb "github.com/TheThingsNetwork/api/discovery"
"github.com/TheThingsNetwork/ttn/core/component"
"github.com/TheThingsNetwork/ttn/core/discovery/announcement"
"github.com/TheThingsNetwork/ttn/core/storage"
"github.com/TheThingsNetwork/ttn/core/types"
"github.com/TheThingsNetwork/ttn/utils/errors"
"gopkg.in/redis.v5"
)
// Discovery specifies the interface for the TTN Service Discovery component
type Discovery interface {
component.Interface
WithCache(options announcement.CacheOptions)
MonitorServiceConnectivity()
WithMasterAuthServers(serverID ...string)
Announce(announcement *pb.Announcement) error
GetAll(serviceName string, limit, offset uint64) ([]*pb.Announcement, error)
Get(serviceName string, id string) (*pb.Announcement, error)
AddMetadata(serviceName string, id string, metadata *pb.Metadata) error
DeleteMetadata(serviceName string, id string, metadata *pb.Metadata) error
}
// discovery is a reference implementation for a TTN Service Discovery component.
type discovery struct {
*component.Component
services announcement.Store
masterAuthServers map[string]struct{}
statusMu sync.RWMutex
serviceStatus map[serviceID]*serviceStatus
}
func (d *discovery) WithCache(options announcement.CacheOptions) {
d.services = announcement.NewCachedAnnouncementStore(d.services, options)
}
func (d *discovery) MonitorServiceConnectivity() {
d.startConnectivityMonitor()
}
func (d *discovery) WithMasterAuthServers(serverID ...string) {
for _, serverID := range serverID {
d.masterAuthServers[serverID] = struct{}{}
}
}
func (d *discovery) IsMasterAuthServer(serverID string) bool {
_, ok := d.masterAuthServers[serverID]
return ok
}
func (d *discovery) Init(c *component.Component) error {
d.Component = c
err := d.Component.UpdateTokenKey()
if err != nil {
return err
}
d.Component.SetStatus(component.StatusHealthy)
return nil
}
func (d *discovery) Shutdown() {}
func (d *discovery) Announce(in *pb.Announcement) error {
service, err := d.services.Get(in.ServiceName, in.ID)
if err != nil && errors.GetErrType(err) != errors.NotFound {
return err
}
if service == nil {
service = new(announcement.Announcement)
}
service.StartUpdate()
service.ID = in.ID
service.ServiceName = in.ServiceName
service.ServiceVersion = in.ServiceVersion
service.Description = in.Description
service.URL = in.Url
service.Public = in.Public
service.NetAddress = in.NetAddress
service.PublicKey = in.PublicKey
service.Certificate = in.Certificate
service.APIAddress = in.ApiAddress
service.MQTTAddress = in.MqttAddress
service.AMQPAddress = in.AmqpAddress
return d.services.Set(service)
}
func (d *discovery) Get(serviceName string, id string) (*pb.Announcement, error) {
service, err := d.services.Get(serviceName, id)
if err != nil {
return nil, err
}
return service.ToProto(), nil
}
func (d *discovery) GetAll(serviceName string, limit, offset uint64) ([]*pb.Announcement, error) {
services, err := d.services.ListService(serviceName, &storage.ListOptions{
Limit: limit,
Offset: offset,
})
if err != nil {
return nil, err
}
services = d.filterAvailable(services)
serviceCopies := make([]*pb.Announcement, 0, len(services))
for _, service := range services {
if service == nil {
continue
}
serviceCopies = append(serviceCopies, service.ToProto())
}
return serviceCopies, nil
}
func (d *discovery) AddMetadata(serviceName string, id string, in *pb.Metadata) error {
meta := announcement.MetadataFromProto(in)
return d.services.AddMetadata(serviceName, id, meta)
}
func (d *discovery) DeleteMetadata(serviceName string, id string, in *pb.Metadata) error {
meta := announcement.MetadataFromProto(in)
return d.services.RemoveMetadata(serviceName, id, meta)
}
func (d *discovery) GetByAppID(appID string) (*pb.Announcement, error) {
service, err := d.services.GetForAppID(appID)
if err != nil {
return nil, err
}
return service.ToProto(), nil
}
func (d *discovery) GetByGatewayID(gatewayID string) (*pb.Announcement, error) {
service, err := d.services.GetForGatewayID(gatewayID)
if err != nil {
return nil, err
}
return service.ToProto(), nil
}
func (d *discovery) GetByAppEUI(appEUI types.AppEUI) (*pb.Announcement, error) {
service, err := d.services.GetForAppEUI(appEUI)
if err != nil {
return nil, err
}
return service.ToProto(), nil
}
// NewRedisDiscovery creates a new Redis-based discovery service
func NewRedisDiscovery(client *redis.Client) Discovery {
return &discovery{
services: announcement.NewRedisAnnouncementStore(client, "discovery"),
masterAuthServers: make(map[string]struct{}),
}
}