Class TransportFactory

java.lang.Object
express.mvp.myra.transport.TransportFactory

public final class TransportFactory extends Object
Factory for creating transport instances and related components.

This factory abstracts the construction of transports, backends, and buffer pools, handling backend selection and fallback logic automatically.

Backend Selection

The factory attempts to create the requested backend type. If unavailable, it falls back gracefully:

  • IO_URING: Preferred on Linux 5.1+. Falls back to NIO if liburing is not available.
  • NIO: Always available as the baseline implementation.
  • XDP/DPDK: Experimental, not yet implemented.

Usage Example

TransportConfig config = TransportConfig.builder()
    .backendType(BackendType.IO_URING)
    .registeredBuffers(RegisteredBuffersConfig.builder()
        .numBuffers(256)
        .bufferSize(65536)
        .build())
    .build();

InetSocketAddress remote = new InetSocketAddress("example.com", 8080);
try (Transport transport = TransportFactory.create(config, remote)) {
    transport.start(handler);
    // Use transport...
}

Thread Safety

This factory is thread-safe. Multiple threads can create transports concurrently.

See Also:
  • Method Details

    • create

      public static Transport create(TransportConfig config, SocketAddress remoteAddress)
      Creates a new transport with the given configuration.

      This method creates and initializes all required components:

      1. Creates a buffer pool with the configured size
      2. Creates the I/O backend (io_uring or NIO)
      3. Initializes the backend with configuration
      4. Registers the buffer pool with the backend
      5. Creates the TCP transport wrapper
      Parameters:
      config - the transport configuration
      remoteAddress - the remote endpoint to connect to
      Returns:
      a new transport instance ready for use
      Throws:
      TransportException - if initialization fails
      IllegalArgumentException - if registered buffers are disabled in config
    • createPool

      public static ConnectionPool createPool(TransportConfig config)
      Creates a connection pool for managing multiple connections.
      Parameters:
      config - the transport configuration
      Returns:
      a new connection pool instance
    • createBufferPool

      public static RegisteredBufferPool createBufferPool(TransportConfig config)
      Creates a registered buffer pool with the configured parameters.

      The buffer pool allocates off-heap memory and prepares it for registration with the I/O backend.

      Parameters:
      config - the transport configuration containing buffer settings
      Returns:
      a new buffer pool
      Throws:
      IllegalArgumentException - if registered buffers are disabled
    • createBackend

      public static TransportBackend createBackend(TransportConfig config)
      Creates an I/O backend based on the configuration.

      This method handles backend selection with automatic fallback:

      • IO_URING: Attempts to load io_uring backend. If liburing is not installed or linked, falls back to NIO with a warning.
      • NIO: Uses standard Java NIO (always available).
      • XDP/DPDK: Throws UnsupportedOperationException (not yet implemented).
      Parameters:
      config - the transport configuration
      Returns:
      a new backend instance (not yet initialized)
      Throws:
      UnsupportedOperationException - if XDP or DPDK is requested
      IllegalArgumentException - if backend type is unknown