From ee4d26b7807856efbf61ebebb2687df21561a787 Mon Sep 17 00:00:00 2001 From: Anifalak <56835284+Daseinlux@users.noreply.github.com> Date: Tue, 27 Aug 2024 21:35:19 +0800 Subject: [PATCH] plugin/etcd: fix etcd connection leakage when reload (#6646) Signed-off-by: zhongyuanjun Co-authored-by: zhongyuanjun --- plugin/etcd/etcd.go | 8 ++++++++ plugin/etcd/setup.go | 2 ++ 2 files changed, 10 insertions(+) diff --git a/plugin/etcd/etcd.go b/plugin/etcd/etcd.go index 077e490f1..a78673027 100644 --- a/plugin/etcd/etcd.go +++ b/plugin/etcd/etcd.go @@ -183,3 +183,11 @@ func (e *Etcd) TTL(kv *mvccpb.KeyValue, serv *msg.Service) uint32 { func shouldInclude(serv *msg.Service, qType uint16) bool { return (qType == dns.TypeTXT && serv.Text != "") || serv.Host != "" } + +// OnShutdown shuts down etcd client when caddy instance restart +func (e *Etcd) OnShutdown() error { + if e.Client != nil { + e.Client.Close() + } + return nil +} diff --git a/plugin/etcd/setup.go b/plugin/etcd/setup.go index ab6c4b798..68a1b7f42 100644 --- a/plugin/etcd/setup.go +++ b/plugin/etcd/setup.go @@ -21,6 +21,8 @@ func setup(c *caddy.Controller) error { return plugin.Error("etcd", err) } + c.OnShutdown(e.OnShutdown) + dnsserver.GetConfig(c).AddPlugin(func(next plugin.Handler) plugin.Handler { e.Next = next return e