-
Notifications
You must be signed in to change notification settings - Fork 0
/
manager.go
99 lines (83 loc) · 2.11 KB
/
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
package scraper
import (
"context"
"log/slog"
"math/rand"
"sync"
"time"
)
type ScrapeFunc func(ctx context.Context) error
type targetInfo struct {
scrapeFunc ScrapeFunc
cancel context.CancelFunc
}
type Manager struct {
targets map[string]*targetInfo
mu sync.Mutex
ctx context.Context
disableScrapingDelay bool
}
func NewManager(ctx context.Context, disableScrapingDelay bool) *Manager {
return &Manager{
targets: make(map[string]*targetInfo),
ctx: ctx,
disableScrapingDelay: disableScrapingDelay,
}
}
func (m *Manager) AddTarget(id string, scrapeFunc ScrapeFunc, interval time.Duration) {
m.mu.Lock()
defer m.mu.Unlock()
// Check if target already exists
if _, exists := m.targets[id]; exists {
return
}
scrapeCtx, cancel := context.WithCancel(m.ctx)
m.targets[id] = &targetInfo{
scrapeFunc: scrapeFunc,
cancel: cancel,
}
go func() {
if !m.disableScrapingDelay {
randomDelay(scrapeCtx, interval)
}
ticker := time.NewTicker(interval)
defer ticker.Stop()
// start first scrape immediately
if err := scrapeFunc(scrapeCtx); err != nil {
slog.Error("scraping target", "id", id, "error", err)
}
for {
select {
case <-ticker.C:
// Perform the scrape
if err := scrapeFunc(scrapeCtx); err != nil {
slog.Error("scraping target", "id", id, "error", err)
}
case <-scrapeCtx.Done():
slog.Info("target scraper cancelled", "id", id)
return
}
}
}()
slog.Info("new scraping target added", "id", id)
}
func randomDelay(ctx context.Context, maxInterval time.Duration) {
// nolint:gosec
initialDelay := time.Duration(rand.Int63n(int64(maxInterval)))
select {
case <-time.After(initialDelay):
// Continue with the scraping after the delay
case <-ctx.Done():
// Context was cancelled during the initial delay
return
}
}
func (m *Manager) RemoveTarget(id string) {
m.mu.Lock()
defer m.mu.Unlock()
if target, exists := m.targets[id]; exists {
target.cancel() // Signal the goroutine to stop
delete(m.targets, id)
}
slog.Info("scraping target removed", "id", id)
}