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 ofServer, 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 Classes Modifier and Type Class Description (package private) static classServerImpl.ContextCloser(package private) static classServerImpl.JumpToApplicationThreadServerStreamListenerDispatches callbacks onto an application-provided executor and correctly propagates exceptions.private static classServerImpl.NoopListenerprivate classServerImpl.ServerListenerImplprivate classServerImpl.ServerTransportListenerImpl
-
Field Summary
Fields Modifier and Type Field Description private BinaryLogbinlogprivate InternalChannelzchannelzprivate CompressorRegistrycompressorRegistryprivate DecompressorRegistrydecompressorRegistryprivate java.util.concurrent.ExecutorexecutorExecutor for application processing.private ObjectPool<? extends java.util.concurrent.Executor>executorPoolprivate ServerCallExecutorSupplierexecutorSupplierprivate HandlerRegistryfallbackRegistryprivate longhandshakeTimeoutMillisprivate ServerInterceptor[]interceptorsprivate java.lang.Objectlockprivate static java.util.logging.Loggerlogprivate InternalLogIdlogIdprivate static ServerStreamListenerNOOP_LISTENERprivate HandlerRegistryregistryprivate ContextrootContextprivate CallTracerserverCallTracerprivate booleanserverShutdownCallbackInvokedtrueif ServerListenerImpl.serverShutdown() was called.private booleanshutdownprivate StatusshutdownNowStatusnon-nullif immediate shutdown has been requested.private booleanstartedprivate booleanterminatedprivate Deadline.Tickertickerprivate java.util.List<ServerTransportFilter>transportFiltersprivate java.util.Set<ServerTransport>transportstransportServerand services encapsulating something similar to a TCP connection.private InternalServertransportServerService encapsulating something similar to an accept() socket.private booleantransportServersTerminated
-
Constructor Summary
Constructors Constructor Description ServerImpl(ServerImplBuilder builder, InternalServer transportServer, Context rootContext)Construct a server.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidawaitTermination()Waits for the server to become terminated.booleanawaitTermination(long timeout, java.util.concurrent.TimeUnit unit)Waits for the server to become terminated, giving up if the timeout is reached.private voidcheckForTermination()Notify of complete shutdown if necessary.java.util.List<ServerServiceDefinition>getImmutableServices()Returns immutable services registered with the server, or an empty list if not supported by the implementation.java.util.List<java.net.SocketAddress>getListenSockets()Returns a list of listening sockets for this server.private java.util.List<java.net.SocketAddress>getListenSocketsIgnoringLifecycle()InternalLogIdgetLogId()Returns an ID that is primarily used in debug logs.java.util.List<ServerServiceDefinition>getMutableServices()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.java.util.List<ServerServiceDefinition>getServices()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.booleanisShutdown()Returns whether the server is shutdown.booleanisTerminated()Returns whether the server is terminated.ServerImplshutdown()Initiates an orderly shutdown in which preexisting calls continue but new calls are rejected.ServerImplshutdownNow()Initiates a forceful shutdown in which preexisting and new calls are rejected.ServerImplstart()Bind and start the server.java.lang.StringtoString()private voidtransportClosed(ServerTransport transport)Remove transport service from accounting collection and notify of complete shutdown if necessary.
-
-
-
Field Detail
-
log
private static final java.util.logging.Logger log
-
NOOP_LISTENER
private static final ServerStreamListener NOOP_LISTENER
-
logId
private final InternalLogId logId
-
executorPool
private final ObjectPool<? extends java.util.concurrent.Executor> executorPool
-
executor
private java.util.concurrent.Executor executor
Executor for application processing. Safe to read afterstart().
-
registry
private final HandlerRegistry registry
-
fallbackRegistry
private final HandlerRegistry fallbackRegistry
-
transportFilters
private final java.util.List<ServerTransportFilter> transportFilters
-
interceptors
private final ServerInterceptor[] interceptors
-
handshakeTimeoutMillis
private final long handshakeTimeoutMillis
-
started
private boolean started
-
shutdown
private boolean shutdown
-
shutdownNowStatus
private Status shutdownNowStatus
non-nullif immediate shutdown has been requested.
-
serverShutdownCallbackInvoked
private boolean serverShutdownCallbackInvoked
trueif ServerListenerImpl.serverShutdown() was called.
-
terminated
private boolean terminated
-
transportServer
private final InternalServer transportServer
Service encapsulating something similar to an accept() socket.
-
lock
private final java.lang.Object lock
-
transportServersTerminated
private boolean transportServersTerminated
-
transports
private final java.util.Set<ServerTransport> transports
transportServerand services encapsulating something similar to a TCP connection.
-
rootContext
private final Context rootContext
-
decompressorRegistry
private final DecompressorRegistry decompressorRegistry
-
compressorRegistry
private final CompressorRegistry compressorRegistry
-
binlog
private final BinaryLog binlog
-
channelz
private final InternalChannelz channelz
-
serverCallTracer
private final CallTracer serverCallTracer
-
ticker
private final Deadline.Ticker ticker
-
executorSupplier
private final ServerCallExecutorSupplier executorSupplier
-
-
Constructor Detail
-
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 Detail
-
start
public ServerImpl start() throws java.io.IOException
Bind and start the server.
-
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.- Overrides:
getPortin classServer- See Also:
Server.getListenSockets()
-
getListenSockets
public java.util.List<java.net.SocketAddress> 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
private java.util.List<java.net.SocketAddress> getListenSocketsIgnoringLifecycle()
-
getServices
public java.util.List<ServerServiceDefinition> 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
public java.util.List<ServerServiceDefinition> 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
public java.util.List<ServerServiceDefinition> 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
public ServerImpl shutdown()
Initiates an orderly shutdown in which preexisting calls continue but new calls are rejected.
-
shutdownNow
public ServerImpl 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:
Server.shutdown(),Server.isTerminated()
-
awaitTermination
public boolean awaitTermination(long timeout, java.util.concurrent.TimeUnit unit) throws java.lang.InterruptedExceptionDescription 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:
java.lang.InterruptedException
-
awaitTermination
public void awaitTermination() throws java.lang.InterruptedExceptionDescription 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:
java.lang.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:
Server.isShutdown()
-
transportClosed
private void transportClosed(ServerTransport transport)
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
public InternalLogId 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
public com.google.common.util.concurrent.ListenableFuture<InternalChannelz.ServerStats> getStats()
Description copied from interface:InternalInstrumentedReturns the stats object.- Specified by:
getStatsin interfaceInternalInstrumented<InternalChannelz.ServerStats>
-
toString
public java.lang.String toString()
- Overrides:
toStringin classjava.lang.Object
-
-