Skip to content

Commit

Permalink
ADR 101: Add Close method to gRPC client (#1251)
Browse files Browse the repository at this point in the history
* grpc: Add close method to client

Signed-off-by: Thane Thomson <connect@thanethomson.com>

* grpc: Add close method to privileged client

Signed-off-by: Thane Thomson <connect@thanethomson.com>

* test/e2e: Close gRPC clients after testing

Signed-off-by: Thane Thomson <connect@thanethomson.com>

---------

Signed-off-by: Thane Thomson <connect@thanethomson.com>
  • Loading branch information
thanethomson authored and cason committed Oct 11, 2023
1 parent 54597a9 commit 34de583
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 12 deletions.
16 changes: 11 additions & 5 deletions rpc/grpc/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
"fmt"
"net"

"github.com/cosmos/gogoproto/grpc"
ggrpc "google.golang.org/grpc"
"google.golang.org/grpc/credentials/insecure"

Expand All @@ -24,6 +23,9 @@ type Client interface {
VersionServiceClient
BlockServiceClient
BlockResultsServiceClient

// Close the connection to the server. Any subsequent requests will fail.
Close() error
}

type clientBuilder struct {
Expand All @@ -50,13 +52,18 @@ func defaultDialerFunc(ctx context.Context, addr string) (net.Conn, error) {
}

type client struct {
conn grpc.ClientConn
conn *ggrpc.ClientConn

VersionServiceClient
BlockServiceClient
BlockResultsServiceClient
}

// Close implements Client.
func (c *client) Close() error {
return c.conn.Close()
}

// WithInsecure disables transport security for the underlying client
// connection.
//
Expand Down Expand Up @@ -126,11 +133,10 @@ func New(ctx context.Context, addr string, opts ...Option) (Client, error) {
if builder.blockResultsServiceEnabled {
blockResultServiceClient = newBlockResultsServiceClient(conn)
}
client := &client{
return &client{
conn: conn,
VersionServiceClient: versionServiceClient,
BlockServiceClient: blockServiceClient,
BlockResultsServiceClient: blockResultServiceClient,
}
return client, nil
}, nil
}
16 changes: 11 additions & 5 deletions rpc/grpc/client/privileged/privileged.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"fmt"
"net"

"github.com/cosmos/gogoproto/grpc"
ggrpc "google.golang.org/grpc"
"google.golang.org/grpc/credentials/insecure"

Expand All @@ -18,6 +17,9 @@ type Option func(*clientBuilder)
// a CometBFT node via the privileged gRPC server.
type Client interface {
PruningServiceClient

// Close the connection to the server. Any subsequent requests will fail.
Close() error
}

type clientBuilder struct {
Expand All @@ -40,11 +42,16 @@ func defaultDialerFunc(ctx context.Context, addr string) (net.Conn, error) {
}

type client struct {
conn grpc.ClientConn
conn *ggrpc.ClientConn

PruningServiceClient
}

// Close implements Client.
func (c *client) Close() error {
return c.conn.Close()
}

// WithInsecure disables transport security for the underlying client
// connection.
//
Expand Down Expand Up @@ -96,9 +103,8 @@ func New(ctx context.Context, addr string, opts ...Option) (Client, error) {
if builder.pruningServiceEnabled {
pruningServiceClient = newPruningServiceClient(conn)
}
client := &client{
return &client{
conn: conn,
PruningServiceClient: pruningServiceClient,
}
return client, nil
}, nil
}
12 changes: 10 additions & 2 deletions test/e2e/tests/grpc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ func TestGRPC_Version(t *testing.T) {
defer ctxCancel()
client, err := node.GRPCClient(ctx)
require.NoError(t, err)
defer client.Close()

res, err := client.GetVersion(ctx)
require.NoError(t, err)
Expand Down Expand Up @@ -71,6 +72,7 @@ func TestGRPC_Block_GetByHeight(t *testing.T) {
defer ctxCancel()
gRPCClient, err := node.GRPCClient(ctx)
require.NoError(t, err)
defer gRPCClient.Close()

for _, block := range blocks {
if block.Header.Height < first {
Expand Down Expand Up @@ -110,6 +112,7 @@ func TestGRPC_Block_GetLatest(t *testing.T) {

gclient, err := node.GRPCClient(ctx)
require.NoError(t, err)
defer gclient.Close()

resultCh, err := gclient.GetLatestHeight(ctx)
require.NoError(t, err)
Expand Down Expand Up @@ -140,6 +143,7 @@ func TestGRPC_Block_GetLatestHeight(t *testing.T) {

gclient, err := node.GRPCClient(ctx)
require.NoError(t, err)
defer gclient.Close()

resultCh, err := gclient.GetLatestHeight(ctx)
require.NoError(t, err)
Expand Down Expand Up @@ -175,6 +179,7 @@ func TestGRPC_GetBlockResults(t *testing.T) {
defer ctxCancel()
gRPCClient, err := node.GRPCClient(ctx)
require.NoError(t, err)
defer gRPCClient.Close()

// GetLatestBlockResults
latestBlockResults, err := gRPCClient.GetLatestBlockResults(ctx)
Expand Down Expand Up @@ -220,6 +225,7 @@ func TestGRPC_BlockRetainHeight(t *testing.T) {
defer ctxCancel()
grpcClient, err := node.GRPCPrivilegedClient(ctx)
require.NoError(t, err)
defer grpcClient.Close()

client, err := node.Client()
require.NoError(t, err)
Expand All @@ -246,19 +252,21 @@ func TestGRPC_BlockResultsRetainHeight(t *testing.T) {

ctx, ctxCancel := context.WithTimeout(context.Background(), time.Minute)
defer ctxCancel()

grpcClient, err := node.GRPCPrivilegedClient(ctx)
require.NoError(t, err)
defer grpcClient.Close()

client, err := node.Client()
require.NoError(t, err)

status, err := client.Status(ctx)
require.NoError(t, err)

err = grpcClient.SetBlockResultsRetainHeight(ctx, uint64(status.SyncInfo.LatestBlockHeight)-1)

require.NoError(t, err, "Unexpected error for SetBlockResultsRetainHeight")

height, err := grpcClient.GetBlockResultsRetainHeight(ctx)

require.NoError(t, err, "Unexpected error for GetBlockRetainHeight")
require.Equal(t, height, uint64(status.SyncInfo.LatestBlockHeight)-1)
})
Expand Down

0 comments on commit 34de583

Please sign in to comment.