From d257acee24c804e5b7afdfc2b6109729a935e4ea Mon Sep 17 00:00:00 2001 From: Daniel Nephin Date: Thu, 22 Apr 2021 13:40:12 -0400 Subject: [PATCH] rpcclient: close the grpc.ClientConn on shutdown --- agent/agent.go | 5 +++-- agent/rpcclient/health/health.go | 11 ++++++++++- agent/rpcclient/health/view.go | 4 ++-- 3 files changed, 15 insertions(+), 5 deletions(-) diff --git a/agent/agent.go b/agent/agent.go index e429d9f86..21bddae4d 100644 --- a/agent/agent.go +++ b/agent/agent.go @@ -50,7 +50,6 @@ import ( "github.com/hashicorp/consul/lib/file" "github.com/hashicorp/consul/lib/mutex" "github.com/hashicorp/consul/logging" - "github.com/hashicorp/consul/proto/pbsubscribe" "github.com/hashicorp/consul/tlsutil" "github.com/hashicorp/consul/types" ) @@ -386,7 +385,7 @@ func New(bd BaseDeps) (*Agent, error) { CacheName: cachetype.HealthServicesName, ViewStore: bd.ViewStore, MaterializerDeps: health.MaterializerDeps{ - Client: pbsubscribe.NewStateChangeSubscriptionClient(conn), + Conn: conn, Logger: bd.Logger.Named("rpcclient.health"), }, } @@ -1393,6 +1392,8 @@ func (a *Agent) ShutdownAgent() error { a.cache.Close() } + a.rpcClientHealth.Close() + var err error if a.delegate != nil { err = a.delegate.Shutdown() diff --git a/agent/rpcclient/health/health.go b/agent/rpcclient/health/health.go index a5712b86f..389ad65ba 100644 --- a/agent/rpcclient/health/health.go +++ b/agent/rpcclient/health/health.go @@ -6,6 +6,7 @@ import ( "github.com/hashicorp/consul/agent/cache" "github.com/hashicorp/consul/agent/structs" "github.com/hashicorp/consul/agent/submatview" + "github.com/hashicorp/consul/proto/pbsubscribe" ) // TODO: godoc @@ -108,6 +109,14 @@ func (c *Client) newServiceRequest(req structs.ServiceSpecificRequest) serviceRe } } +// Close any underlying connections used by the client. +func (c *Client) Close() error { + if c == nil { + return nil + } + return c.MaterializerDeps.Conn.Close() +} + type serviceRequest struct { structs.ServiceSpecificRequest deps MaterializerDeps @@ -128,7 +137,7 @@ func (r serviceRequest) NewMaterializer() (*submatview.Materializer, error) { } return submatview.NewMaterializer(submatview.Deps{ View: view, - Client: r.deps.Client, + Client: pbsubscribe.NewStateChangeSubscriptionClient(r.deps.Conn), Logger: r.deps.Logger, Request: newMaterializerRequest(r.ServiceSpecificRequest), }), nil diff --git a/agent/rpcclient/health/view.go b/agent/rpcclient/health/view.go index ef89a156e..a648686c4 100644 --- a/agent/rpcclient/health/view.go +++ b/agent/rpcclient/health/view.go @@ -8,15 +8,15 @@ import ( "github.com/hashicorp/go-bexpr" "github.com/hashicorp/go-hclog" + "google.golang.org/grpc" "github.com/hashicorp/consul/agent/structs" - "github.com/hashicorp/consul/agent/submatview" "github.com/hashicorp/consul/proto/pbservice" "github.com/hashicorp/consul/proto/pbsubscribe" ) type MaterializerDeps struct { - Client submatview.StreamClient + Conn *grpc.ClientConn Logger hclog.Logger }