-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathelasticip_provider.go
59 lines (47 loc) · 1.41 KB
/
elasticip_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
55
56
57
58
59
package main
import (
"context"
"sync"
"time"
"github.com/sirupsen/logrus"
)
type elasticIPProvider interface {
Test(context.Context) error
NewElasticIPRefresher(*logrus.Entry, netAddress) (elasticIPRefresher, error)
}
type elasticIPRefresher interface {
Logger() *logrus.Entry
Refresh(context.Context) error
}
func pinElasticIPs(ctx context.Context, provider elasticIPProvider, addresses []netAddress, cfg notifyConfig) error {
refreshers := []elasticIPRefresher{}
for _, address := range addresses {
logger := logrus.WithField("address", address)
refresher, err := provider.NewElasticIPRefresher(logger, address)
if err != nil {
return err
}
refreshers = append(refreshers, refresher)
}
wg := sync.WaitGroup{}
for _, i := range refreshers {
wg.Add(1)
go func(refresher elasticIPRefresher) {
defer wg.Done()
runRefresher(ctx, cfg.RefreshInterval, cfg.RefreshTimeout, cfg.BackOff, refresher)
}(i)
}
wg.Wait()
return nil
}
func runRefresher(ctx context.Context, interval time.Duration, timeout time.Duration, backOff backOffConfig, r elasticIPRefresher) {
logger := r.Logger()
logger.Infof("Refreshing %q every %s on average", r, interval)
err := loopWithRetries(ctx, logger, interval, backOff.New(),
func(ctx context.Context) error {
ctxRefresh, cancel := context.WithTimeout(ctx, timeout)
defer cancel()
return r.Refresh(ctxRefresh)
})
logger.Debugf("Shutdown (%s)", err)
}