Package org.littleshoot.proxy.impl
Class ServerGroup
java.lang.Object
org.littleshoot.proxy.impl.ServerGroup
Manages thread pools for one or more proxy server instances. When servers are created, they must register with the
ServerGroup using
registerProxyServer(HttpProxyServer), and when they shut down, must unregister with the
ServerGroup using unregisterProxyServer(HttpProxyServer, boolean).-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final intThe default number of threads to accept incoming requests from clients.static final intThe default number of threads to service incoming requests from clients.static final intThe default number of threads to service outgoing requests to servers.private final intprivate final intprivate static final org.slf4j.Loggerprivate final StringA name for this ServerGroup to use in naming threads.private final intprivate final EnumMap<TransportProtocol,ProxyThreadPools> A mapping ofTransportProtocols to their initializedProxyThreadPools.final List<HttpProxyServer>List of all servers registered to use this ServerGroup.private final ObjectLock controlling access to theregisterProxyServer(HttpProxyServer)andunregisterProxyServer(HttpProxyServer, boolean)methods.private static final AtomicIntegerGlobal counter for theserverGroupId.private final intThe ID of this server group.private final AtomicBooleanTrue when this ServerGroup is stopped.private final ObjectLock for initializing any transport protocols.private static final EnumMap<TransportProtocol,SelectorProvider> A mapping of selector providers to transport protocols. -
Constructor Summary
ConstructorsConstructorDescriptionServerGroup(String name, int incomingAcceptorThreads, int incomingWorkerThreads, int outgoingWorkerThreads) Creates a new ServerGroup instance for a proxy. -
Method Summary
Modifier and TypeMethodDescriptionRetrieves the client-to-proxy acceptor thread pool for the specified protocol.Retrieves the client-to-proxy acceptor worker pool for the specified protocol.Retrieves the proxy-to-server worker thread pool for the specified protocol.private ProxyThreadPoolsgetThreadPoolsForProtocol(TransportProtocol protocol) Retrieves theProxyThreadPoolsfor the specified transport protocol.booleanvoidregisterProxyServer(HttpProxyServer proxyServer) Registers the specified proxy server as a consumer of this server group.private voidshutdown(boolean graceful) Shuts down all event loops owned by this server group.voidunregisterProxyServer(HttpProxyServer proxyServer, boolean graceful) Unregisters the specified proxy server from this server group.
-
Field Details
-
log
private static final org.slf4j.Logger log -
DEFAULT_INCOMING_ACCEPTOR_THREADS
public static final int DEFAULT_INCOMING_ACCEPTOR_THREADSThe default number of threads to accept incoming requests from clients. (Requests are serviced by worker threads, not acceptor threads.)- See Also:
-
DEFAULT_INCOMING_WORKER_THREADS
public static final int DEFAULT_INCOMING_WORKER_THREADSThe default number of threads to service incoming requests from clients.- See Also:
-
DEFAULT_OUTGOING_WORKER_THREADS
public static final int DEFAULT_OUTGOING_WORKER_THREADSThe default number of threads to service outgoing requests to servers.- See Also:
-
serverGroupCount
Global counter for theserverGroupId. -
name
A name for this ServerGroup to use in naming threads. -
serverGroupId
private final int serverGroupIdThe ID of this server group. Forms part of the name of each thread created for this server group. Useful for differentiating threads when multiple proxy instances are running. -
incomingAcceptorThreads
private final int incomingAcceptorThreads -
incomingWorkerThreads
private final int incomingWorkerThreads -
outgoingWorkerThreads
private final int outgoingWorkerThreads -
registeredServers
List of all servers registered to use this ServerGroup. Any access to this list should be synchronized using theSERVER_REGISTRATION_LOCK. -
protocolThreadPools
A mapping ofTransportProtocols to their initializedProxyThreadPools. Each transport uses a different thread pool, since the initialization parameters are different. -
TRANSPORT_PROTOCOL_SELECTOR_PROVIDERS
private static final EnumMap<TransportProtocol,SelectorProvider> TRANSPORT_PROTOCOL_SELECTOR_PROVIDERSA mapping of selector providers to transport protocols. Avoids special-casing each transport protocol during transport protocol initialization. -
stopped
True when this ServerGroup is stopped. -
THREAD_POOL_INIT_LOCK
Lock for initializing any transport protocols. -
SERVER_REGISTRATION_LOCK
Lock controlling access to theregisterProxyServer(HttpProxyServer)andunregisterProxyServer(HttpProxyServer, boolean)methods.
-
-
Constructor Details
-
ServerGroup
public ServerGroup(String name, int incomingAcceptorThreads, int incomingWorkerThreads, int outgoingWorkerThreads) Creates a new ServerGroup instance for a proxy. Threads created for this ServerGroup will have the specified ServerGroup name in the Thread name. This constructor does not actually initialize any thread pools; instead, thread pools for specific transport protocols are lazily initialized as needed.- Parameters:
name- ServerGroup name to include in thread namesincomingAcceptorThreads- number of acceptor threads per protocolincomingWorkerThreads- number of client-to-proxy worker threads per protocoloutgoingWorkerThreads- number of proxy-to-server worker threads per protocol
-
-
Method Details
-
getThreadPoolsForProtocol
Retrieves theProxyThreadPoolsfor the specified transport protocol. Lazily initializes the thread pools for the transport protocol if they have not yet been initialized. If the protocol has already been initialized, this method returns immediately, without synchronization. If initialization is necessary, the initialization process creates the acceptor and worker threads necessary to service requests to/from the proxy.This method is thread-safe; no external locking is necessary.
- Parameters:
protocol- transport protocol to retrieve thread pools for- Returns:
- thread pools for the specified transport protocol
-
registerProxyServer
Registers the specified proxy server as a consumer of this server group. The server group will not be shut down until the proxy unregisters itself.- Parameters:
proxyServer- proxy server instance to register
-
unregisterProxyServer
Unregisters the specified proxy server from this server group. If this was the last registered proxy server, the server group will be shut down.- Parameters:
proxyServer- proxy server instance to unregistergraceful- when true, the server group shutdown (if necessary) will be graceful
-
shutdown
private void shutdown(boolean graceful) Shuts down all event loops owned by this server group.- Parameters:
graceful- when true, event loops will "gracefully" terminate, waiting for submitted tasks to finish
-
getClientToProxyAcceptorPoolForTransport
Retrieves the client-to-proxy acceptor thread pool for the specified protocol. Initializes the pool if it has not yet been initialized.This method is thread-safe; no external locking is necessary.
- Parameters:
protocol- transport protocol to retrieve the thread pool for- Returns:
- the client-to-proxy acceptor thread pool
-
getClientToProxyWorkerPoolForTransport
Retrieves the client-to-proxy acceptor worker pool for the specified protocol. Initializes the pool if it has not yet been initialized.This method is thread-safe; no external locking is necessary.
- Parameters:
protocol- transport protocol to retrieve the thread pool for- Returns:
- the client-to-proxy worker thread pool
-
getProxyToServerWorkerPoolForTransport
Retrieves the proxy-to-server worker thread pool for the specified protocol. Initializes the pool if it has not yet been initialized.This method is thread-safe; no external locking is necessary.
- Parameters:
protocol- transport protocol to retrieve the thread pool for- Returns:
- the proxy-to-server worker thread pool
-
isStopped
public boolean isStopped()- Returns:
- true if this ServerGroup has already been stopped
-