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 type
S - the subscription request
T - transactional subscription response and potential unsubscription request
U - the unsubscription response
All Implemented Interfaces:
TransactionalSubscriber<S,​T,​U>, java.lang.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).
  • Field Details

    • disposeUnusedConnections

      protected final boolean disposeUnusedConnections
      Whether 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 maxSubscriptionsPerConnection
      The 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:
      PubSub Limits, Previous Chat Limits
    • saturatedConnections

      protected final java.util.Set<C> saturatedConnections
      Connections that are already at the maximum subscriptions.
    • unsaturatedConnections

      protected final java.util.concurrent.ConcurrentMap<C,​java.lang.Integer> unsaturatedConnections
      A mapping of connections that are not saturated yet to the number of subscriptions they have.
    • subscriptions

      protected final java.util.concurrent.ConcurrentMap<S,​C> subscriptions
      A mapping of which connections hold what subscriptions.
  • Constructor Details

  • Method Details

    • subscribe

      public T subscribe​(S s)
      Description copied from interface: TransactionalSubscriber
      Submits a subscription request.
      Specified by:
      subscribe in interface TransactionalSubscriber<C,​S,​T>
      Parameters:
      s - the subscription request
      Returns:
      a response to the request
    • unsubscribe

      public U unsubscribe​(T t)
      Description copied from interface: TransactionalSubscriber
      Submits an unsubscription request.
      Specified by:
      unsubscribe in interface TransactionalSubscriber<C,​S,​T>
      Parameters:
      t - the unsubscription request
      Returns:
      a response to the request
    • numConnections

      public int numConnections()
      Overrides:
      numConnections in class AbstractConnectionPool<C>
      Returns:
      the number of open connections held by this pool.
    • getConnections

      protected java.lang.Iterable<C> getConnections()
      Specified by:
      getConnections in class AbstractConnectionPool<C>
    • numSubscriptions

      public int numSubscriptions()
      Returns:
      the total number of subscriptions held by all connections
    • handleSubscription

      protected abstract T handleSubscription​(C c, S s)
    • handleDuplicateSubscription

      protected abstract T handleDuplicateSubscription​(C c, C old, S s)
    • handleUnsubscription

      protected abstract U handleUnsubscription​(C c, T t)
    • getRequestFromSubscription

      protected abstract S getRequestFromSubscription​(T t)
    • getSubscriptionSize

      protected abstract int getSubscriptionSize​(S s)