Class TcpResources

java.lang.Object
reactor.netty.tcp.TcpResources
All Implemented Interfaces:
Disposable, ConnectionProvider, LoopResources
Direct Known Subclasses:
HttpResources

public class TcpResources extends Object implements ConnectionProvider, LoopResources
Hold the default Tcp resources.
Since:
0.6
Author:
Stephane Maldini
  • Constructor Details

  • Method Details

    • disposeLoopsAndConnections

      public static void disposeLoopsAndConnections()
      Shutdown the global TcpResources without resetting them, effectively cleaning up associated resources without creating new ones. This method is NOT blocking. It is implemented as fire-and-forget. Use disposeLoopsAndConnectionsLater() when you need to observe the final status of the operation, combined with Mono.block() if you need to synchronously wait for the underlying resources to be disposed.
    • disposeLoopsAndConnectionsLater

      public static Mono<Void> disposeLoopsAndConnectionsLater()
      Prepare to shutdown the global TcpResources without resetting them, effectively cleaning up associated resources without creating new ones. This only occurs when the returned Mono is subscribed to. The quiet period will be 2s and the timeout will be 15s
      Returns:
      a Mono triggering the disposeLoopsAndConnections() when subscribed to.
    • disposeLoopsAndConnectionsLater

      public static Mono<Void> disposeLoopsAndConnectionsLater(Duration quietPeriod, Duration timeout)
      Prepare to shutdown the global TcpResources without resetting them, effectively cleaning up associated resources without creating new ones. This only occurs when the returned Mono is subscribed to. It is guaranteed that the disposal of the underlying LoopResources will not happen before quietPeriod is over. If a task is submitted during the quietPeriod, it is guaranteed to be accepted and the quietPeriod will start over.
      Parameters:
      quietPeriod - the quiet period as described above
      timeout - the maximum amount of time to wait until the disposal of the underlying LoopResources regardless if a task was submitted during the quiet period
      Returns:
      a Mono triggering the disposeLoopsAndConnections() when subscribed to.
      Since:
      0.9.3
    • get

      public static TcpResources get()
      Return the global TCP resources for event loops and pooling.
      Returns:
      the global TCP resources for event loops and pooling
    • reset

      public static TcpResources reset()
      Reset TCP resources to default and return its instance.
      Returns:
      the global TCP resources
    • set

      public static TcpResources set(ConnectionProvider provider)
      Update pooling resources and return the global TCP resources. Note: The previous ConnectionProvider will be disposed.
      Parameters:
      provider - a new ConnectionProvider to replace the current
      Returns:
      the global TCP resources
    • set

      public static TcpResources set(LoopResources loops)
      Update event loops resources and return the global TCP resources. Note: The previous LoopResources will be disposed.
      Parameters:
      loops - a new LoopResources to replace the current
      Returns:
      the global TCP resources
    • acquire

      public Mono<? extends Connection> acquire(TransportConfig config, ConnectionObserver observer, @Nullable Supplier<? extends SocketAddress> remoteAddress, @Nullable AddressResolverGroup<?> resolverGroup)
      Description copied from interface: ConnectionProvider
      Return an existing or new Connection on subscribe.
      Specified by:
      acquire in interface ConnectionProvider
      Parameters:
      config - the transport configuration
      observer - the ConnectionObserver
      remoteAddress - the SocketAddress to connect to
      resolverGroup - the resolver which will resolve the address of the unresolved named address
      Returns:
      an existing or new Mono of Connection
    • daemon

      public boolean daemon()
      Description copied from interface: LoopResources
      return true if EventLoopGroup should not be shutdown.
      Specified by:
      daemon in interface LoopResources
      Returns:
      true if EventLoopGroup should not be shutdown
    • dispose

      public void dispose()
      This has a NOOP implementation by default in order to prevent unintended disposal of the global TCP resources which has a longer lifecycle than regular LoopResources and ConnectionProvider. If a disposal of the global TCP resources is needed, disposeLoopsAndConnections() should be used instead.
      Specified by:
      dispose in interface ConnectionProvider
      Specified by:
      dispose in interface Disposable
      Specified by:
      dispose in interface LoopResources
    • disposeLater

      public Mono<Void> disposeLater()
      This has a NOOP implementation by default in order to prevent unintended disposal of the global TCP resources which has a longer lifecycle than regular LoopResources and ConnectionProvider. If a disposal of the global TCP resources is needed, disposeLoopsAndConnectionsLater() should be used instead.
      Specified by:
      disposeLater in interface ConnectionProvider
      Specified by:
      disposeLater in interface LoopResources
      Returns:
      a Mono representing the completion of the ConnectionProvider disposal.
    • disposeLater

      public Mono<Void> disposeLater(Duration quietPeriod, Duration timeout)
      This has a NOOP implementation by default in order to prevent unintended disposal of the global TCP resources which has a longer lifecycle than regular LoopResources and ConnectionProvider. If a disposal of the global TCP resources is needed, disposeLoopsAndConnectionsLater(Duration, Duration) should be used instead.
      Specified by:
      disposeLater in interface LoopResources
      Parameters:
      quietPeriod - the quiet period as described above
      timeout - the maximum amount of time to wait until the disposal of the underlying LoopResources regardless if a task was submitted during the quiet period
      Returns:
      a Mono representing the completion of the LoopResources disposal.
    • disposeWhen

      public void disposeWhen(SocketAddress remoteAddress)
      Dispose all connection pools for the specified remote address.

      As opposed to dispose(), this method delegates to the underlying connection provider. It has a global effect and removes all connection pools for this remote address from the global TCP resources (making it closer to disposeLoopsAndConnections() than to dispose()).

      Specified by:
      disposeWhen in interface ConnectionProvider
      Parameters:
      remoteAddress - the remote address
    • isDisposed

      public boolean isDisposed()
      Specified by:
      isDisposed in interface Disposable
    • maxConnections

      public int maxConnections()
      Description copied from interface: ConnectionProvider
      Returns the maximum number of connections before starting pending.
      Specified by:
      maxConnections in interface ConnectionProvider
      Returns:
      the maximum number of connections before starting pending
    • maxConnectionsPerHost

      public @Nullable Map<SocketAddress,Integer> maxConnectionsPerHost()
      Description copied from interface: ConnectionProvider
      Returns the maximum number of connections per host before starting pending.
      Specified by:
      maxConnectionsPerHost in interface ConnectionProvider
      Returns:
      the maximum number of connections per host before starting pending
    • mutate

      public @Nullable ConnectionProvider.Builder mutate()
      Description copied from interface: ConnectionProvider
      Returns a builder to mutate properties of this ConnectionProvider.
      Specified by:
      mutate in interface ConnectionProvider
      Returns:
      a builder to mutate properties of this ConnectionProvider
    • name

      public @Nullable String name()
      Description copied from interface: ConnectionProvider
      Returns ConnectionProvider name used for metrics.
      Specified by:
      name in interface ConnectionProvider
      Returns:
      ConnectionProvider name used for metrics
    • onChannel

      public <CHANNEL extends Channel> CHANNEL onChannel(Class<CHANNEL> channelType, EventLoopGroup group)
      Description copied from interface: LoopResources
      Callback for a Channel selection.
      Specified by:
      onChannel in interface LoopResources
      Type Parameters:
      CHANNEL - the Channel implementation
      Parameters:
      channelType - the channel type
      group - the source EventLoopGroup to assign a loop from
      Returns:
      a Channel instance
    • onChannelClass

      public <CHANNEL extends Channel> Class<? extends CHANNEL> onChannelClass(Class<CHANNEL> channelType, EventLoopGroup group)
      Description copied from interface: LoopResources
      Callback for a Channel class selection.
      Specified by:
      onChannelClass in interface LoopResources
      Type Parameters:
      CHANNEL - the Channel implementation
      Parameters:
      channelType - the channel type
      group - the source EventLoopGroup to assign a loop from
      Returns:
      a Channel class
    • onClient

      public EventLoopGroup onClient(boolean useNative)
      Description copied from interface: LoopResources
      Callback for client EventLoopGroup creation.
      Specified by:
      onClient in interface LoopResources
      Parameters:
      useNative - should use native group if current environment supports it
      Returns:
      a new EventLoopGroup
    • onServer

      public EventLoopGroup onServer(boolean useNative)
      Description copied from interface: LoopResources
      Callback for server EventLoopGroup creation, this is the EventLoopGroup for the child channel.
      Specified by:
      onServer in interface LoopResources
      Parameters:
      useNative - should use native group if current environment supports it
      Returns:
      a new EventLoopGroup
    • onServerSelect

      public EventLoopGroup onServerSelect(boolean useNative)
      Description copied from interface: LoopResources
      Callback for server select EventLoopGroup creation, this is the EventLoopGroup for the acceptor channel.
      Specified by:
      onServerSelect in interface LoopResources
      Parameters:
      useNative - should use native group if current environment supports it
      Returns:
      a new EventLoopGroup
    • _dispose

      protected void _dispose()
      Dispose underlying resources.
    • _disposeLater

      protected Mono<Void> _disposeLater(Duration quietPeriod, Duration timeout)
      Dispose underlying resources in a listenable fashion. It is guaranteed that the disposal of the underlying LoopResources will not happen before quietPeriod is over. If a task is submitted during the quietPeriod, it is guaranteed to be accepted and the quietPeriod will start over.
      Parameters:
      quietPeriod - the quiet period as described above
      timeout - the maximum amount of time to wait until the disposal of the underlying LoopResources regardless if a task was submitted during the quiet period
      Returns:
      the Mono that represents the end of disposal
    • getOrCreateDefaultResolver

      protected AddressResolverGroup<?> getOrCreateDefaultResolver()
      Safely checks whether a name resolver exists and proceed with a creation if it does not exist. The name resolver uses as an event loop group the LoopResources that are configured. Guarantees that always one and the same instance is returned for a given LoopResources and if the LoopResources is updated the name resolver is also updated.
      Returns:
      an existing or new AddressResolverGroup
    • getOrCreate

      protected static <T extends TcpResources> T getOrCreate(AtomicReference<@Nullable T> ref, @Nullable LoopResources loops, @Nullable ConnectionProvider provider, BiFunction<LoopResources,ConnectionProvider,T> onNew, String name)
      Safely check if existing resource exist and proceed to update/cleanup if new resources references are passed.
      Type Parameters:
      T - the reified type of TcpResources
      Parameters:
      ref - the resources atomic reference
      loops - the eventual new LoopResources
      provider - the eventual new ConnectionProvider
      onNew - a TcpResources factory
      name - a name for resources
      Returns:
      an existing or new TcpResources