From 8339f51702f83d09459dd9747b4485d83ab2bb2c Mon Sep 17 00:00:00 2001 From: Filinto Duran Date: Mon, 31 Jul 2023 06:53:24 -0500 Subject: [PATCH] add atomic closed Signed-off-by: Filinto Duran --- state/hashicorp/consul/consul.go | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/state/hashicorp/consul/consul.go b/state/hashicorp/consul/consul.go index 18e033c918..3a3cf6e22f 100644 --- a/state/hashicorp/consul/consul.go +++ b/state/hashicorp/consul/consul.go @@ -19,6 +19,7 @@ import ( "fmt" "net/http" "reflect" + "sync/atomic" "github.com/hashicorp/consul/api" "github.com/hashicorp/go-cleanhttp" @@ -37,6 +38,7 @@ type Consul struct { keyPrefixPath string logger logger.Logger transport *http.Transport + closed atomic.Bool } type consulConfig struct { @@ -171,7 +173,7 @@ func (c *Consul) GetComponentMetadata() (metadataInfo metadata.MetadataMap) { } func (c *Consul) Close() error { - if c != nil && c.transport != nil { + if c.closed.CompareAndSwap(false, true) && c.transport != nil { c.transport.CloseIdleConnections() } return nil