-
Notifications
You must be signed in to change notification settings - Fork 20
/
registerService.go
74 lines (66 loc) · 2.25 KB
/
registerService.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
package service
import (
"Envoy-Pilot/cmd/server/constant"
"Envoy-Pilot/cmd/server/model"
"Envoy-Pilot/cmd/server/storage"
"context"
"log"
"github.com/rs/xid"
)
var singletonRegisterService *RegisterService
var pollTopics = make(map[string]*model.ConfigMeta)
// RegisterService a service class for cluster specific functionalities
type RegisterService struct {
xdsConfigDao storage.XdsConfigDao
subscriberDao *storage.SubscriberDao
dispatchService *DispatchService
watchService *WatchService
}
// GetRegisterService get a singleton instance
func GetRegisterService() *RegisterService {
if singletonRegisterService == nil {
singletonRegisterService = &RegisterService{
xdsConfigDao: storage.GetXdsConfigDao(),
subscriberDao: storage.GetSubscriberDao(),
dispatchService: GetDispatchService(),
watchService: GetWatchService(),
}
}
return singletonRegisterService
}
// RegisterEnvoy register & subscribe new envoy instance
func (c *RegisterService) RegisterEnvoy(ctx context.Context,
stream XDSStreamServer,
subscriber *model.EnvoySubscriber, dispatchChannel chan model.ConfigMeta) {
if subscriber.IsADS() {
c.subscriberDao.RegisterSubscriber(subscriber)
go c.watchService.listenForUpdatesADS(ctx, dispatchChannel)
go c.dispatchService.dispatchData(ctx, stream, dispatchChannel)
} else {
c.subscriberDao.RegisterSubscriber(subscriber)
go c.watchService.listenForUpdates(ctx, dispatchChannel)
go c.dispatchService.dispatchData(ctx, stream, dispatchChannel)
}
}
func (c *RegisterService) RegisterEnvoyADS(ctx context.Context,
stream XDSStreamServer,
subscriber *model.EnvoySubscriber, dispatchChannel chan model.ConfigMeta) {
subscriber.Guid = xid.New().String()
c.watchService.register(subscriber)
c.watchService.firstTimeCheck(subscriber, dispatchChannel)
}
// RemoveSubscriber Delete entry
func (c *RegisterService) DeleteSubscriber(subscriber *model.EnvoySubscriber) {
c.subscriberDao.DeleteSubscriber(subscriber)
log.Printf("Deleting subscriber %s\n", subscriber.BuildInstanceKey2())
if subscriber.IsADS() {
for _, topic := range constant.SUPPORTED_TYPES {
sub := subscriber.AdsList[topic]
if sub != nil {
delete(pollTopics, sub.BuildRootKey())
}
}
} else {
delete(pollTopics, subscriber.BuildRootKey())
}
}