Class OkHttpServerBuilder


  • @ExperimentalApi("https://github.com/grpc/grpc-java/issues/1785")
    public final class OkHttpServerBuilder
    extends io.grpc.ForwardingServerBuilder<OkHttpServerBuilder>
    Build servers with the OkHttp transport.
    Since:
    1.49.0
    • Method Detail

      • forPort

        public static OkHttpServerBuilder forPort​(int port,
                                                  io.grpc.ServerCredentials creds)
        Creates a builder for a server listening on port.
      • forPort

        public static OkHttpServerBuilder forPort​(java.net.SocketAddress address,
                                                  io.grpc.ServerCredentials creds)
        Creates a builder for a server listening on address.
      • delegate

        @Internal
        protected io.grpc.ServerBuilder<?> delegate()
        Specified by:
        delegate in class io.grpc.ForwardingServerBuilder<OkHttpServerBuilder>
      • transportExecutor

        public OkHttpServerBuilder transportExecutor​(java.util.concurrent.Executor transportExecutor)
        Override the default executor necessary for internal transport use.

        The channel does not take ownership of the given executor. It is the caller' responsibility to shutdown the executor when appropriate.

      • socketFactory

        public OkHttpServerBuilder socketFactory​(javax.net.ServerSocketFactory socketFactory)
        Override the default ServerSocketFactory used to listen. If the socket factory is not set or set to null, a default one will be used.
      • keepAliveTime

        public OkHttpServerBuilder keepAliveTime​(long keepAliveTime,
                                                 java.util.concurrent.TimeUnit timeUnit)
        Sets the time without read activity before sending a keepalive ping. An unreasonably small value might be increased, and Long.MAX_VALUE nano seconds or an unreasonably large value will disable keepalive. Defaults to two hours.
        Overrides:
        keepAliveTime in class io.grpc.ForwardingServerBuilder<OkHttpServerBuilder>
        Throws:
        java.lang.IllegalArgumentException - if time is not positive
      • maxConnectionIdle

        public OkHttpServerBuilder maxConnectionIdle​(long maxConnectionIdle,
                                                     java.util.concurrent.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 io.grpc.ForwardingServerBuilder<OkHttpServerBuilder>
      • maxConnectionAge

        public OkHttpServerBuilder maxConnectionAge​(long maxConnectionAge,
                                                    java.util.concurrent.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 class io.grpc.ForwardingServerBuilder<OkHttpServerBuilder>
      • maxConnectionAgeGrace

        public OkHttpServerBuilder maxConnectionAgeGrace​(long maxConnectionAgeGrace,
                                                         java.util.concurrent.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 io.grpc.ForwardingServerBuilder<OkHttpServerBuilder>
        See Also:
        maxConnectionAge(long, TimeUnit)
      • keepAliveTimeout

        public OkHttpServerBuilder keepAliveTimeout​(long keepAliveTimeout,
                                                    java.util.concurrent.TimeUnit timeUnit)
        Sets a time waiting for read activity after sending a keepalive ping. If the time expires without any read activity on the connection, the connection is considered dead. An unreasonably small value might be increased. Defaults to 20 seconds.

        This value should be at least multiple times the RTT to allow for lost packets.

        Overrides:
        keepAliveTimeout in class io.grpc.ForwardingServerBuilder<OkHttpServerBuilder>
        Throws:
        java.lang.IllegalArgumentException - if timeout is not positive
      • permitKeepAliveTime

        @CanIgnoreReturnValue
        public OkHttpServerBuilder permitKeepAliveTime​(long keepAliveTime,
                                                       java.util.concurrent.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 io.grpc.ForwardingServerBuilder<OkHttpServerBuilder>
        See Also:
        permitKeepAliveWithoutCalls(boolean)
      • permitKeepAliveWithoutCalls

        @CanIgnoreReturnValue
        public OkHttpServerBuilder 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 to false.
        Overrides:
        permitKeepAliveWithoutCalls in class io.grpc.ForwardingServerBuilder<OkHttpServerBuilder>
        See Also:
        permitKeepAliveTime(long, TimeUnit)
      • flowControlWindow

        public OkHttpServerBuilder flowControlWindow​(int flowControlWindow)
        Sets the flow control window in bytes. If not called, the default value is 64 KiB.
      • scheduledExecutorService

        public OkHttpServerBuilder scheduledExecutorService​(java.util.concurrent.ScheduledExecutorService scheduledExecutorService)
        Provides a custom scheduled executor service.

        It's an optional parameter. If the user has not provided a scheduled executor service when the channel is built, the builder will use a static thread pool.

        Returns:
        this
      • maxInboundMetadataSize

        public OkHttpServerBuilder maxInboundMetadataSize​(int bytes)
        Sets the maximum size of metadata allowed to be received. Defaults to 8 KiB.

        The implementation does not currently limit memory usage; this value is checked only after the metadata is decoded from the wire. It does prevent large metadata from being passed to the application.

        Overrides:
        maxInboundMetadataSize in class io.grpc.ForwardingServerBuilder<OkHttpServerBuilder>
        Parameters:
        bytes - the maximum size of received metadata
        Returns:
        this
        Throws:
        java.lang.IllegalArgumentException - if bytes is non-positive
      • maxInboundMessageSize

        public OkHttpServerBuilder maxInboundMessageSize​(int bytes)
        Sets the maximum message size allowed to be received on the server. If not called, defaults to 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.
        Overrides:
        maxInboundMessageSize in class io.grpc.ForwardingServerBuilder<OkHttpServerBuilder>
        Parameters:
        bytes - the maximum number of bytes a single message can be.
        Returns:
        this
        Throws:
        java.lang.IllegalArgumentException - if bytes is negative.