Class NettyPythonEventLoop

java.lang.Object
io.micronaut.context.python.netty.NettyPythonEventLoop
All Implemented Interfaces:
PythonEventLoop

@Internal public final class NettyPythonEventLoop extends Object implements PythonEventLoop
Python event loop backed by a Netty EventLoop.

This class is intentionally a narrow bridge between Python's asyncio transport protocol APIs and Micronaut's Netty event loop. It does not try to expose raw Netty channels to Python callers. The public nested transport/server/facade types are GraalPy host objects whose method names are part of the Python-facing surface, including snake_case names that mirror asyncio. Do not rename those methods to satisfy Java style checks unless the Python shims and tests are updated at the same time.

All callbacks into Python protocols are made from the Netty event loop thread. When this loop is entered from a different Java thread, work is rescheduled onto Netty before touching channel state or invoking Python callbacks.

  • Constructor Details

    • NettyPythonEventLoop

      public NettyPythonEventLoop(io.netty.channel.EventLoop eventLoop)
      Parameters:
      eventLoop - The Netty event loop.
  • Method Details

    • inEventLoop

      public boolean inEventLoop()
      Description copied from interface: PythonEventLoop
      Used to decide whether Python work can run immediately or must be queued.
      Specified by:
      inEventLoop in interface PythonEventLoop
      Returns:
      Whether the current thread is this event-loop thread.
    • execute

      public void execute(Runnable runnable)
      Description copied from interface: PythonEventLoop
      Queue a callback for serialized execution on the loop.
      Specified by:
      execute in interface PythonEventLoop
      Parameters:
      runnable - The callback.
    • schedule

      public ScheduledFuture<?> schedule(Runnable runnable, long delay, TimeUnit unit)
      Description copied from interface: PythonEventLoop
      Queue a callback after the requested delay using loop time.
      Specified by:
      schedule in interface PythonEventLoop
      Parameters:
      runnable - The callback.
      delay - The delay.
      unit - The delay unit.
      Returns:
      The scheduled future.
    • time

      public double time()
      Description copied from interface: PythonEventLoop
      Returns the monotonic time base exposed to Python's asyncio APIs.
      Specified by:
      time in interface PythonEventLoop
      Returns:
      Event-loop time in seconds.
    • equals

      public boolean equals(Object object)
      Overrides:
      equals in class Object
    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class Object
    • eventLoop

      public io.netty.channel.EventLoop eventLoop()
      Returns the underlying Netty event loop.
      Returns:
      The underlying Netty event loop.
    • createConnection

      public CompletionStage<Object[]> createConnection(org.graalvm.polyglot.Value protocolFactory, String host, int port, @Nullable String localHost, int localPort, @Nullable Object ssl, @Nullable String serverHostname, @Nullable Double sslHandshakeTimeout, @Nullable Double sslShutdownTimeout)
      Create a Netty-backed asyncio TCP connection.
      Parameters:
      protocolFactory - The Python protocol factory.
      host - The remote host.
      port - The remote port.
      localHost - The local host, or null.
      localPort - The local port, or -1.
      ssl - TLS options.
      serverHostname - The TLS server hostname.
      sslHandshakeTimeout - The TLS handshake timeout in seconds.
      sslShutdownTimeout - The TLS shutdown timeout in seconds.
      Returns:
      A stage completing with transport and protocol.
    • createServer

      public CompletionStage<NettyPythonEventLoop.NettyServer> createServer(org.graalvm.polyglot.Value protocolFactory, @Nullable String host, int port, int backlog, boolean reuseAddress, boolean reusePort, boolean startServing, @Nullable Object ssl, @Nullable Double sslHandshakeTimeout, @Nullable Double sslShutdownTimeout)
      Create a Netty-backed asyncio TCP server.
      Parameters:
      protocolFactory - The Python protocol factory.
      host - The local host.
      port - The local port.
      backlog - The listen backlog.
      reuseAddress - Whether address reuse is enabled.
      reusePort - Whether port reuse is requested.
      startServing - Whether to start accepting connections immediately.
      ssl - TLS options.
      sslHandshakeTimeout - The TLS handshake timeout in seconds.
      sslShutdownTimeout - The TLS shutdown timeout in seconds.
      Returns:
      A stage completing with the server.
    • connectAcceptedSocket

      public CompletionStage<Object[]> connectAcceptedSocket(org.graalvm.polyglot.Value protocolFactory, Object socket, @Nullable Object ssl, @Nullable Double sslHandshakeTimeout, @Nullable Double sslShutdownTimeout)
      Wrap a Netty channel accepted elsewhere as an asyncio TCP transport.
      Parameters:
      protocolFactory - The Python protocol factory.
      socket - The accepted Netty channel or transport exposing one.
      ssl - TLS options.
      sslHandshakeTimeout - The TLS handshake timeout in seconds.
      sslShutdownTimeout - The TLS shutdown timeout in seconds.
      Returns:
      A stage completing with transport and protocol.
    • createUnixConnection

      public CompletionStage<Object[]> createUnixConnection(org.graalvm.polyglot.Value protocolFactory, String path, @Nullable Object ssl, @Nullable String serverHostname, @Nullable Double sslHandshakeTimeout, @Nullable Double sslShutdownTimeout)
      Create a Netty-backed asyncio Unix-domain socket connection.
      Parameters:
      protocolFactory - The Python protocol factory.
      path - The Unix-domain socket path.
      ssl - TLS options.
      serverHostname - The TLS server hostname.
      sslHandshakeTimeout - The TLS handshake timeout in seconds.
      sslShutdownTimeout - The TLS shutdown timeout in seconds.
      Returns:
      A stage completing with transport and protocol.
    • createUnixServer

      public CompletionStage<NettyPythonEventLoop.NettyServer> createUnixServer(org.graalvm.polyglot.Value protocolFactory, String path, int backlog, boolean startServing, @Nullable Object ssl, @Nullable Double sslHandshakeTimeout, @Nullable Double sslShutdownTimeout)
      Create a Netty-backed asyncio Unix-domain socket server.
      Parameters:
      protocolFactory - The Python protocol factory.
      path - The Unix-domain socket path.
      backlog - The listen backlog.
      startServing - Whether to start accepting connections immediately.
      ssl - TLS options.
      sslHandshakeTimeout - The TLS handshake timeout in seconds.
      sslShutdownTimeout - The TLS shutdown timeout in seconds.
      Returns:
      A stage completing with the server.
    • createDatagramEndpoint

      public CompletionStage<Object[]> createDatagramEndpoint(org.graalvm.polyglot.Value protocolFactory, @Nullable String localHost, int localPort, @Nullable String remoteHost, int remotePort, boolean allowBroadcast, boolean reusePort)
      Create a Netty-backed asyncio datagram endpoint.
      Parameters:
      protocolFactory - The Python protocol factory.
      localHost - The local host, or null.
      localPort - The local port, or -1.
      remoteHost - The remote host, or null.
      remotePort - The remote port, or -1.
      allowBroadcast - Whether broadcast is enabled.
      reusePort - Whether port reuse is requested.
      Returns:
      A stage completing with transport and protocol.