Package com.github.twitch4j.common.pool
Class SubscriptionConnectionPool<C,S,T,U>
java.lang.Object
com.github.twitch4j.common.pool.AbstractConnectionPool<C>
com.github.twitch4j.common.pool.SubscriptionConnectionPool<C,S,T,U>
- Type Parameters:
C
- the connection typeS
- the subscription requestT
- transactional subscription response and potential unsubscription requestU
- the unsubscription response
- All Implemented Interfaces:
TransactionalSubscriber<S,
,T, U> AutoCloseable
- Direct Known Subclasses:
TwitchModuleConnectionPool
public abstract class SubscriptionConnectionPool<C,S,T,U>
extends AbstractConnectionPool<C>
implements TransactionalSubscriber<S,T,U>
A pool of connections for making subscriptions (and potentially unsubscribing from later).
-
Nested Class Summary
Modifier and TypeClassDescriptionstatic class
SubscriptionConnectionPool.SubscriptionConnectionPoolBuilder<C,
S, T, U, C2 extends SubscriptionConnectionPool<C, S, T, U>, B extends SubscriptionConnectionPool.SubscriptionConnectionPoolBuilder<C, S, T, U, C2, B>> Nested classes/interfaces inherited from class com.github.twitch4j.common.pool.AbstractConnectionPool
AbstractConnectionPool.AbstractConnectionPoolBuilder<C,
C2 extends AbstractConnectionPool<C>, B extends AbstractConnectionPool.AbstractConnectionPoolBuilder<C, C2, B>> -
Field Summary
Modifier and TypeFieldDescriptionprotected final AtomicBoolean
Tracks whether the pool has been closed.protected final boolean
Whether connections without subscriptions should be disposed of.protected final int
The maximum number of outstanding subscriptions a connection may have.Connections that are already at the maximum subscriptions.protected final ConcurrentMap<S,
C> A mapping of which connections hold what subscriptions.protected final ConcurrentMap<C,
Integer> A mapping of connections that are not saturated yet to the number of subscriptions they have. -
Constructor Summary
ModifierConstructorDescriptionprotected
SubscriptionConnectionPool
(SubscriptionConnectionPool.SubscriptionConnectionPoolBuilder<C, S, T, U, ?, ?> b) -
Method Summary
Modifier and TypeMethodDescriptionvoid
close()
protected abstract S
protected abstract int
protected abstract T
handleDuplicateSubscription
(C c, C old, S s) protected abstract T
handleSubscription
(C c, S s) protected abstract U
handleUnsubscription
(C c, T t) int
int
Submits a subscription request.unsubscribe
(T t) Submits an unsubscription request.Methods inherited from class com.github.twitch4j.common.pool.AbstractConnectionPool
createConnection, disposeConnection
-
Field Details
-
disposeUnusedConnections
protected final boolean disposeUnusedConnectionsWhether connections without subscriptions should be disposed of. Default: true.As an optimization, this can be set to false to not dispose of connections in an environment where it is known that a large burst of unsubscriptions will be followed by enough subscriptions such that connections will never need to be disposed of automatically. However, be wary of configuring this as a misstep in your calculations may lead to connections sitting idly while consuming resources.
-
maxSubscriptionsPerConnection
protected final int maxSubscriptionsPerConnectionThe maximum number of outstanding subscriptions a connection may have. Default: 50.50 is a reasonable default given that it is the standard limit imposed on PubSub connections by Twitch and it is the previously-documented rate-limit on the number of possible JOINs in chat in a short burst.
- See Also:
-
saturatedConnections
Connections that are already at the maximum subscriptions. -
unsaturatedConnections
A mapping of connections that are not saturated yet to the number of subscriptions they have. -
subscriptions
A mapping of which connections hold what subscriptions. -
closed
Tracks whether the pool has been closed.
-
-
Constructor Details
-
SubscriptionConnectionPool
protected SubscriptionConnectionPool(SubscriptionConnectionPool.SubscriptionConnectionPoolBuilder<C, S, T, U, ?, ?> b)
-
-
Method Details
-
subscribe
Description copied from interface:TransactionalSubscriber
Submits a subscription request.- Specified by:
subscribe
in interfaceTransactionalSubscriber<C,
S, T> - Parameters:
s
- the subscription request- Returns:
- a response to the request
-
unsubscribe
Description copied from interface:TransactionalSubscriber
Submits an unsubscription request.- Specified by:
unsubscribe
in interfaceTransactionalSubscriber<C,
S, T> - Parameters:
t
- the unsubscription request- Returns:
- a response to the request
-
numConnections
public int numConnections()- Overrides:
numConnections
in classAbstractConnectionPool<C>
- Returns:
- the number of open connections held by this pool.
-
getConnections
- Specified by:
getConnections
in classAbstractConnectionPool<C>
-
close
public void close()- Specified by:
close
in interfaceAutoCloseable
- Overrides:
close
in classAbstractConnectionPool<C>
-
numSubscriptions
public int numSubscriptions()- Returns:
- the total number of subscriptions held by all connections
-
handleSubscription
-
handleDuplicateSubscription
-
handleUnsubscription
-
getRequestFromSubscription
-
getSubscriptionSize
-