fix: implement lazy initialization for global transport registry

- Change global registry from immediate to lazy initialization
- Fix doctest failure caused by debug logging during MultiError import
- Update all functions to use get_transport_registry() instead of direct access
- Resolves CI/CD doctest failure in libp2p.rst
This commit is contained in:
acul71
2025-09-21 19:29:48 -04:00
parent 87429eb2e9
commit 6a1b955a4e

View File

@ -180,18 +180,22 @@ class TransportRegistry:
return None return None
# Global transport registry instance # Global transport registry instance (lazy initialization)
_global_registry = TransportRegistry() _global_registry: TransportRegistry | None = None
def get_transport_registry() -> TransportRegistry: def get_transport_registry() -> TransportRegistry:
"""Get the global transport registry instance.""" """Get the global transport registry instance."""
global _global_registry
if _global_registry is None:
_global_registry = TransportRegistry()
return _global_registry return _global_registry
def register_transport(protocol: str, transport_class: type[ITransport]) -> None: def register_transport(protocol: str, transport_class: type[ITransport]) -> None:
"""Register a transport class in the global registry.""" """Register a transport class in the global registry."""
_global_registry.register_transport(protocol, transport_class) registry = get_transport_registry()
registry.register_transport(protocol, transport_class)
def create_transport_for_multiaddr( def create_transport_for_multiaddr(
@ -219,12 +223,11 @@ def create_transport_for_multiaddr(
is_quic_multiaddr = _get_quic_validation() is_quic_multiaddr = _get_quic_validation()
if is_quic_multiaddr(maddr): if is_quic_multiaddr(maddr):
# Determine QUIC version # Determine QUIC version
registry = get_transport_registry()
if "quic-v1" in protocols: if "quic-v1" in protocols:
return _global_registry.create_transport( return registry.create_transport("quic-v1", upgrader, **kwargs)
"quic-v1", upgrader, **kwargs
)
else: else:
return _global_registry.create_transport("quic", upgrader, **kwargs) return registry.create_transport("quic", upgrader, **kwargs)
elif "ws" in protocols or "wss" in protocols or "tls" in protocols: elif "ws" in protocols or "wss" in protocols or "tls" in protocols:
# For WebSocket, we need a valid structure like: # For WebSocket, we need a valid structure like:
# /ip4/127.0.0.1/tcp/8080/ws (insecure) # /ip4/127.0.0.1/tcp/8080/ws (insecure)
@ -233,15 +236,17 @@ def create_transport_for_multiaddr(
# /ip4/127.0.0.1/tcp/8080/tls/sni/example.com/ws (secure with SNI) # /ip4/127.0.0.1/tcp/8080/tls/sni/example.com/ws (secure with SNI)
if is_valid_websocket_multiaddr(maddr): if is_valid_websocket_multiaddr(maddr):
# Determine if this is a secure WebSocket connection # Determine if this is a secure WebSocket connection
registry = get_transport_registry()
if "wss" in protocols or "tls" in protocols: if "wss" in protocols or "tls" in protocols:
return _global_registry.create_transport("wss", upgrader, **kwargs) return registry.create_transport("wss", upgrader, **kwargs)
else: else:
return _global_registry.create_transport("ws", upgrader, **kwargs) return registry.create_transport("ws", upgrader, **kwargs)
elif "tcp" in protocols: elif "tcp" in protocols:
# For TCP, we need a valid structure like /ip4/127.0.0.1/tcp/8080 # For TCP, we need a valid structure like /ip4/127.0.0.1/tcp/8080
# Check if the multiaddr has proper TCP structure # Check if the multiaddr has proper TCP structure
if _is_valid_tcp_multiaddr(maddr): if _is_valid_tcp_multiaddr(maddr):
return _global_registry.create_transport("tcp", upgrader) registry = get_transport_registry()
return registry.create_transport("tcp", upgrader)
# If no supported transport protocol found or structure is invalid, return None # If no supported transport protocol found or structure is invalid, return None
logger.warning( logger.warning(
@ -258,4 +263,5 @@ def create_transport_for_multiaddr(
def get_supported_transport_protocols() -> list[str]: def get_supported_transport_protocols() -> list[str]:
"""Get list of supported transport protocols from the global registry.""" """Get list of supported transport protocols from the global registry."""
return _global_registry.get_supported_protocols() registry = get_transport_registry()
return registry.get_supported_protocols()