Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ADR 101: Add Close method to gRPC client #1251

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure why this grpc.ClientConn from the gogoproto fork was pulled in - could've been done automatically by my IDE.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you know what the diff is, this might be the cause of Lauren's problems maybe? :) anyways, PR looks good to me


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 @@ -21,6 +21,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 @@ -55,6 +56,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 @@ -94,6 +96,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 @@ -124,6 +127,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 @@ -159,6 +163,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 @@ -204,6 +209,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 @@ -230,19 +236,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
Loading