Skip to content

Commit

Permalink
Refactor max message sizes in comm client config
Browse files Browse the repository at this point in the history
Signed-off-by: andrew-coleman <andrew_coleman@uk.ibm.com>
  • Loading branch information
andrew-coleman authored and denyeart committed Aug 23, 2021
1 parent fbf7b93 commit da9e1bd
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 34 deletions.
33 changes: 12 additions & 21 deletions internal/pkg/comm/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,6 @@ type GRPCClient struct {
dialOpts []grpc.DialOption
// Duration for which to block while established a new connection
timeout time.Duration
// Maximum message size the client can receive
maxRecvMsgSize int
// Maximum message size the client can send
maxSendMsgSize int
}

// NewGRPCClient creates a new implementation of GRPCClient given an address
Expand Down Expand Up @@ -57,8 +53,18 @@ func NewGRPCClient(config ClientConfig) (*GRPCClient, error) {
}
client.timeout = config.Timeout
// set send/recv message size to package defaults
client.maxRecvMsgSize = MaxRecvMsgSize
client.maxSendMsgSize = MaxSendMsgSize
maxRecvMsgSize := MaxRecvMsgSize
if config.MaxRecvMsgSize != 0 {
maxRecvMsgSize = config.MaxRecvMsgSize
}
maxSendMsgSize := MaxSendMsgSize
if config.MaxSendMsgSize != 0 {
maxSendMsgSize = config.MaxSendMsgSize
}
client.dialOpts = append(client.dialOpts, grpc.WithDefaultCallOptions(
grpc.MaxCallRecvMsgSize(maxRecvMsgSize),
grpc.MaxCallSendMsgSize(maxSendMsgSize),
))

return client, nil
}
Expand Down Expand Up @@ -131,16 +137,6 @@ func (client *GRPCClient) MutualTLSRequired() bool {
len(client.tlsConfig.Certificates) > 0
}

// SetMaxRecvMsgSize sets the maximum message size the client can receive
func (client *GRPCClient) SetMaxRecvMsgSize(size int) {
client.maxRecvMsgSize = size
}

// SetMaxSendMsgSize sets the maximum message size the client can send
func (client *GRPCClient) SetMaxSendMsgSize(size int) {
client.maxSendMsgSize = size
}

// SetServerRootCAs sets the list of authorities used to verify server
// certificates based on a list of PEM-encoded X509 certificate authorities
func (client *GRPCClient) SetServerRootCAs(serverRoots [][]byte) error {
Expand Down Expand Up @@ -195,11 +191,6 @@ func (client *GRPCClient) NewConnection(address string, tlsOptions ...TLSOption)
dialOpts = append(dialOpts, grpc.WithInsecure())
}

dialOpts = append(dialOpts, grpc.WithDefaultCallOptions(
grpc.MaxCallRecvMsgSize(client.maxRecvMsgSize),
grpc.MaxCallSendMsgSize(client.maxSendMsgSize),
))

ctx, cancel := context.WithTimeout(context.Background(), client.timeout)
defer cancel()
conn, err := grpc.DialContext(ctx, address, dialOpts...)
Expand Down
20 changes: 7 additions & 13 deletions internal/pkg/comm/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -478,25 +478,19 @@ func TestSetMessageSize(t *testing.T) {
},
}

// set up test client
client, err := comm.NewGRPCClient(comm.ClientConfig{
Timeout: testTimeout,
})
if err != nil {
t.Fatalf("error creating test client: %v", err)
}
// run tests
for _, test := range tests {
test := test
address := lis.Addr().String()
t.Run(test.name, func(t *testing.T) {
t.Log(test.name)
if test.maxRecvSize > 0 {
client.SetMaxRecvMsgSize(test.maxRecvSize)
}
if test.maxSendSize > 0 {
client.SetMaxSendMsgSize(test.maxSendSize)
}
// set up test client
client, err := comm.NewGRPCClient(comm.ClientConfig{
Timeout: testTimeout,
MaxRecvMsgSize: test.maxRecvSize,
MaxSendMsgSize: test.maxSendSize,
})
require.NoError(t, err, "error creating test client")
conn, err := client.NewConnection(address)
assert.NoError(t, err)
defer conn.Close()
Expand Down
4 changes: 4 additions & 0 deletions internal/pkg/comm/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,10 @@ type ClientConfig struct {
Timeout time.Duration
// AsyncConnect makes connection creation non blocking
AsyncConnect bool
// Maximum message size the client can receive
MaxRecvMsgSize int
// Maximum message size the client can send
MaxSendMsgSize int
}

// Clone clones this ClientConfig
Expand Down

0 comments on commit da9e1bd

Please sign in to comment.