Use cancelable contexts for cloud provider plugin refreshes (#4226)
This commit uses a cancelable context to spawn goroutines that refresh records from a cloud DNS provider. The Caddy shutdown routine uses the returned cancel function to terminate existing goroutines when a USR1 reload signal is received. Signed-off-by: Matt Kulka <mkulka@parchment.com>
This commit is contained in:
parent
054c9ae1fb
commit
3168a722ca
6 changed files with 12 additions and 11 deletions
|
@ -87,11 +87,11 @@ func (h *Azure) Run(ctx context.Context) error {
|
||||||
for {
|
for {
|
||||||
select {
|
select {
|
||||||
case <-ctx.Done():
|
case <-ctx.Done():
|
||||||
log.Infof("Breaking out of Azure update loop: %v", ctx.Err())
|
log.Debugf("Breaking out of Azure update loop for %v: %v", h.zoneNames, ctx.Err())
|
||||||
return
|
return
|
||||||
case <-time.After(1 * time.Minute):
|
case <-time.After(1 * time.Minute):
|
||||||
if err := h.updateZones(ctx); err != nil && ctx.Err() == nil {
|
if err := h.updateZones(ctx); err != nil && ctx.Err() == nil {
|
||||||
log.Errorf("Failed to update zones: %v", err)
|
log.Errorf("Failed to update zones %v: %v", h.zoneNames, err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -25,7 +25,7 @@ func setup(c *caddy.Controller) error {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return plugin.Error("azure", err)
|
return plugin.Error("azure", err)
|
||||||
}
|
}
|
||||||
ctx := context.Background()
|
ctx, cancel := context.WithCancel(context.Background())
|
||||||
|
|
||||||
publicDNSClient := publicAzureDNS.NewRecordSetsClient(env.Values[auth.SubscriptionID])
|
publicDNSClient := publicAzureDNS.NewRecordSetsClient(env.Values[auth.SubscriptionID])
|
||||||
if publicDNSClient.Authorizer, err = env.GetAuthorizer(); err != nil {
|
if publicDNSClient.Authorizer, err = env.GetAuthorizer(); err != nil {
|
||||||
|
@ -50,6 +50,7 @@ func setup(c *caddy.Controller) error {
|
||||||
h.Next = next
|
h.Next = next
|
||||||
return h
|
return h
|
||||||
})
|
})
|
||||||
|
c.OnShutdown(func() error { cancel(); return nil })
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -85,11 +85,11 @@ func (h *CloudDNS) Run(ctx context.Context) error {
|
||||||
for {
|
for {
|
||||||
select {
|
select {
|
||||||
case <-ctx.Done():
|
case <-ctx.Done():
|
||||||
log.Infof("Breaking out of CloudDNS update loop: %v", ctx.Err())
|
log.Debugf("Breaking out of CloudDNS update loop for %v: %v", h.zoneNames, ctx.Err())
|
||||||
return
|
return
|
||||||
case <-time.After(1 * time.Minute):
|
case <-time.After(1 * time.Minute):
|
||||||
if err := h.updateZones(ctx); err != nil && ctx.Err() == nil /* Don't log error if ctx expired. */ {
|
if err := h.updateZones(ctx); err != nil && ctx.Err() == nil /* Don't log error if ctx expired. */ {
|
||||||
log.Errorf("Failed to update zones: %v", err)
|
log.Errorf("Failed to update zones %v: %v", h.zoneNames, err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -78,7 +78,7 @@ func setup(c *caddy.Controller) error {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
ctx := context.Background()
|
ctx, cancel := context.WithCancel(context.Background())
|
||||||
client, err := f(ctx, opt)
|
client, err := f(ctx, opt)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
|
@ -98,7 +98,7 @@ func setup(c *caddy.Controller) error {
|
||||||
h.Next = next
|
h.Next = next
|
||||||
return h
|
return h
|
||||||
})
|
})
|
||||||
c.OnShutdown(func() error { ctx.Done(); return nil })
|
c.OnShutdown(func() error { cancel(); return nil })
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
|
|
|
@ -87,11 +87,11 @@ func (h *Route53) Run(ctx context.Context) error {
|
||||||
for {
|
for {
|
||||||
select {
|
select {
|
||||||
case <-ctx.Done():
|
case <-ctx.Done():
|
||||||
log.Infof("Breaking out of Route53 update loop: %v", ctx.Err())
|
log.Debugf("Breaking out of Route53 update loop for %v: %v", h.zoneNames, ctx.Err())
|
||||||
return
|
return
|
||||||
case <-time.After(h.refresh):
|
case <-time.After(h.refresh):
|
||||||
if err := h.updateZones(ctx); err != nil && ctx.Err() == nil /* Don't log error if ctx expired. */ {
|
if err := h.updateZones(ctx); err != nil && ctx.Err() == nil /* Don't log error if ctx expired. */ {
|
||||||
log.Errorf("Failed to update zones: %v", err)
|
log.Errorf("Failed to update zones %v: %v", h.zoneNames, err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -124,7 +124,7 @@ func setup(c *caddy.Controller) error {
|
||||||
Client: ec2metadata.New(session),
|
Client: ec2metadata.New(session),
|
||||||
})
|
})
|
||||||
client := f(credentials.NewChainCredentials(providers))
|
client := f(credentials.NewChainCredentials(providers))
|
||||||
ctx := context.Background()
|
ctx, cancel := context.WithCancel(context.Background())
|
||||||
h, err := New(ctx, client, keys, refresh)
|
h, err := New(ctx, client, keys, refresh)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return plugin.Error("route53", c.Errf("failed to create route53 plugin: %v", err))
|
return plugin.Error("route53", c.Errf("failed to create route53 plugin: %v", err))
|
||||||
|
@ -137,7 +137,7 @@ func setup(c *caddy.Controller) error {
|
||||||
h.Next = next
|
h.Next = next
|
||||||
return h
|
return h
|
||||||
})
|
})
|
||||||
c.OnShutdown(func() error { ctx.Done(); return nil })
|
c.OnShutdown(func() error { cancel(); return nil })
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue