Class NettyServerBuilder
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionprivate final class -
Field Summary
FieldsModifier and TypeFieldDescriptionprivate static final longprivate booleanprivate ObjectPool<? extends io.netty.channel.EventLoopGroup> private io.netty.channel.ChannelFactory<? extends io.netty.channel.ServerChannel> private static final ObjectPool<? extends io.netty.channel.EventLoopGroup> static final intprivate static final ObjectPool<? extends io.netty.channel.EventLoopGroup> private Attributesprivate intprivate booleanprivate final booleanprivate longprivate longprivate final List<SocketAddress> (package private) static final long(package private) static final long(package private) static final long(package private) static final intprivate intprivate longprivate longprivate longprivate intprivate intprivate intprivate longprivate static final longprivate static final longprivate longprivate booleanprivate ProtocolNegotiator.ServerFactoryprivate final ServerImplBuilderprivate TransportTracer.Factoryprivate ObjectPool<? extends io.netty.channel.EventLoopGroup> -
Constructor Summary
ConstructorsModifierConstructorDescriptionprivateNettyServerBuilder(SocketAddress address) (package private)NettyServerBuilder(SocketAddress address, ProtocolNegotiator.ServerFactory negotiatorFactory) -
Method Summary
Modifier and TypeMethodDescriptionaddListenAddress(SocketAddress listenAddress) Adds an additional address for this server to listen on.(package private) voidbossEventLoopGroup(io.netty.channel.EventLoopGroup group) Provides the boss EventGroupLoop to the server.(package private) NettyServerBuilderbossEventLoopGroupPool(ObjectPool<? extends io.netty.channel.EventLoopGroup> bossEventLoopGroupPool) (package private) NettyServerbuildTransportServers(List<? extends ServerStreamTracer.Factory> streamTracerFactories) channelFactory(io.netty.channel.ChannelFactory<? extends io.netty.channel.ServerChannel> channelFactory) Specifies theChannelFactoryto createServerChannelinstances.channelType(Class<? extends io.netty.channel.ServerChannel> channelType) Specifies the channel type to use, by default we useEpollServerSocketChannelif available, otherwise usingNioServerSocketChannel.protected ServerBuilder<?> delegate()Returns the delegatedServerBuilder.(package private) voideagAttributes(Attributes eagAttributes) Sets the EAG attributes available to protocol negotiators.flowControlWindow(int flowControlWindow) Sets the flow control window in bytes.static NettyServerBuilderforAddress(SocketAddress address) Creates a server builder configured with the givenSocketAddress.static NettyServerBuilderforAddress(SocketAddress address, ServerCredentials creds) Creates a server builder configured with the givenSocketAddress.static NettyServerBuilderforPort(int port) Creates a server builder that will bind to the given port.static NettyServerBuilderforPort(int port, ServerCredentials creds) Creates a server builder that will bind to the given port.initialFlowControlWindow(int initialFlowControlWindow) Sets the initial flow control window in bytes.keepAliveTime(long keepAliveTime, TimeUnit timeUnit) Sets a custom keepalive time, the delay time for sending next keepalive ping.keepAliveTimeout(long keepAliveTimeout, TimeUnit timeUnit) Sets a custom keepalive timeout, the timeout for keepalive ping requests.maxConcurrentCallsPerConnection(int maxCalls) The maximum number of concurrent calls permitted for each incoming connection.maxConnectionAge(long maxConnectionAge, TimeUnit timeUnit) Sets a custom max connection age, connection lasting longer than which will be gracefully terminated.maxConnectionAgeGrace(long maxConnectionAgeGrace, TimeUnit timeUnit) Sets a custom grace time for the graceful connection termination.maxConnectionIdle(long maxConnectionIdle, TimeUnit timeUnit) Sets a custom max connection idle time, connection being idle for longer than which will be gracefully terminated.maxHeaderListSize(int maxHeaderListSize) Deprecated.maxInboundMessageSize(int bytes) Sets the maximum message size allowed to be received on the server.maxInboundMetadataSize(int bytes) Sets the maximum size of metadata allowed to be received.maxMessageSize(int maxMessageSize) Deprecated.CallmaxInboundMessageSize(int)instead.maxRstFramesPerWindow(int maxRstStream, int secondsPerWindow) Limits the rate of incoming RST_STREAM frames per connection to maxRstStream per secondsPerWindow.permitKeepAliveTime(long keepAliveTime, TimeUnit timeUnit) Specify the most aggressive keep-alive time clients are permitted to configure.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.final NettyServerBuilderprotocolNegotiator(ProtocolNegotiator protocolNegotiator) Sets theProtocolNegotiatorto be used.(package private) voidsetForceHeapBuffer(boolean value) Force using heap buffer when custom allocator is enabled.(package private) voidsetStatsEnabled(boolean value) (package private) voidsetStatsRecordRealTimeMetrics(boolean value) (package private) voidsetStatsRecordStartedRpcs(boolean value) (package private) voidsetTracingEnabled(boolean value) (package private) NettyServerBuildersetTransportTracerFactory(TransportTracer.Factory transportTracerFactory) sslContext(io.netty.handler.ssl.SslContext sslContext) Sets the TLS context to use for encryption.useTransportSecurity(File certChain, File privateKey) Makes the server use TLS.useTransportSecurity(InputStream certChain, InputStream privateKey) Makes the server use TLS.withChildOption(io.netty.channel.ChannelOption<T> option, T value) Specifies a child channel option.withOption(io.netty.channel.ChannelOption<T> option, T value) Specifies a channel option.workerEventLoopGroup(io.netty.channel.EventLoopGroup group) Provides the worker EventGroupLoop to the server.(package private) NettyServerBuilderworkerEventLoopGroupPool(ObjectPool<? extends io.netty.channel.EventLoopGroup> workerEventLoopGroupPool) Methods inherited from class ForwardingServerBuilder
addService, addService, addStreamTracerFactory, addTransportFilter, build, callExecutor, compressorRegistry, decompressorRegistry, directExecutor, executor, fallbackHandlerRegistry, handshakeTimeout, intercept, setBinaryLog, toStringMethods inherited from class ServerBuilder
addServices
-
Field Details
-
DEFAULT_FLOW_CONTROL_WINDOW
public static final int DEFAULT_FLOW_CONTROL_WINDOW- See Also:
-
MAX_CONNECTION_IDLE_NANOS_DISABLED
static final long MAX_CONNECTION_IDLE_NANOS_DISABLED- See Also:
-
MAX_CONNECTION_AGE_NANOS_DISABLED
static final long MAX_CONNECTION_AGE_NANOS_DISABLED- See Also:
-
MAX_CONNECTION_AGE_GRACE_NANOS_INFINITE
static final long MAX_CONNECTION_AGE_GRACE_NANOS_INFINITE- See Also:
-
MAX_RST_COUNT_DISABLED
static final int MAX_RST_COUNT_DISABLED- See Also:
-
MIN_MAX_CONNECTION_IDLE_NANO
private static final long MIN_MAX_CONNECTION_IDLE_NANO -
MIN_MAX_CONNECTION_AGE_NANO
private static final long MIN_MAX_CONNECTION_AGE_NANO -
AS_LARGE_AS_INFINITE
private static final long AS_LARGE_AS_INFINITE -
DEFAULT_BOSS_EVENT_LOOP_GROUP_POOL
private static final ObjectPool<? extends io.netty.channel.EventLoopGroup> DEFAULT_BOSS_EVENT_LOOP_GROUP_POOL -
DEFAULT_WORKER_EVENT_LOOP_GROUP_POOL
private static final ObjectPool<? extends io.netty.channel.EventLoopGroup> DEFAULT_WORKER_EVENT_LOOP_GROUP_POOL -
serverImplBuilder
-
listenAddresses
-
transportTracerFactory
-
channelFactory
private io.netty.channel.ChannelFactory<? extends io.netty.channel.ServerChannel> channelFactory -
channelOptions
-
childChannelOptions
-
bossEventLoopGroupPool
-
workerEventLoopGroupPool
-
forceHeapBuffer
private boolean forceHeapBuffer -
protocolNegotiatorFactory
-
freezeProtocolNegotiatorFactory
private final boolean freezeProtocolNegotiatorFactory -
maxConcurrentCallsPerConnection
private int maxConcurrentCallsPerConnection -
autoFlowControl
private boolean autoFlowControl -
flowControlWindow
private int flowControlWindow -
maxMessageSize
private int maxMessageSize -
maxHeaderListSize
private int maxHeaderListSize -
keepAliveTimeInNanos
private long keepAliveTimeInNanos -
keepAliveTimeoutInNanos
private long keepAliveTimeoutInNanos -
maxConnectionIdleInNanos
private long maxConnectionIdleInNanos -
maxConnectionAgeInNanos
private long maxConnectionAgeInNanos -
maxConnectionAgeGraceInNanos
private long maxConnectionAgeGraceInNanos -
permitKeepAliveWithoutCalls
private boolean permitKeepAliveWithoutCalls -
permitKeepAliveTimeInNanos
private long permitKeepAliveTimeInNanos -
maxRstCount
private int maxRstCount -
maxRstPeriodNanos
private long maxRstPeriodNanos -
eagAttributes
-
-
Constructor Details
-
NettyServerBuilder
-
NettyServerBuilder
NettyServerBuilder(SocketAddress address, ProtocolNegotiator.ServerFactory negotiatorFactory)
-
-
Method Details
-
forPort
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
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
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
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
Description copied from class:ForwardingServerBuilderReturns the delegatedServerBuilder.- Specified by:
delegatein classForwardingServerBuilder<NettyServerBuilder>
-
addListenAddress
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 io.netty.channel.ServerChannel> channelType) Specifies the channel type to use, by default we useEpollServerSocketChannelif available, otherwise usingNioServerSocketChannel.You either use this or
channelFactory(io.netty.channel.ChannelFactory)if yourServerChannelimplementation 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
EventLoopGroupusingworkerEventLoopGroup(EventLoopGroup)andbossEventLoopGroup(EventLoopGroup). For example,NioServerSocketChannelmust useNioEventLoopGroup, otherwise your server won't start. -
channelFactory
@CanIgnoreReturnValue public NettyServerBuilder channelFactory(io.netty.channel.ChannelFactory<? extends io.netty.channel.ServerChannel> channelFactory) Specifies theChannelFactoryto createServerChannelinstances. This method is usually only used if the specificServerChannelrequires 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
EventLoopGroupusingworkerEventLoopGroup(EventLoopGroup)andbossEventLoopGroup(EventLoopGroup). For example, if the factory createsNioServerSocketChannelyou must useNioEventLoopGroup, otherwise your server won't start. -
withOption
@CanIgnoreReturnValue public <T> NettyServerBuilder withOption(io.netty.channel.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(io.netty.channel.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(io.netty.channel.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
Channeltype usingchannelType(Class)andworkerEventLoopGroup(EventLoopGroup). For example,NioServerSocketChannelmust useNioEventLoopGroupfor 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
Threads by default and thus aServerwill continue to run even after the main thread has terminated. However, users have to be cautious when providing their ownEventLoopGroups. For example, Netty'sEventLoopGroups 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. -
bossEventLoopGroupPool
@CanIgnoreReturnValue NettyServerBuilder bossEventLoopGroupPool(ObjectPool<? extends io.netty.channel.EventLoopGroup> bossEventLoopGroupPool) -
workerEventLoopGroup
@CanIgnoreReturnValue public NettyServerBuilder workerEventLoopGroup(io.netty.channel.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
Channeltype usingchannelType(Class)andbossEventLoopGroup(EventLoopGroup). For example,NioServerSocketChannelmust useNioEventLoopGroupfor 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
Threads by default and thus aServerwill continue to run even after the main thread has terminated. However, users have to be cautious when providing their ownEventLoopGroups. For example, Netty'sEventLoopGroups 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. -
workerEventLoopGroupPool
@CanIgnoreReturnValue NettyServerBuilder workerEventLoopGroupPool(ObjectPool<? extends io.netty.channel.EventLoopGroup> workerEventLoopGroupPool) -
setForceHeapBuffer
void setForceHeapBuffer(boolean value) Force using heap buffer when custom allocator is enabled. -
sslContext
@CanIgnoreReturnValue public NettyServerBuilder sslContext(io.netty.handler.ssl.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(ProtocolNegotiator protocolNegotiator) Sets theProtocolNegotiatorto be used. Overrides the value specified insslContext(SslContext). -
setTracingEnabled
void setTracingEnabled(boolean value) -
setStatsEnabled
void setStatsEnabled(boolean value) -
setStatsRecordStartedRpcs
void setStatsRecordStartedRpcs(boolean value) -
setStatsRecordRealTimeMetrics
void setStatsRecordRealTimeMetrics(boolean value) -
maxConcurrentCallsPerConnection
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
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
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:
maxInboundMessageSizein 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
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:
maxInboundMetadataSizein 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_VALUEnano seconds or an unreasonably large value will disable keepalive.- Overrides:
keepAliveTimein classForwardingServerBuilder<NettyServerBuilder>- Since:
- 1.3.0
- See Also:
-
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:
keepAliveTimeoutin classForwardingServerBuilder<NettyServerBuilder>- Since:
- 1.3.0
- See Also:
-
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_VALUEnano seconds or an unreasonably large value will disable max connection idle.- Overrides:
maxConnectionIdlein classForwardingServerBuilder<NettyServerBuilder>- Since:
- 1.4.0
- See Also:
-
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_VALUEnano seconds or an unreasonably large value will disable max connection age.- Overrides:
maxConnectionAgein classForwardingServerBuilder<NettyServerBuilder>- Since:
- 1.3.0
- See Also:
-
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_VALUEnano seconds or an unreasonably large value are considered infinite.- Overrides:
maxConnectionAgeGracein classForwardingServerBuilder<NettyServerBuilder>- Since:
- 1.3.0
- See Also:
-
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:
permitKeepAliveTimein classForwardingServerBuilder<NettyServerBuilder>- Since:
- 1.3.0
- See Also:
-
permitKeepAliveWithoutCalls
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:
permitKeepAliveWithoutCallsin classForwardingServerBuilder<NettyServerBuilder>- Since:
- 1.3.0
- See Also:
-
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_VALUEfor unlimitedsecondsPerWindow- the positive number of seconds per period
-
eagAttributes
Sets the EAG attributes available to protocol negotiators. Not for general use. -
buildTransportServers
-
assertEventLoopsAndChannelType
void assertEventLoopsAndChannelType() -
setTransportTracerFactory
@CanIgnoreReturnValue NettyServerBuilder setTransportTracerFactory(TransportTracer.Factory transportTracerFactory) -
useTransportSecurity
@CanIgnoreReturnValue public NettyServerBuilder useTransportSecurity(File certChain, File privateKey) Description copied from class:ServerBuilderMakes the server use TLS.- Overrides:
useTransportSecurityin 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:ServerBuilderMakes the server use TLS.- Overrides:
useTransportSecurityin classForwardingServerBuilder<NettyServerBuilder>- Parameters:
certChain- InputStream containing the full certificate chainprivateKey- InputStream containing the private key- Returns:
- this
-
maxInboundMetadataSize(int)instead