Class NettyServerBuilder
- java.lang.Object
-
- io.grpc.ServerBuilder<T>
-
- io.grpc.ForwardingServerBuilder<NettyServerBuilder>
-
- io.grpc.netty.NettyServerBuilder
-
@ExperimentalApi("https://github.com/grpc/grpc-java/issues/1784") @CheckReturnValue public final class NettyServerBuilder extends ForwardingServerBuilder<NettyServerBuilder>
A builder to help simplify the construction of a Netty-based GRPC server.
-
-
Field Summary
Fields Modifier and Type Field Description static int
DEFAULT_FLOW_CONTROL_WINDOW
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Deprecated Methods Modifier and Type Method Description NettyServerBuilder
addListenAddress(SocketAddress listenAddress)
Adds an additional address for this server to listen on.NettyServerBuilder
bossEventLoopGroup(EventLoopGroup group)
Provides the boss EventGroupLoop to the server.NettyServerBuilder
channelFactory(ChannelFactory<? extends ServerChannel> channelFactory)
Specifies theChannelFactory
to createServerChannel
instances.NettyServerBuilder
channelType(Class<? extends ServerChannel> channelType)
Specifies the channel type to use, by default we useEpollServerSocketChannel
if available, otherwise usingNioServerSocketChannel
.protected ServerBuilder<?>
delegate()
Returns the delegatedServerBuilder
.NettyServerBuilder
flowControlWindow(int flowControlWindow)
Sets the flow control window in bytes.static NettyServerBuilder
forAddress(SocketAddress address)
Creates a server builder configured with the givenSocketAddress
.static NettyServerBuilder
forAddress(SocketAddress address, ServerCredentials creds)
Creates a server builder configured with the givenSocketAddress
.static NettyServerBuilder
forPort(int port)
Creates a server builder that will bind to the given port.static NettyServerBuilder
forPort(int port, ServerCredentials creds)
Creates a server builder that will bind to the given port.NettyServerBuilder
initialFlowControlWindow(int initialFlowControlWindow)
Sets the initial flow control window in bytes.NettyServerBuilder
keepAliveTime(long keepAliveTime, TimeUnit timeUnit)
Sets a custom keepalive time, the delay time for sending next keepalive ping.NettyServerBuilder
keepAliveTimeout(long keepAliveTimeout, TimeUnit timeUnit)
Sets a custom keepalive timeout, the timeout for keepalive ping requests.NettyServerBuilder
maxConcurrentCallsPerConnection(int maxCalls)
The maximum number of concurrent calls permitted for each incoming connection.NettyServerBuilder
maxConnectionAge(long maxConnectionAge, TimeUnit timeUnit)
Sets a custom max connection age, connection lasting longer than which will be gracefully terminated.NettyServerBuilder
maxConnectionAgeGrace(long maxConnectionAgeGrace, TimeUnit timeUnit)
Sets a custom grace time for the graceful connection termination.NettyServerBuilder
maxConnectionIdle(long maxConnectionIdle, TimeUnit timeUnit)
Sets a custom max connection idle time, connection being idle for longer than which will be gracefully terminated.NettyServerBuilder
maxHeaderListSize(int maxHeaderListSize)
Deprecated.UsemaxInboundMetadataSize(int)
insteadNettyServerBuilder
maxInboundMessageSize(int bytes)
Sets the maximum message size allowed to be received on the server.NettyServerBuilder
maxInboundMetadataSize(int bytes)
Sets the maximum size of metadata allowed to be received.NettyServerBuilder
maxMessageSize(int maxMessageSize)
Deprecated.CallmaxInboundMessageSize(int)
instead.NettyServerBuilder
maxRstFramesPerWindow(int maxRstStream, int secondsPerWindow)
Limits the rate of incoming RST_STREAM frames per connection to maxRstStream per secondsPerWindow.NettyServerBuilder
permitKeepAliveTime(long keepAliveTime, TimeUnit timeUnit)
Specify the most aggressive keep-alive time clients are permitted to configure.NettyServerBuilder
permitKeepAliveWithoutCalls(boolean permit)
Sets whether to allow clients to send keep-alive HTTP/2 PINGs even if there are no outstanding RPCs on the connection.NettyServerBuilder
protocolNegotiator(io.grpc.netty.ProtocolNegotiator protocolNegotiator)
Sets theProtocolNegotiator
to be used.NettyServerBuilder
sslContext(SslContext sslContext)
Sets the TLS context to use for encryption.NettyServerBuilder
useTransportSecurity(File certChain, File privateKey)
Makes the server use TLS.NettyServerBuilder
useTransportSecurity(InputStream certChain, InputStream privateKey)
Makes the server use TLS.<T> NettyServerBuilder
withChildOption(ChannelOption<T> option, T value)
Specifies a child channel option.<T> NettyServerBuilder
withOption(ChannelOption<T> option, T value)
Specifies a channel option.NettyServerBuilder
workerEventLoopGroup(EventLoopGroup group)
Provides the worker EventGroupLoop to the server.-
Methods inherited from class io.grpc.ForwardingServerBuilder
addService, addService, addStreamTracerFactory, addTransportFilter, build, callExecutor, compressorRegistry, decompressorRegistry, directExecutor, executor, fallbackHandlerRegistry, handshakeTimeout, intercept, setBinaryLog, toString
-
Methods inherited from class io.grpc.ServerBuilder
addServices
-
-
-
-
Field Detail
-
DEFAULT_FLOW_CONTROL_WINDOW
public static final int DEFAULT_FLOW_CONTROL_WINDOW
- See Also:
- Constant Field Values
-
-
Method Detail
-
forPort
public static NettyServerBuilder forPort(int port)
Creates a server builder that will bind to the given port.- Parameters:
port
- the port on which the server is to be bound.- Returns:
- the server builder.
-
forPort
public static NettyServerBuilder forPort(int port, ServerCredentials creds)
Creates a server builder that will bind to the given port.- Parameters:
port
- the port on which the server is to be bound.- Returns:
- the server builder.
-
forAddress
public static NettyServerBuilder forAddress(SocketAddress address)
Creates a server builder configured with the givenSocketAddress
.- Parameters:
address
- the socket address on which the server is to be bound.- Returns:
- the server builder
-
forAddress
public static NettyServerBuilder forAddress(SocketAddress address, ServerCredentials creds)
Creates a server builder configured with the givenSocketAddress
.- Parameters:
address
- the socket address on which the server is to be bound.- Returns:
- the server builder
-
delegate
@Internal protected ServerBuilder<?> delegate()
Description copied from class:ForwardingServerBuilder
Returns the delegatedServerBuilder
.- Specified by:
delegate
in classForwardingServerBuilder<NettyServerBuilder>
-
addListenAddress
@CanIgnoreReturnValue public NettyServerBuilder addListenAddress(SocketAddress listenAddress)
Adds an additional address for this server to listen on. Callers must ensure that all socket addresses are compatible with the Netty channel type, and that they don't conflict with each other.
-
channelType
@CanIgnoreReturnValue public NettyServerBuilder channelType(Class<? extends ServerChannel> channelType)
Specifies the channel type to use, by default we useEpollServerSocketChannel
if available, otherwise usingNioServerSocketChannel
.You either use this or
channelFactory(io.netty.channel.ChannelFactory)
if yourServerChannel
implementation has no no-args constructor.It's an optional parameter. If the user has not provided an Channel type or ChannelFactory when the channel is built, the builder will use the default one which is static.
You must also provide corresponding
EventLoopGroup
usingworkerEventLoopGroup(EventLoopGroup)
andbossEventLoopGroup(EventLoopGroup)
. For example,NioServerSocketChannel
must useNioEventLoopGroup
, otherwise your server won't start.
-
channelFactory
@CanIgnoreReturnValue public NettyServerBuilder channelFactory(ChannelFactory<? extends ServerChannel> channelFactory)
Specifies theChannelFactory
to createServerChannel
instances. This method is usually only used if the specificServerChannel
requires complex logic which requires additional information to create theServerChannel
. Otherwise, recommend to usechannelType(Class)
.It's an optional parameter. If the user has not provided an Channel type or ChannelFactory when the channel is built, the builder will use the default one which is static.
You must also provide corresponding
EventLoopGroup
usingworkerEventLoopGroup(EventLoopGroup)
andbossEventLoopGroup(EventLoopGroup)
. For example, if the factory createsNioServerSocketChannel
you must useNioEventLoopGroup
, otherwise your server won't start.
-
withOption
@CanIgnoreReturnValue public <T> NettyServerBuilder withOption(ChannelOption<T> option, T value)
Specifies a channel option. As the underlying channel as well as network implementation may ignore this value applications should consider it a hint.- Since:
- 1.30.0
-
withChildOption
@CanIgnoreReturnValue public <T> NettyServerBuilder withChildOption(ChannelOption<T> option, T value)
Specifies a child channel option. As the underlying channel as well as network implementation may ignore this value applications should consider it a hint.- Since:
- 1.9.0
-
bossEventLoopGroup
@CanIgnoreReturnValue public NettyServerBuilder bossEventLoopGroup(EventLoopGroup group)
Provides the boss EventGroupLoop to the server.It's an optional parameter. If the user has not provided one when the server is built, the builder will use the default one which is static.
You must also provide corresponding
Channel
type usingchannelType(Class)
andworkerEventLoopGroup(EventLoopGroup)
. For example,NioServerSocketChannel
must useNioEventLoopGroup
for both boss and workerEventLoopGroup
, otherwise your server won't start.The server won't take ownership of the given EventLoopGroup. It's caller's responsibility to shut it down when it's desired.
Grpc uses non-daemon
Thread
s by default and thus aServer
will continue to run even after the main thread has terminated. However, users have to be cautious when providing their ownEventLoopGroup
s. For example, Netty'sEventLoopGroup
s use daemon threads by default and thus an application with only daemon threads running besides the main thread will exit as soon as the main thread completes. A simple solution to this problem is to callServer.awaitTermination()
to keep the main thread alive until the server has terminated.
-
workerEventLoopGroup
@CanIgnoreReturnValue public NettyServerBuilder workerEventLoopGroup(EventLoopGroup group)
Provides the worker EventGroupLoop to the server.It's an optional parameter. If the user has not provided one when the server is built, the builder will create one.
You must also provide corresponding
Channel
type usingchannelType(Class)
andbossEventLoopGroup(EventLoopGroup)
. For example,NioServerSocketChannel
must useNioEventLoopGroup
for both boss and workerEventLoopGroup
, otherwise your server won't start.The server won't take ownership of the given EventLoopGroup. It's caller's responsibility to shut it down when it's desired.
Grpc uses non-daemon
Thread
s by default and thus aServer
will continue to run even after the main thread has terminated. However, users have to be cautious when providing their ownEventLoopGroup
s. For example, Netty'sEventLoopGroup
s use daemon threads by default and thus an application with only daemon threads running besides the main thread will exit as soon as the main thread completes. A simple solution to this problem is to callServer.awaitTermination()
to keep the main thread alive until the server has terminated.
-
sslContext
@CanIgnoreReturnValue public NettyServerBuilder sslContext(SslContext sslContext)
Sets the TLS context to use for encryption. Providing a context enables encryption. It must have been configured withGrpcSslContexts
, but options could have been overridden.
-
protocolNegotiator
@CanIgnoreReturnValue @Internal public final NettyServerBuilder protocolNegotiator(io.grpc.netty.ProtocolNegotiator protocolNegotiator)
Sets theProtocolNegotiator
to be used. Overrides the value specified insslContext(SslContext)
.
-
maxConcurrentCallsPerConnection
@CanIgnoreReturnValue public NettyServerBuilder maxConcurrentCallsPerConnection(int maxCalls)
The maximum number of concurrent calls permitted for each incoming connection. Defaults to no limit.
-
initialFlowControlWindow
@CanIgnoreReturnValue public NettyServerBuilder initialFlowControlWindow(int initialFlowControlWindow)
Sets the initial flow control window in bytes. Setting initial flow control window enables auto flow control tuning using bandwidth-delay product algorithm. To disable auto flow control tuning, useflowControlWindow(int)
. By default, auto flow control is enabled with initial flow control window size ofDEFAULT_FLOW_CONTROL_WINDOW
.
-
flowControlWindow
@CanIgnoreReturnValue public NettyServerBuilder flowControlWindow(int flowControlWindow)
Sets the flow control window in bytes. Setting flowControlWindow disables auto flow control tuning; useinitialFlowControlWindow(int)
to enable auto flow control tuning. If not called, the default value isDEFAULT_FLOW_CONTROL_WINDOW
) with auto flow control tuning.
-
maxMessageSize
@CanIgnoreReturnValue @Deprecated @InlineMe(replacement="this.maxInboundMessageSize(maxMessageSize)") public NettyServerBuilder maxMessageSize(int maxMessageSize)
Deprecated.CallmaxInboundMessageSize(int)
instead. This method will be removed in a future release.Sets the maximum message size allowed to be received on the server. If not called, defaults to 4 MiB. The default provides protection to services who haven't considered the possibility of receiving large messages while trying to be large enough to not be hit in normal usage.
-
maxInboundMessageSize
@CanIgnoreReturnValue public NettyServerBuilder maxInboundMessageSize(int bytes)
Sets the maximum message size allowed to be received on the server. If not called, defaults to 4 MiB. The default provides protection to servers who haven't considered the possibility of receiving large messages while trying to be large enough to not be hit in normal usage.This method is advisory, and implementations may decide to not enforce this. Currently, the only known transport to not enforce this is
InProcessServer
.- Overrides:
maxInboundMessageSize
in classForwardingServerBuilder<NettyServerBuilder>
- Parameters:
bytes
- the maximum number of bytes a single message can be.- Returns:
- this
-
maxHeaderListSize
@CanIgnoreReturnValue @Deprecated @InlineMe(replacement="this.maxInboundMetadataSize(maxHeaderListSize)") public NettyServerBuilder maxHeaderListSize(int maxHeaderListSize)
Deprecated.UsemaxInboundMetadataSize(int)
insteadSets the maximum size of header list allowed to be received. This is cumulative size of the headers with some overhead, as defined for HTTP/2's SETTINGS_MAX_HEADER_LIST_SIZE. The default is 8 KiB.
-
maxInboundMetadataSize
@CanIgnoreReturnValue public NettyServerBuilder maxInboundMetadataSize(int bytes)
Sets the maximum size of metadata allowed to be received. This is cumulative size of the entries with some overhead, as defined for HTTP/2's SETTINGS_MAX_HEADER_LIST_SIZE. The default is 8 KiB.- Overrides:
maxInboundMetadataSize
in classForwardingServerBuilder<NettyServerBuilder>
- Parameters:
bytes
- the maximum size of received metadata- Returns:
- this
- Throws:
IllegalArgumentException
- if bytes is non-positive- Since:
- 1.17.0
-
keepAliveTime
@CanIgnoreReturnValue public NettyServerBuilder keepAliveTime(long keepAliveTime, TimeUnit timeUnit)
Sets a custom keepalive time, the delay time for sending next keepalive ping. An unreasonably small value might be increased, andLong.MAX_VALUE
nano seconds or an unreasonably large value will disable keepalive.- Overrides:
keepAliveTime
in classForwardingServerBuilder<NettyServerBuilder>
- Since:
- 1.3.0
- See Also:
- gRFC A9 Server-side Connection Management
-
keepAliveTimeout
@CanIgnoreReturnValue public NettyServerBuilder keepAliveTimeout(long keepAliveTimeout, TimeUnit timeUnit)
Sets a custom keepalive timeout, the timeout for keepalive ping requests. An unreasonably small value might be increased.- Overrides:
keepAliveTimeout
in classForwardingServerBuilder<NettyServerBuilder>
- Since:
- 1.3.0
- See Also:
- gRFC A9 Server-side Connection Management
-
maxConnectionIdle
@CanIgnoreReturnValue public NettyServerBuilder maxConnectionIdle(long maxConnectionIdle, TimeUnit timeUnit)
Sets a custom max connection idle time, connection being idle for longer than which will be gracefully terminated. Idleness duration is defined since the most recent time the number of outstanding RPCs became zero or the connection establishment. An unreasonably small value might be increased.Long.MAX_VALUE
nano seconds or an unreasonably large value will disable max connection idle.- Overrides:
maxConnectionIdle
in classForwardingServerBuilder<NettyServerBuilder>
- Since:
- 1.4.0
- See Also:
- gRFC A9 Server-side Connection Management
-
maxConnectionAge
@CanIgnoreReturnValue public NettyServerBuilder maxConnectionAge(long maxConnectionAge, TimeUnit timeUnit)
Sets a custom max connection age, connection lasting longer than which will be gracefully terminated. An unreasonably small value might be increased. A random jitter of +/-10% will be added to it.Long.MAX_VALUE
nano seconds or an unreasonably large value will disable max connection age.- Overrides:
maxConnectionAge
in classForwardingServerBuilder<NettyServerBuilder>
- Since:
- 1.3.0
- See Also:
- gRFC A9 Server-side Connection Management
-
maxConnectionAgeGrace
@CanIgnoreReturnValue public NettyServerBuilder maxConnectionAgeGrace(long maxConnectionAgeGrace, TimeUnit timeUnit)
Sets a custom grace time for the graceful connection termination. Once the max connection age is reached, RPCs have the grace time to complete. RPCs that do not complete in time will be cancelled, allowing the connection to terminate.Long.MAX_VALUE
nano seconds or an unreasonably large value are considered infinite.- Overrides:
maxConnectionAgeGrace
in classForwardingServerBuilder<NettyServerBuilder>
- Since:
- 1.3.0
- See Also:
maxConnectionAge(long, TimeUnit)
-
permitKeepAliveTime
@CanIgnoreReturnValue public NettyServerBuilder permitKeepAliveTime(long keepAliveTime, TimeUnit timeUnit)
Specify the most aggressive keep-alive time clients are permitted to configure. The server will try to detect clients exceeding this rate and when detected will forcefully close the connection. The default is 5 minutes.Even though a default is defined that allows some keep-alives, clients must not use keep-alive without approval from the service owner. Otherwise, they may experience failures in the future if the service becomes more restrictive. When unthrottled, keep-alives can cause a significant amount of traffic and CPU usage, so clients and servers should be conservative in what they use and accept.
- Overrides:
permitKeepAliveTime
in classForwardingServerBuilder<NettyServerBuilder>
- Since:
- 1.3.0
- See Also:
permitKeepAliveWithoutCalls(boolean)
-
permitKeepAliveWithoutCalls
@CanIgnoreReturnValue public NettyServerBuilder permitKeepAliveWithoutCalls(boolean permit)
Sets whether to allow clients to send keep-alive HTTP/2 PINGs even if there are no outstanding RPCs on the connection. Defaults tofalse
.- Overrides:
permitKeepAliveWithoutCalls
in classForwardingServerBuilder<NettyServerBuilder>
- Since:
- 1.3.0
- See Also:
permitKeepAliveTime(long, TimeUnit)
-
maxRstFramesPerWindow
@CanIgnoreReturnValue public NettyServerBuilder maxRstFramesPerWindow(int maxRstStream, int secondsPerWindow)
Limits the rate of incoming RST_STREAM frames per connection to maxRstStream per secondsPerWindow. When exceeded on a connection, the connection is closed. This can reduce the impact of an attacker continually resetting RPCs before they complete, when combined with TLS andmaxConcurrentCallsPerConnection(int)
.gRPC clients send RST_STREAM when they cancel RPCs, so some RST_STREAMs are normal and setting this too low can cause errors for legimitate clients.
By default there is no limit.
- Parameters:
maxRstStream
- the positive limit of RST_STREAM frames per connection per period, orInteger.MAX_VALUE
for unlimitedsecondsPerWindow
- the positive number of seconds per period
-
useTransportSecurity
@CanIgnoreReturnValue public NettyServerBuilder useTransportSecurity(File certChain, File privateKey)
Description copied from class:ServerBuilder
Makes the server use TLS.- Overrides:
useTransportSecurity
in classForwardingServerBuilder<NettyServerBuilder>
- Parameters:
certChain
- file containing the full certificate chainprivateKey
- file containing the private key- Returns:
- this
-
useTransportSecurity
@CanIgnoreReturnValue public NettyServerBuilder useTransportSecurity(InputStream certChain, InputStream privateKey)
Description copied from class:ServerBuilder
Makes the server use TLS.- Overrides:
useTransportSecurity
in classForwardingServerBuilder<NettyServerBuilder>
- Parameters:
certChain
- InputStream containing the full certificate chainprivateKey
- InputStream containing the private key- Returns:
- this
-
-