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>, 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:
    • saturatedConnections

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

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

      protected final ConcurrentMap<S,C> subscriptions
      A mapping of which connections hold what subscriptions.
    • closed

      protected final AtomicBoolean closed
      Tracks whether the pool has been closed.
  • 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 Iterable<C> getConnections()
      Specified by:
      getConnections in class AbstractConnectionPool<C>
    • close

      public void close()
      Specified by:
      close in interface AutoCloseable
      Overrides:
      close 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)