-
Notifications
You must be signed in to change notification settings - Fork 6
/
source_manager.go
111 lines (95 loc) · 2.31 KB
/
source_manager.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
package server
import (
"fmt"
"sort"
"time"
"github.com/doomsday-project/doomsday/client/doomsday"
"github.com/doomsday-project/doomsday/server/logger"
)
const (
MinAuthInterval = 5 * time.Second
ExpiredRetryInterval = 5 * time.Minute
)
type SourceManager struct {
sources []Source
queue *taskQueue
log *logger.Logger
global *Cache
}
func NewSourceManager(sources []Source, log *logger.Logger) *SourceManager {
if log == nil {
panic("No logger was given")
}
globalCache := NewCache()
//TODO: Make the number of workers configurable
queue := newTaskQueue(globalCache, 4, log)
return &SourceManager{
sources: sources,
queue: queue,
log: log,
global: globalCache,
}
}
func (s *SourceManager) BackgroundScheduler() error {
for i := range s.sources {
s.sources[i].Auth(s.log)
if s.sources[i].authStatus.LastErr != nil {
return fmt.Errorf("Error performing initial auth for backend `%s': %s",
s.sources[i].Core.Name,
s.sources[i].authStatus.LastErr)
}
}
now := time.Now()
for i := range s.sources {
s.queue.enqueue(managerTask{
kind: queueTaskKindRefresh,
source: &s.sources[i],
runTime: now,
reason: runReasonSchedule,
})
nextAuthTime, skipAuth := s.sources[i].CalcNextAuth()
if !skipAuth {
s.queue.enqueue(managerTask{
kind: queueTaskKindAuth,
source: &s.sources[i],
runTime: nextAuthTime,
reason: runReasonSchedule,
})
}
}
s.queue.start()
return nil
}
func (s *SourceManager) Data() doomsday.CacheItems {
items := []doomsday.CacheItem{}
for _, v := range s.global.Map() {
paths := []doomsday.CacheItemPath{}
for _, path := range v.Paths {
paths = append(paths, doomsday.CacheItemPath{
Backend: path.Source,
Location: path.Location,
})
}
items = append(items, doomsday.CacheItem{
Paths: paths,
CommonName: v.Subject.CommonName,
NotAfter: v.NotAfter.Unix(),
})
}
sort.Slice(items, func(i, j int) bool { return items[i].NotAfter < items[j].NotAfter })
return items
}
func (s *SourceManager) RefreshAll() {
now := time.Now()
for i := range s.sources {
s.queue.enqueue(managerTask{
source: &s.sources[i],
kind: queueTaskKindRefresh,
runTime: now,
reason: runReasonAdhoc,
})
}
}
func (s *SourceManager) SchedulerState() SchedulerState {
return s.queue.dumpState()
}