Package io.grpc.netty

Class NettyServerBuilder

    • 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 given SocketAddress.
        Parameters:
        address - the socket address on which the server is to be bound.
        Returns:
        the server builder
      • 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.
      • 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 using channelType(Class) and workerEventLoopGroup(EventLoopGroup). For example, NioServerSocketChannel must use NioEventLoopGroup for both boss and worker EventLoopGroup, 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 a Server will continue to run even after the main thread has terminated. However, users have to be cautious when providing their own EventLoopGroups. For example, Netty's EventLoopGroups 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 call Server.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 using channelType(Class) and bossEventLoopGroup(EventLoopGroup). For example, NioServerSocketChannel must use NioEventLoopGroup for both boss and worker EventLoopGroup, 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 a Server will continue to run even after the main thread has terminated. However, users have to be cautious when providing their own EventLoopGroups. For example, Netty's EventLoopGroups 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 call Server.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 with GrpcSslContexts, but options could have been overridden.
      • protocolNegotiator

        @CanIgnoreReturnValue
        @Internal
        public final NettyServerBuilder protocolNegotiator​(io.grpc.netty.ProtocolNegotiator protocolNegotiator)
        Sets the ProtocolNegotiator to be used. Overrides the value specified in sslContext(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, use flowControlWindow(int). By default, auto flow control is enabled with initial flow control window size of DEFAULT_FLOW_CONTROL_WINDOW.
      • flowControlWindow

        @CanIgnoreReturnValue
        public NettyServerBuilder flowControlWindow​(int flowControlWindow)
        Sets the flow control window in bytes. Setting flowControlWindow disables auto flow control tuning; use initialFlowControlWindow(int) to enable auto flow control tuning. If not called, the default value is DEFAULT_FLOW_CONTROL_WINDOW) with auto flow control tuning.
      • maxMessageSize

        @CanIgnoreReturnValue
        @Deprecated
        @InlineMe(replacement="this.maxInboundMessageSize(maxMessageSize)")
        public NettyServerBuilder maxMessageSize​(int maxMessageSize)
        Deprecated.
        Call maxInboundMessageSize(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 class ForwardingServerBuilder<NettyServerBuilder>
        Parameters:
        bytes - the maximum number of bytes a single message can be.
        Returns:
        this
      • maxInboundMetadataSize

        @CanIgnoreReturnValue
        public NettyServerBuilder maxInboundMetadataSize​(int soft,
                                                         int max)
        Sets the size of metadata that clients are advised to not exceed. When a metadata with size larger than the soft limit is encountered there will be a probability the RPC will fail. The chance of failing increases as the metadata size approaches the hard limit. Integer.MAX_VALUE disables the enforcement. The default is implementation-dependent, but is not generally less than 8 KiB and may be unlimited.

        This is cumulative size of the metadata. The precise calculation is implementation-dependent, but implementations are encouraged to follow the calculation used for HTTP/2's SETTINGS_MAX_HEADER_LIST_SIZE. It sums the bytes from each entry's key and value, plus 32 bytes of overhead per entry.

        Parameters:
        soft - the soft size limit of received metadata
        max - the hard size limit of received metadata
        Returns:
        this
        Throws:
        IllegalArgumentException - if soft and/or max is non-positive, or max smaller than soft
        Since:
        1.68.0
      • 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 class ForwardingServerBuilder<NettyServerBuilder>
        Since:
        1.4.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 class ForwardingServerBuilder<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 class ForwardingServerBuilder<NettyServerBuilder>
        Since:
        1.3.0
        See Also:
        permitKeepAliveWithoutCalls(boolean)
      • 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 and maxConcurrentCallsPerConnection(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, or Integer.MAX_VALUE for unlimited
        secondsPerWindow - the positive number of seconds per period