Skip to content
This repository was archived by the owner on Mar 8, 2020. It is now read-only.

Commit 171fa39

Browse files
committed
Review: fail fast on CLI arg validation + simplify check
Signed-off-by: Alexander Bezzubov <[email protected]>
1 parent 664933f commit 171fa39

File tree

3 files changed

+23
-21
lines changed

3 files changed

+23
-21
lines changed

cmd/util.go

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -16,33 +16,29 @@ const (
1616
maxMsgSizeCLIDesc = "max. message size to send/receive to/from clients (in MB)"
1717

1818
// DefaulGRPCMaxSendRecvMsgSizeMB is maximum msg size for gRPC in MB.
19-
DefaulGRPCMaxSendRecvMsgSizeMB = 100
19+
defaulGRPCMaxSendRecvMsgSizeMB = 100
2020
maxMsgSizeCapMB = 2048
2121
)
2222

2323
// GRPCSizeOptions returns a slice of gRPC server options with the max
2424
// message size the server can send/receive set.
25-
// If a >2GB value is requested: the maximum size limit is capped
26-
// at 100 MB and an error is returned.
25+
// Error is returned if requested size is bigger than 2GB.
2726
// It is intended to be shared by gRPC in bblfshd Server and Drivers.
28-
func GRPCSizeOptions(maxMessageSizeMB int) ([]grpc.ServerOption, error) {
29-
var err error
30-
sizeMB := maxMessageSizeMB
27+
func GRPCSizeOptions(sizeMB int) ([]grpc.ServerOption, error) {
3128
if sizeMB >= maxMsgSizeCapMB || sizeMB <= 0 {
32-
err = fmt.Errorf("%s=%d is too big (limit is %dMB), using %d instead",
33-
maxMsgSizeCLIName, sizeMB, maxMsgSizeCapMB-1, DefaulGRPCMaxSendRecvMsgSizeMB)
34-
sizeMB = DefaulGRPCMaxSendRecvMsgSizeMB
29+
return nil, fmt.Errorf("%s=%d value should be in between 1 and %dMB",
30+
maxMsgSizeCLIName, sizeMB, maxMsgSizeCapMB-1)
3531
}
3632

3733
sizeBytes := sizeMB * 1024 * 1024
3834
return []grpc.ServerOption{
3935
grpc.MaxRecvMsgSize(sizeBytes),
4036
grpc.MaxSendMsgSize(sizeBytes),
41-
}, err
37+
}, nil
4238
}
4339

44-
// MaxSendRecvMsgSizeMB sets the CLI configuation flag for max
40+
// FlagMaxGRPCMsgSizeMB sets the CLI configuation flag for max
4541
// gRPC send/recive msg size.
46-
func MaxSendRecvMsgSizeMB(fs *flag.FlagSet) *int {
47-
return fs.Int(maxMsgSizeCLIName, DefaulGRPCMaxSendRecvMsgSizeMB, maxMsgSizeCLIDesc)
42+
func FlagMaxGRPCMsgSizeMB(fs *flag.FlagSet) *int {
43+
return fs.Int(maxMsgSizeCLIName, defaulGRPCMaxSendRecvMsgSizeMB, maxMsgSizeCLIDesc)
4844
}

cmd/util_test.go

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,19 +7,24 @@ import (
77
)
88

99
func TestGRPCOptions_InvalidInput(t *testing.T) {
10+
// too small
1011
opts, err := GRPCSizeOptions(-1)
12+
require.Error(t, err, "a small value should not be applied")
13+
require.Nil(t, opts)
1114

12-
require.Error(t, err, "a given value was not applied")
13-
require.NotNil(t, opts)
14-
require.Len(t, opts, 2)
15-
// does not work as expected for Func values like grpc.ServerOption
16-
//require.Contains(t, opts, grpc.MaxRecvMsgSize(DefaulGRPCMaxSendRecvMsgSizeMB))
17-
//require.Contains(t, opts, grpc.MaxSendMsgSize(DefaulGRPCMaxSendRecvMsgSizeMB))
15+
// too big
16+
opts, err = GRPCSizeOptions(maxMsgSizeCapMB + 1)
17+
require.Error(t, err, "a big value should not be applied")
18+
require.Nil(t, opts)
1819
}
1920

2021
func TestGRPCOptions_ValidInput(t *testing.T) {
2122
opts, err := GRPCSizeOptions(32)
2223

2324
require.Nil(t, err)
2425
require.NotNil(t, opts)
26+
require.Len(t, opts, 2)
27+
// does not work as expected for Func values like grpc.ServerOption
28+
//require.Contains(t, opts, grpc.MaxRecvMsgSize(DefaulGRPCMaxSendRecvMsgSizeMB))
29+
//require.Contains(t, opts, grpc.MaxSendMsgSize(DefaulGRPCMaxSendRecvMsgSizeMB))
2530
}

driver/server/server.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,8 @@ func (s *Server) initialize() error {
7575

7676
grpcOpts, err := cmdutil.GRPCSizeOptions(*maxMessageSize)
7777
if err != nil {
78-
s.Logger.Warningf(err.Error())
78+
s.Logger.Errorf(err.Error())
79+
os.Exit(1)
7980
}
8081
s.Options = grpcOpts
8182

@@ -103,7 +104,7 @@ func (s *Server) initializeFlags() {
103104
cmd := flag.NewFlagSet("server", flag.ExitOnError)
104105
network = cmd.String("network", defaultNetwork, "network type: tcp, tcp4, tcp6, unix or unixpacket.")
105106
address = cmd.String("address", defaultAddress, "address to listen.")
106-
maxMessageSize = cmdutil.MaxSendRecvMsgSizeMB(cmd)
107+
maxMessageSize = cmdutil.FlagMaxGRPCMsgSizeMB(cmd)
107108

108109
logs.level = cmd.String("log-level", defaultVerbose, "log level: panic, fatal, error, warning, info, debug.")
109110
logs.format = cmd.String("log-format", defaultFormat, "format of the logs: text or json.")

0 commit comments

Comments
 (0)