Skip to content

Commit

Permalink
Add rpc-max-requests-bytes flag to set client request limit
Browse files Browse the repository at this point in the history
  • Loading branch information
hackerwins committed Nov 21, 2021
1 parent 8649a3f commit e544cdb
Show file tree
Hide file tree
Showing 7 changed files with 36 additions and 9 deletions.
6 changes: 6 additions & 0 deletions internal/cli/agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,12 @@ func init() {
"",
"RPC key file's path",
)
cmd.Flags().Uint64Var(
&conf.RPC.MaxRequestBytes,
"rpc-max-requests-bytes",
yorkie.DefaultRPCMaxRequestsBytes,
"Maximum client request size in bytes the server will accept.",
)
cmd.Flags().IntVar(
&conf.Profiling.Port,
"profiling-port",
Expand Down
4 changes: 3 additions & 1 deletion test/helper/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ var testStartedAt int64
// Below are the values of the Yorkie config used in the test.
const (
RPCPort = 21101
RPCMaxRequestBytes = 4 * 1024 * 1024
ProfilingPort = 21102
MongoConnectionURI = "mongodb://localhost:27017"
MongoConnectionTimeout = "5s"
Expand Down Expand Up @@ -87,7 +88,8 @@ func TestConfig(authWebhook string) *yorkie.Config {
portOffset += 100
return &yorkie.Config{
RPC: &rpc.Config{
Port: RPCPort + portOffset,
Port: RPCPort + portOffset,
MaxRequestBytes: RPCMaxRequestBytes,
},
Profiling: &profiling.Config{
Port: ProfilingPort + portOffset,
Expand Down
9 changes: 7 additions & 2 deletions yorkie/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,9 @@ import (

// Below are the values of the default values of Yorkie config.
const (
DefaultRPCPort = 11101
DefaultProfilingPort = 11102
DefaultRPCPort = 11101
DefaultProfilingPort = 11102
DefaultRPCMaxRequestsBytes = 4 * 1024 * 1024 // 4MiB

DefaultMongoConnectionURI = "mongodb://localhost:27017"
DefaultMongoConnectionTimeout = 5 * time.Second
Expand Down Expand Up @@ -120,6 +121,10 @@ func (c *Config) ensureDefaultValue() {
c.RPC.Port = DefaultRPCPort
}

if c.RPC.MaxRequestBytes == 0 {
c.RPC.MaxRequestBytes = DefaultRPCMaxRequestsBytes
}

if c.Profiling.Port == 0 {
c.Profiling.Port = DefaultProfilingPort
}
Expand Down
9 changes: 6 additions & 3 deletions yorkie/config.sample.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ RPC:
# Port to listen on for RPC connections (default: 11101).
Port: 11101

# MaxRequestBytes is the maximum client request size in bytes the server will accept (default: 4194304, 4MiB).
MaxRequestBytes: 4194304

# CertFile is the file containing the TLS certificate.
CertFile: ""

Expand All @@ -21,10 +24,10 @@ Profiling:
Backend:
# SnapshotThreshold is the threshold that determines if changes should be
# sent with snapshot when the number of changes is greater than this value.
SnapshotThreshold: 500
SnapshotThreshold: 1000

# SnapshotInterval is the interval of changes to create a snapshot.
SnapshotInterval: 100
# SnapshotInterval is the number of changes to create a snapshot.
SnapshotInterval: 5000

# AuthWebhookURL is the URL to send authorization requests to.
AuthWebhookURL: ""
Expand Down
12 changes: 10 additions & 2 deletions yorkie/rpc/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,17 @@ var (

// Config is the configuration for creating a Server instance.
type Config struct {
Port int
// Port is the port number for the RPC server.
Port int

// CertFile is the path to the certificate file.
CertFile string
KeyFile string

// KeyFile is the path to the key file.
KeyFile string

// MaxRequestBytes is the maximum client request size in bytes the server will accept.
MaxRequestBytes uint64
}

// Validate validates the port number and the files for certification.
Expand Down
2 changes: 2 additions & 0 deletions yorkie/rpc/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@ func NewServer(conf *Config, be *backend.Backend) (*Server, error) {
opts = append(opts, grpc.Creds(creds))
}

opts = append(opts, grpc.MaxRecvMsgSize(int(conf.MaxRequestBytes)))
opts = append(opts, grpc.MaxSendMsgSize(math.MaxInt32))
opts = append(opts, grpc.MaxConcurrentStreams(math.MaxUint32))

yorkieServiceCtx, yorkieServiceCancel := context.WithCancel(context.Background())
Expand Down
3 changes: 2 additions & 1 deletion yorkie/rpc/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,8 @@ func TestMain(m *testing.M) {
}

testRPCServer, err = rpc.NewServer(&rpc.Config{
Port: helper.RPCPort,
Port: helper.RPCPort,
MaxRequestBytes: helper.RPCMaxRequestBytes,
}, be)
if err != nil {
log.Fatal(err)
Expand Down

0 comments on commit e544cdb

Please sign in to comment.