Index
pyvider.rpcplugin.transport
¶
Transport Layer for Pyvider RPC Plugin¶
This package provides the network transport abstractions for communication between plugin clients and servers. It handles the low-level socket operations, connection management, and protocol negotiation.
Key components: - RPCPluginTransport: Base interface for all transport implementations - TCPSocketTransport: TCP socket-based transport implementation - UnixSocketTransport: Unix domain socket-based transport implementation
The transport layer is responsible for: 1. Listening for connections (server-side) 2. Connecting to endpoints (client-side) 3. Managing connection lifecycle and cleanup 4. Ensuring Go-Python interoperability
Classes¶
RPCPluginTransport
¶
Bases: ABC
Abstract base class defining the interface for all transport implementations.
This class defines the contract that concrete transport implementations must fulfill to provide network communication for plugins. The interface supports both client-side (connect) and server-side (listen) operations.
Implementations must handle: - Connection setup and teardown - Socket lifecycle management - Error handling and reporting - Resource cleanup
Custom transports can be implemented by subclassing this class and implementing the required abstract methods.
Functions¶
close
abstractmethod
async
¶
Close the transport and release any associated resources.
Implementations should ensure that all network resources (like sockets) are properly closed and cleaned up. This method should be idempotent.
Raises:
| Type | Description |
|---|---|
TransportError
|
If an error occurs during closing. |
Source code in pyvider/rpcplugin/transport/base.py
connect
abstractmethod
async
¶
Connect to a remote endpoint.
Implementations should establish a connection to the specified endpoint address. This is typically used by client components.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
endpoint
|
str
|
The target endpoint address string. |
required |
Raises:
| Type | Description |
|---|---|
TransportError
|
If the connection cannot be established. |
Source code in pyvider/rpcplugin/transport/base.py
listen
abstractmethod
async
¶
Start listening for connections.
Implementations should bind to an appropriate socket or address and begin accepting connections. This is typically used by server components.
Returns:
| Type | Description |
|---|---|
str
|
The endpoint address as a string (e.g., "127.0.0.1:50051" or |
str
|
"/tmp/socket.sock") |
Raises:
| Type | Description |
|---|---|
TransportError
|
If binding or listening fails |
Source code in pyvider/rpcplugin/transport/base.py
TCPSocketTransport
¶
Bases: RPCPluginTransport
TCP socket transport for network-based RPC communication.
This transport implementation provides TCP/IP connectivity for RPC plugins, supporting both server (listen) and client (connect) modes. It's suitable for network communication between processes on the same machine or across a network.
The transport handles: - Dynamic port allocation (when port=0) - DNS resolution for hostnames - Connection lifecycle management - Graceful shutdown with timeouts
Attributes:
| Name | Type | Description |
|---|---|---|
host |
str
|
IP address or hostname to bind/connect to (default: "127.0.0.1") |
port |
int
|
Port number to use (0 for dynamic allocation, default: 0) |
endpoint |
str | None
|
The resolved endpoint string in "host:port" format (set after listen/connect) |
Example
# Server mode with dynamic port
transport = TCPSocketTransport()
endpoint = await transport.listen() # Returns "127.0.0.1:45678"
# Server mode with specific port
transport = TCPSocketTransport(host="0.0.0.0", port=8080)
endpoint = await transport.listen() # Returns "0.0.0.0:8080"
# Client mode
transport = TCPSocketTransport()
await transport.connect("remote.host:8080")
# Cleanup
await transport.close()
Note
This transport is typically created automatically by the factory functions (plugin_server, plugin_client) rather than instantiated directly. For local IPC on Unix-like systems, prefer UnixSocketTransport for better performance and security.
Functions¶
__attrs_post_init__
¶
Initializes locks and events for managing transport state.
Source code in pyvider/rpcplugin/transport/tcp.py
close
async
¶
Closes the TCP transport, including any active server or client connections.
This method is idempotent and ensures that all resources associated with this transport instance are released.
Source code in pyvider/rpcplugin/transport/tcp.py
connect
async
¶
Connects to a remote TCP endpoint.
The endpoint string must be in the format 'host:port'. This method parses the endpoint, performs DNS resolution, and establishes a connection.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
endpoint
|
str
|
The target TCP endpoint string (e.g., "127.0.0.1:12345"). |
required |
Raises:
| Type | Description |
|---|---|
TransportError
|
If the endpoint format is invalid, DNS resolution fails, or the connection cannot be established (e.g., timeout, refused). |
Source code in pyvider/rpcplugin/transport/tcp.py
listen
async
¶
endpoint (host:port).
Source code in pyvider/rpcplugin/transport/tcp.py
UnixSocketTransport
¶
Bases: RPCPluginTransport
Unix domain socket transport for local IPC communication.
This transport provides high-performance local inter-process communication using Unix domain sockets. It's the preferred transport for plugin communication on Linux and macOS systems, offering better security and performance than TCP for local connections.
The implementation is compatible with HashiCorp's go-plugin protocol, handling: - Automatic socket path generation when path is None - Path normalization for unix:, unix:/, unix:// prefixes - Proper file permissions (0660) for cross-process access - Socket lifecycle management with cleanup on close - Stale socket detection and removal
Attributes:
| Name | Type | Description |
|---|---|---|
path |
str | None
|
Unix socket file path. If None, generates temporary path. |
endpoint |
str | None
|
The normalized endpoint string (e.g., "unix:/tmp/plugin.sock") |
Example
# Server with auto-generated path
transport = UnixSocketTransport()
endpoint = await transport.listen() # Returns "unix:/tmp/pyvider-xxx.sock"
# Server with specific path
transport = UnixSocketTransport(path="/var/run/myplugin.sock")
endpoint = await transport.listen() # Returns "unix:/var/run/myplugin.sock"
# Client connection
transport = UnixSocketTransport()
await transport.connect("unix:/tmp/server.sock")
# Cleanup (removes socket file)
await transport.close()
Platform Notes
- Linux/macOS: Full support with optimal performance
- Windows: Not supported (use TCPSocketTransport instead)
- Docker: Ensure socket paths are in shared volumes for cross-container IPC
Security Notes
- Socket files are created with 0660 permissions (user/group read/write)
- Consider socket file location for security (avoid world-writable directories)
- Socket files are automatically removed on close()
Note
This transport is typically created automatically by factory functions (plugin_server, plugin_client) when transport="unix" is specified.
Functions¶
__attrs_post_init__
¶
Post-initialization hook for UnixSocketTransport.
If a socket path is not provided, it generates an ephemeral path. Otherwise, it normalizes the provided path. Initializes locks and events.
Source code in pyvider/rpcplugin/transport/unix/transport.py
close
async
¶
Closes the Unix socket transport.
This involves closing any active client connections, stopping the server, and removing the socket file from the filesystem. It is designed to be idempotent.
Source code in pyvider/rpcplugin/transport/unix/transport.py
connect
async
¶
Connect to a remote Unix socket with robust path handling.
This method: 1. Normalizes the endpoint path to handle various formats 2. Verifies the socket file exists (with retries) 3. Establishes the connection with timeout handling
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
endpoint
|
str
|
The Unix socket path to connect to, which can be in various formats: - Absolute path: "/tmp/socket.sock" - With prefix: "unix:/tmp/socket.sock" |
required |
Raises:
| Type | Description |
|---|---|
TransportError
|
If the socket file doesn't exist or connection fails |
TimeoutError
|
If the connection attempt times out |
Source code in pyvider/rpcplugin/transport/unix/transport.py
listen
async
¶
Start listening on Unix socket with cross-platform compatibility.