-
Notifications
You must be signed in to change notification settings - Fork 16
/
polling_provider.go
54 lines (43 loc) · 1.29 KB
/
polling_provider.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
package traefikkop
import (
"context"
"time"
"github.com/sirupsen/logrus"
"github.com/traefik/traefik/v2/pkg/config/dynamic"
"github.com/traefik/traefik/v2/pkg/log"
"github.com/traefik/traefik/v2/pkg/provider"
"github.com/traefik/traefik/v2/pkg/safe"
)
// PollingProvider simply wraps the target upstream provider with a poller.
type PollingProvider struct {
refreshInterval time.Duration
upstreamProvider provider.Provider
}
func NewPollingProvider(refreshInterval time.Duration, upstream provider.Provider) *PollingProvider {
return &PollingProvider{refreshInterval, upstream}
}
func (p PollingProvider) Init() error {
return p.upstreamProvider.Init()
}
func (p PollingProvider) Provide(configurationChan chan<- dynamic.Message, pool *safe.Pool) error {
if p.refreshInterval == 0 {
logrus.Infoln("Disabling polling provider (interval=0)")
return nil
}
logrus.Infof("starting polling provider with %s interval", p.refreshInterval.String())
ticker := time.NewTicker(p.refreshInterval)
pool.GoCtx(func(ctx context.Context) {
ctx = log.With(ctx, log.Str(log.ProviderName, "docker"))
for {
select {
case <-ticker.C:
logrus.Debugln("tick")
p.upstreamProvider.Provide(configurationChan, pool)
case <-ctx.Done():
ticker.Stop()
return
}
}
})
return nil
}