-
Notifications
You must be signed in to change notification settings - Fork 0
/
backend.go
40 lines (31 loc) · 1015 Bytes
/
backend.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
package stats
import (
"fmt"
"strings"
"time"
"github.com/robfig/cron"
)
type StatsBackend interface {
Start() error
Ready() bool
Store(domain string, questionType string, metadata Metadata)
Stop() error
}
func PrepareStatsBackend(uri string, workers int64, queryTimeout time.Duration, statsPrefix string, maxEntryAge time.Duration, maxEntryCleanCron string, logger Logger) (StatsBackend, error) {
maxEntryCleanTicker := make(chan struct{}, 5)
c := cron.New()
if err := c.AddFunc(maxEntryCleanCron, func() {
maxEntryCleanTicker <- struct{}{}
}); err != nil {
return nil, fmt.Errorf("error adding maxEntryCleanCron: %w", err)
}
if strings.HasPrefix(uri, "postgresql://") || strings.HasPrefix(uri, "postgres://") {
backend := newBackendPostgres(uri, workers, queryTimeout, statsPrefix, maxEntryAge, maxEntryCleanTicker, logger)
if err := backend.Start(); err != nil {
return nil, err
}
c.Start()
return backend, nil
}
return nil, fmt.Errorf("unsupported backend: %s", uri)
}