Class OutboundConnectionCacheImpl<C extends Connection>

All Implemented Interfaces:
ConnectionCache<C>, OutboundConnectionCache<C>

public final class OutboundConnectionCacheImpl<C extends Connection> extends ConnectionCacheNonBlockingBase<C> implements OutboundConnectionCache<C>
Manage connections that are initiated from this VM. Connections are managed by a get/release mechanism and cached by the ContactInfo used to create them. For efficiency, multiple connections (referred to as parallel connections) may be created using the same ContactInfo. Connections are reclaimed when they are no longer in use and there are too many connections open.

A connection is obtained through the get method, and released back to the cache through the release method. Note that a connection that is released may still be expecting a response, in which case the connection is NOT eligible for reclamation. If a connection is released to the cache while expecting a response, the connection must me made available for reclamation by calling responseReceived. XXX Should a get/release cycle expect at most one response? Should it support more than one response? Are there cases where we don't know in advance how many responses are expected?

A connection basically represents some sort of communication channel, but few requirements are placed on the connection. Basically the ability to close a connection is required in order for reclamation to work.

Also we need the ContactInfo as a factory for the Connection.

  • Field Details

  • Constructor Details

    • OutboundConnectionCacheImpl

      public OutboundConnectionCacheImpl(String cacheType, int highWaterMark, int numberToReclaim, int maxParallelConnections, long ttl)
  • Method Details

    • maxParallelConnections

      public int maxParallelConnections()
      Description copied from interface: OutboundConnectionCache
      Configured maximum number of connections supported per ContactInfo.
      Specified by:
      maxParallelConnections in interface OutboundConnectionCache<C extends Connection>
      Returns:
      maximum number of connections
    • thisClassName

      protected String thisClassName()
      Specified by:
      thisClassName in class ConnectionCacheBase<C extends Connection>
    • get

      public C get(ContactInfo<C> cinfo, ConnectionFinder<C> finder) throws IOException
      Description copied from interface: OutboundConnectionCache
      Return a Connection corresponding to the given ContactInfo. This works as follows:
      • Call the finder. If it returns non-null, use that connection; (Note that this may be a new connection, created in the finder). The finder SHOULD NOT create a new connection if canCreateNewConnection returns false, but this is advisory.
      • otherwise, Use an idle connection, if one is available;
      • otherwise, create a new connection, if not too many connections are open;
      • otherwise, use a busy connection.
      Note that creating a new connection requires EITHER:
      • there is no existing connection for the ContactInfo
      • OR the total number of connections in the cache is less than the HighWaterMark and the number of connections for this ContactInfo is less than MaxParallelConnections.
      We will always return a Connection for a get call UNLESS we have no existing connection and an attempt to create a new connection fails. In this case, the IOException thrown by ContactInfo.create is propagated out of this method.

      It is possible that the cache contains connections that no longer connect to their destination. In this case, it is the responsibility of the client of the cache to close the broken connection as they are detected. Connection reclamation may also handle the cleanup, but note that a broken connection with pending responses will never be reclaimed.

      Note that the idle and busy connection collections that are passed to the finder are unmodifiable collections. They have iterators that return connections in LRU order, with the least recently used connection first. This is done to aid a finder that wishes to consider load balancing in its determination of an appropriate connection.

      Specified by:
      get in interface OutboundConnectionCache<C extends Connection>
      Parameters:
      cinfo - info on Connection to get
      finder - Finder to use to search for Connection
      Returns:
      Connection corresponding to the ContactInfo
      Throws:
      IOException - if an error occurred
    • get

      public C get(ContactInfo<C> cinfo) throws IOException
      Description copied from interface: OutboundConnectionCache
      Behaves the same as OutboundConnectionCache.get(ContactInfo, ConnectionFinder) except that no connection finder is provided, so that step is ignored.
      Specified by:
      get in interface OutboundConnectionCache<C extends Connection>
      Parameters:
      cinfo - info on Connection to get
      Returns:
      Connection corresponding to the ContactInfo
      Throws:
      IOException - if an error occurred
    • release

      public void release(C conn, int numResponsesExpected)
      Description copied from interface: OutboundConnectionCache
      Release a Connection previously obtained from get. Connections that have been released as many times as they have been returned by get are idle; otherwise a Connection is busy. Some number of responses (usually 0 or 1) may be expected ON THE SAME CONNECTION even for an idle connection. We maintain a count of the number of outstanding responses we expect for protocols that return the response on the same connection on which the request was received. This is necessary to prevent reclamation of a Connection that is idle, but still needed to send responses to old requests.
      Specified by:
      release in interface OutboundConnectionCache<C extends Connection>
      Parameters:
      conn - connection to release
      numResponsesExpected - number of responses expected
    • responseReceived

      public void responseReceived(C conn)
      Decrement the number of expected responses. When a connection is idle and has no expected responses, it can be reclaimed.
      Specified by:
      responseReceived in interface OutboundConnectionCache<C extends Connection>
      Parameters:
      conn - Connection that has received a response
    • close

      public void close(C conn)
      Close a connection, regardless of whether the connection is busy or not.
      Specified by:
      close in interface ConnectionCache<C extends Connection>
      Parameters:
      conn - connection to close
    • getEntry

    • canCreateNewConnection

      private boolean canCreateNewConnection(OutboundConnectionCacheImpl.CacheEntry<C> entry)
    • canCreateNewConnection

      public boolean canCreateNewConnection(ContactInfo<C> cinfo)
      Description copied from interface: OutboundConnectionCache
      Determine whether a new connection could be created by the ConnectionCache or not.
      Specified by:
      canCreateNewConnection in interface OutboundConnectionCache<C extends Connection>
      Parameters:
      cinfo - info to use for Connection
      Returns:
      if a new connection can be created