Manages a network connection.
| 41 | |
| 42 | |
| 43 | class Connection(Base): |
| 44 | """Manages a network connection.""" |
| 45 | |
| 46 | def __init__(self, host: str, port: int = 8080): |
| 47 | """Initialize connection with host and port.""" |
| 48 | super().__init__(host) |
| 49 | self.__port = port |
| 50 | self._connected = False |
| 51 | |
| 52 | @retry |
| 53 | async def connect(self) -> bool: |
| 54 | """Establish the connection asynchronously.""" |
| 55 | log(f"Connecting to {self._name}:{self.__port}") |
| 56 | self._connected = True |
| 57 | return True |
| 58 | |
| 59 | def disconnect(self) -> None: |
| 60 | self._connected = False |
| 61 | |
| 62 | @property |
| 63 | def is_connected(self) -> bool: |
| 64 | return self._connected |
| 65 | |
| 66 | class Config: |
| 67 | """Nested configuration class.""" |
| 68 | def __init__(self, timeout: int = DEFAULT_TIMEOUT): |
| 69 | self.timeout = timeout |
| 70 | |
| 71 | def validate(self) -> bool: |
| 72 | return self.timeout > 0 |
| 73 | |
| 74 | |
| 75 | class Pool(Connection): |