forked from prebid/prebid-server
/
configupdate.go
61 lines (49 loc) · 1.49 KB
/
configupdate.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
package pubstack
import (
"fmt"
"net/http"
"net/url"
"time"
"github.com/prebid/prebid-server/util/task"
)
// ConfigUpdateTask publishes configurations until the stop channel is signaled.
type ConfigUpdateTask interface {
Start(stop <-chan struct{}) <-chan *Configuration
}
// ConfigUpdateHttpTask polls an HTTP endpoint on a specified interval and publishes configurations until
// the stop channel is signaled.
type ConfigUpdateHttpTask struct {
task *task.TickerTask
configChan chan *Configuration
}
func NewConfigUpdateHttpTask(httpClient *http.Client, scope, endpoint, refreshInterval string) (*ConfigUpdateHttpTask, error) {
refreshDuration, err := time.ParseDuration(refreshInterval)
if err != nil {
return nil, fmt.Errorf("fail to parse the module args, arg=analytics.pubstack.configuration_refresh_delay: %v", err)
}
endpointUrl, err := url.Parse(endpoint + "/bootstrap?scopeId=" + scope)
if err != nil {
return nil, err
}
configChan := make(chan *Configuration)
tr := task.NewTickerTaskFromFunc(refreshDuration, func() error {
config, err := fetchConfig(httpClient, endpointUrl)
if err != nil {
return fmt.Errorf("[pubstack] Fail to fetch remote configuration: %v", err)
}
configChan <- config
return nil
})
return &ConfigUpdateHttpTask{
task: tr,
configChan: configChan,
}, nil
}
func (t *ConfigUpdateHttpTask) Start(stop <-chan struct{}) <-chan *Configuration {
go t.task.Start()
go func() {
<-stop
t.task.Stop()
}()
return t.configChan
}