Commit 1a087f9b authored by Andrei Mihu's avatar Andrei Mihu
Browse files

Independent config flags for socket read and write buffer sizes.

parent e3aba332
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -7,9 +7,9 @@ The format is based on [keep a changelog](http://keepachangelog.com) and this pr
### Added
- Print a log message when all authoritative messages have stopped during graceful shutdown.
- Option for read-only globals in Lua runtime to reduce memory footprint. Enabled by default.
- Independent config flags for socket read and write buffer sizes.

### Changed
- Upper bound on socket buffer sizes. Flush buffer more than once per message if payload size exceeds buffer size.
- Build with Go 1.14.2 release.

### Fixed
+10 −2
Original line number Diff line number Diff line
@@ -107,8 +107,6 @@ func ParseArgs(logger *zap.Logger, args []string) Config {
		mainConfig.GetRuntime().Env = append(mainConfig.GetRuntime().Env, fmt.Sprintf("%v=%v", k, v))
	}

	logger.Warn("ENVIRONMENT", zap.Any("environment", mainConfig.GetRuntime().Environment), zap.Any("env", mainConfig.GetRuntime().Env))

	return mainConfig
}

@@ -156,6 +154,12 @@ func CheckConfig(logger *zap.Logger, config Config) map[string]string {
	if config.GetSocket().MaxMessageSizeBytes < 1 {
		logger.Fatal("Socket max message size bytes must be >= 1", zap.Int64("socket.max_message_size_bytes", config.GetSocket().MaxMessageSizeBytes))
	}
	if config.GetSocket().ReadBufferSizeBytes < 1 {
		logger.Fatal("Socket read buffer size bytes must be >= 1", zap.Int("socket.read_buffer_size_bytes", config.GetSocket().ReadBufferSizeBytes))
	}
	if config.GetSocket().WriteBufferSizeBytes < 1 {
		logger.Fatal("Socket write buffer size bytes must be >= 1", zap.Int("socket.write_buffer_size_bytes", config.GetSocket().WriteBufferSizeBytes))
	}
	if config.GetSocket().ReadTimeoutMs < 1 {
		logger.Fatal("Socket read timeout milliseconds must be >= 1", zap.Int("socket.read_timeout_ms", config.GetSocket().ReadTimeoutMs))
	}
@@ -539,6 +543,8 @@ type SocketConfig struct {
	Protocol             string            `yaml:"protocol" json:"protocol" usage:"The network protocol to listen for traffic on. Possible values are 'tcp' for both IPv4 and IPv6, 'tcp4' for IPv4 only, or 'tcp6' for IPv6 only. Default 'tcp'."`
	MaxMessageSizeBytes  int64             `yaml:"max_message_size_bytes" json:"max_message_size_bytes" usage:"Maximum amount of data in bytes allowed to be read from the client socket per message. Used for real-time connections."`
	MaxRequestSizeBytes  int64             `yaml:"max_request_size_bytes" json:"max_request_size_bytes" usage:"Maximum amount of data in bytes allowed to be read from clients per request. Used for gRPC and HTTP connections."`
	ReadBufferSizeBytes  int               `yaml:"read_buffer_size_bytes" json:"read_buffer_size_bytes" usage:"Size in bytes of the pre-allocated socket read buffer. Default 4096."`
	WriteBufferSizeBytes int               `yaml:"write_buffer_size_bytes" json:"write_buffer_size_bytes" usage:"Size in bytes of the pre-allocated socket write buffer. Default 4096."`
	ReadTimeoutMs        int               `yaml:"read_timeout_ms" json:"read_timeout_ms" usage:"Maximum duration in milliseconds for reading the entire request. Used for HTTP connections."`
	WriteTimeoutMs       int               `yaml:"write_timeout_ms" json:"write_timeout_ms" usage:"Maximum duration in milliseconds before timing out writes of the response. Used for HTTP connections."`
	IdleTimeoutMs        int               `yaml:"idle_timeout_ms" json:"idle_timeout_ms" usage:"Maximum amount of time in milliseconds to wait for the next request when keep-alives are enabled. Used for HTTP connections."`
@@ -563,6 +569,8 @@ func NewSocketConfig() *SocketConfig {
		Protocol:             "tcp",
		MaxMessageSizeBytes:  4096,
		MaxRequestSizeBytes:  0,
		ReadBufferSizeBytes:  4096,
		WriteBufferSizeBytes: 4096,
		ReadTimeoutMs:        10 * 1000,
		WriteTimeoutMs:       10 * 1000,
		IdleTimeoutMs:        60 * 1000,
+2 −7
Original line number Diff line number Diff line
@@ -29,14 +29,9 @@ import (
var SocketWsStatsCtx = context.Background()

func NewSocketWsAcceptor(logger *zap.Logger, config Config, sessionRegistry SessionRegistry, matchmaker Matchmaker, tracker Tracker, runtime *Runtime, jsonpbMarshaler *jsonpb.Marshaler, jsonpbUnmarshaler *jsonpb.Unmarshaler, pipeline *Pipeline) func(http.ResponseWriter, *http.Request) {
	// Select buffer size based on max message size, but cap it to 512KB total (256KB read, 256KB write).
	bufferSize := 256 * 1024
	if maxMessageSizeBytes := int(config.GetSocket().MaxMessageSizeBytes); maxMessageSizeBytes < bufferSize {
		bufferSize = maxMessageSizeBytes
	}
	upgrader := &websocket.Upgrader{
		ReadBufferSize:  bufferSize,
		WriteBufferSize: bufferSize,
		ReadBufferSize:  config.GetSocket().ReadBufferSizeBytes,
		WriteBufferSize: config.GetSocket().WriteBufferSizeBytes,
		CheckOrigin:     func(r *http.Request) bool { return true },
	}