-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsource.go
82 lines (66 loc) · 1.58 KB
/
source.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
package conf
import (
"context"
"sync"
)
// SourceStorage
type SourcesStorage interface {
Append(src Source) error
ByID(sourceID string) (Source, error)
List() []Source
}
// Source.
type Source interface {
// Should return unique source identifier persistent for in all source lifetime
ID() string
// Load pull config for the list of service
Load(ctx context.Context, serviceNames []string) error
// Priority returns source priority
Priority() int
// ServiceConfig
ServiceConfig(serviceName string) (Config, error)
// Close closes connections
Close(context.Context)
}
// syncedSources represents sources map protected with mutex.
type SyncedSourcesStorage struct {
mtx sync.Mutex
sources map[string]Source
}
// NewSyncedSourcesStorage
func NewSyncedSourcesStorage() *SyncedSourcesStorage {
return &SyncedSourcesStorage{
sources: make(map[string]Source),
}
}
// Append
func (s *SyncedSourcesStorage) Append(src Source) error {
s.mtx.Lock()
defer s.mtx.Unlock()
srcID := src.ID()
if _, ok := s.sources[srcID]; ok {
return ErrSourceIsNotUnique{srcID}
}
s.sources[srcID] = src
return nil
}
// List returns sources as a slice.
func (s *SyncedSourcesStorage) List() []Source {
s.mtx.Lock()
defer s.mtx.Unlock()
lst := make([]Source, 0, len(s.sources))
for _, src := range s.sources {
lst = append(lst, src)
}
return lst
}
// ByID gets source by it's ID
func (s *SyncedSourcesStorage) ByID(sourceID string) (Source, error) {
s.mtx.Lock()
defer s.mtx.Unlock()
src, ok := s.sources[sourceID]
if !ok {
return nil, ErrSourceNotFound{sourceID}
}
return src, nil
}