Package io.grpc.internal
Class ServerImpl
java.lang.Object
io.grpc.Server
io.grpc.internal.ServerImpl
- All Implemented Interfaces:
InternalInstrumented<InternalChannelz.ServerStats>,InternalWithLogId
public final class ServerImpl
extends Server
implements InternalInstrumented<InternalChannelz.ServerStats>
Default implementation of
Server, for creation by transports.
Expected usage (by a theoretical TCP transport):
public class TcpTransportServerFactory {
public static Server newServer(Executor executor, HandlerRegistry registry,
String configuration) {
return new ServerImpl(executor, registry, new TcpTransportServer(configuration));
}
}
Starting the server starts the underlying transport for servicing requests. Stopping the server stops servicing new requests and waits for all connections to terminate.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescription(package private) static final class(package private) static final classDispatches callbacks onto an application-provided executor and correctly propagates exceptions.private static final classprivate final classprivate final class -
Field Summary
FieldsModifier and TypeFieldDescriptionprivate final BinaryLogprivate final InternalChannelzprivate final CompressorRegistryprivate final DecompressorRegistryprivate ExecutorExecutor for application processing.private final ObjectPool<? extends Executor> private final ServerCallExecutorSupplierprivate final HandlerRegistryprivate final longprivate final ServerInterceptor[]private final Objectprivate static final Loggerprivate final InternalLogIdprivate static final ServerStreamListenerprivate final HandlerRegistryprivate final Contextprivate final CallTracerprivate booleantrueif ServerListenerImpl.serverShutdown() was called.private booleanprivate Statusnon-nullif immediate shutdown has been requested.private booleanprivate booleanprivate final Deadline.Tickerprivate final List<ServerTransportFilter> private final Set<ServerTransport> transportServerand services encapsulating something similar to a TCP connection.private final InternalServerService encapsulating something similar to an accept() socket.private boolean -
Constructor Summary
ConstructorsConstructorDescriptionServerImpl(ServerImplBuilder builder, InternalServer transportServer, Context rootContext) Construct a server. -
Method Summary
Modifier and TypeMethodDescriptionvoidWaits for the server to become terminated.booleanawaitTermination(long timeout, TimeUnit unit) Waits for the server to become terminated, giving up if the timeout is reached.private voidNotify of complete shutdown if necessary.Returns immutable services registered with the server, or an empty list if not supported by the implementation.Returns a list of listening sockets for this server.private List<SocketAddress> getLogId()Returns an ID that is primarily used in debug logs.Returns mutable services registered with the server, or an empty list if not supported by the implementation.intgetPort()Returns the port number the server is listening on.Returns all services registered with the server, or an empty list if not supported by the implementation.com.google.common.util.concurrent.ListenableFuture<InternalChannelz.ServerStats> getStats()Returns the stats object.booleanReturns whether the server is shutdown.booleanReturns whether the server is terminated.shutdown()Initiates an orderly shutdown in which preexisting calls continue but new calls are rejected.Initiates a forceful shutdown in which preexisting and new calls are rejected.start()Bind and start the server.toString()private voidtransportClosed(ServerTransport transport) Remove transport service from accounting collection and notify of complete shutdown if necessary.
-
Field Details
-
log
-
NOOP_LISTENER
-
logId
-
executorPool
-
executor
Executor for application processing. Safe to read afterstart(). -
registry
-
fallbackRegistry
-
transportFilters
-
interceptors
-
handshakeTimeoutMillis
private final long handshakeTimeoutMillis -
started
private boolean started -
shutdown
private boolean shutdown -
shutdownNowStatus
non-nullif immediate shutdown has been requested. -
serverShutdownCallbackInvoked
private boolean serverShutdownCallbackInvokedtrueif ServerListenerImpl.serverShutdown() was called. -
terminated
private boolean terminated -
transportServer
Service encapsulating something similar to an accept() socket. -
lock
-
transportServersTerminated
private boolean transportServersTerminated -
transports
transportServerand services encapsulating something similar to a TCP connection. -
rootContext
-
decompressorRegistry
-
compressorRegistry
-
binlog
-
channelz
-
serverCallTracer
-
ticker
-
executorSupplier
-
-
Constructor Details
-
ServerImpl
ServerImpl(ServerImplBuilder builder, InternalServer transportServer, Context rootContext) Construct a server.- Parameters:
builder- builder with configuration for servertransportServer- transport servers that will create new incoming transportsrootContext- context that callbacks for new RPCs should be derived from
-
-
Method Details
-
start
Bind and start the server.- Specified by:
startin classServer- Returns:
thisobject- Throws:
IllegalStateException- if already startedIOException- if unable to bind
-
getPort
public int getPort()Description copied from class:ServerReturns the port number the server is listening on. This can return -1 if there is no actual port or the result otherwise does not make sense. Result is undefined after the server is terminated. If there are multiple possible ports, this will return one arbitrarily. Implementations are encouraged to return the same port on each call. -
getListenSockets
Description copied from class:ServerReturns a list of listening sockets for this server. May be different than the originally requested sockets (e.g. listening on port '0' may end up listening on a different port). The list is unmodifiable.- Overrides:
getListenSocketsin classServer
-
getListenSocketsIgnoringLifecycle
-
getServices
Description copied from class:ServerReturns all services registered with the server, or an empty list if not supported by the implementation.- Overrides:
getServicesin classServer
-
getImmutableServices
Description copied from class:ServerReturns immutable services registered with the server, or an empty list if not supported by the implementation.- Overrides:
getImmutableServicesin classServer
-
getMutableServices
Description copied from class:ServerReturns mutable services registered with the server, or an empty list if not supported by the implementation.- Overrides:
getMutableServicesin classServer
-
shutdown
Initiates an orderly shutdown in which preexisting calls continue but new calls are rejected. -
shutdownNow
Description copied from class:ServerInitiates a forceful shutdown in which preexisting and new calls are rejected. Although forceful, the shutdown process is still not instantaneous;Server.isTerminated()will likely returnfalseimmediately after this method returns. After this call returns, this server has released the listening socket(s) and may be reused by another server.Calling this method before
start()will shut down and terminate the server like normal, but prevents starting the server in the future.- Specified by:
shutdownNowin classServer- Returns:
thisobject
-
isShutdown
public boolean isShutdown()Description copied from class:ServerReturns whether the server is shutdown. Shutdown servers reject any new calls, but may still have some calls being processed.- Specified by:
isShutdownin classServer- See Also:
-
awaitTermination
Description copied from class:ServerWaits for the server to become terminated, giving up if the timeout is reached.Calling this method before
start()orshutdown()is permitted and does not change its behavior.- Specified by:
awaitTerminationin classServer- Returns:
- whether the server is terminated, as would be done by
Server.isTerminated(). - Throws:
InterruptedException
-
awaitTermination
Description copied from class:ServerWaits for the server to become terminated.Calling this method before
start()orshutdown()is permitted and does not change its behavior.- Specified by:
awaitTerminationin classServer- Throws:
InterruptedException
-
isTerminated
public boolean isTerminated()Description copied from class:ServerReturns whether the server is terminated. Terminated servers have no running calls and relevant resources released (like TCP connections).- Specified by:
isTerminatedin classServer- See Also:
-
transportClosed
Remove transport service from accounting collection and notify of complete shutdown if necessary.- Parameters:
transport- service to remove
-
checkForTermination
private void checkForTermination()Notify of complete shutdown if necessary. -
getLogId
Description copied from interface:InternalWithLogIdReturns an ID that is primarily used in debug logs. It usually contains the class name and a numeric ID that is unique among the instances.The subclasses of this interface usually want to include the log ID in their
Object.toString()results.- Specified by:
getLogIdin interfaceInternalWithLogId
-
getStats
Description copied from interface:InternalInstrumentedReturns the stats object.- Specified by:
getStatsin interfaceInternalInstrumented<InternalChannelz.ServerStats>
-
toString
-