-
Notifications
You must be signed in to change notification settings - Fork 307
/
destination.go
70 lines (63 loc) · 2.12 KB
/
destination.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
package destination
import (
"context"
"sync"
"github.com/rudderlabs/rudder-go-kit/logger"
backendconfig "github.com/rudderlabs/rudder-server/backend-config"
"github.com/rudderlabs/rudder-server/regulation-worker/internal/model"
"github.com/rudderlabs/rudder-server/rruntime"
"github.com/rudderlabs/rudder-server/utils/pubsub"
)
var pkgLogger = logger.NewLogger().Child("client")
//go:generate mockgen -source=destination.go -destination=mock_destination.go -package=destination github.com/rudderlabs/rudder-server/regulation-worker/internal/Destination/destination
type destMiddleware interface {
Subscribe(ctx context.Context, topic backendconfig.Topic) pubsub.DataChannel
}
type DestinationConfig struct {
mu sync.RWMutex
destinations map[string]model.Destination
Dest destMiddleware
}
func (d *DestinationConfig) GetDestDetails(destID string) (model.Destination, error) {
pkgLogger.Debugf("getting destination details for destinationId: %v", destID)
d.mu.RLock()
defer d.mu.RUnlock()
destination, ok := d.destinations[destID]
if !ok {
return model.Destination{}, model.ErrInvalidDestination
}
return destination, nil
}
// Start starts listening for configuration updates and updates the destinations.
// The method blocks until the first update is received.
func (d *DestinationConfig) Start(ctx context.Context) {
initialized := make(chan struct{})
ch := d.Dest.Subscribe(ctx, backendconfig.TopicBackendConfig)
rruntime.Go(func() {
for data := range ch {
destinations := make(map[string]model.Destination)
configs := data.Data.(map[string]backendconfig.ConfigT)
for _, config := range configs {
for _, source := range config.Sources {
for _, dest := range source.Destinations {
destinations[dest.ID] = model.Destination{
DestinationID: dest.ID,
Config: dest.Config,
Name: dest.DestinationDefinition.Name,
DestDefConfig: dest.DestinationDefinition.Config,
}
}
}
}
d.mu.Lock()
d.destinations = destinations
d.mu.Unlock()
select {
case <-initialized:
default:
close(initialized)
}
}
})
<-initialized
}