Skip to content

Commit c1518e0

Browse files
committed
Enhance connection management with server alive check and factor in heartbeat calculation
1 parent 842a8d7 commit c1518e0

File tree

3 files changed

+15
-4
lines changed

3 files changed

+15
-4
lines changed

packages/stompman/stompman/client.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ def __post_init__(self) -> None:
7979
read_timeout=self.read_timeout,
8080
read_max_chunk_size=self.read_max_chunk_size,
8181
write_retry_attempts=self.write_retry_attempts,
82+
check_server_alive_interval_factor=self.check_server_alive_interval_factor,
8283
ssl=self.ssl,
8384
)
8485
self._on_heartbeat_is_async = inspect.iscoroutinefunction(self.on_heartbeat) if self.on_heartbeat else False
@@ -130,7 +131,7 @@ async def _monitor_server_aliveness(self, server_heartbeat_interval_ms: int) ->
130131
if not last_read_time_ms:
131132
print("No last_Read_time_ms")
132133
continue
133-
if (t:=time.time() - last_read_time_ms) > server_heartbeat_interval_seconds:
134+
if (t := time.time() - last_read_time_ms) > server_heartbeat_interval_seconds:
134135
print("reset time", t)
135136
self._connection_manager._active_connection_state = None
136137
else:

packages/stompman/stompman/connection_lifespan.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import asyncio
2+
from collections.abc import Callable
23
from contextlib import suppress
34
from dataclasses import dataclass
4-
from typing import Any, Callable, Protocol
5+
from typing import Any, Protocol
56
from uuid import uuid4
67

78
from stompman.config import ConnectionParameters, Heartbeat
@@ -26,8 +27,6 @@ async def enter(self) -> StompProtocolConnectionIssue | None: ...
2627
async def exit(self) -> None: ...
2728

2829

29-
30-
3130
@dataclass(kw_only=True, slots=True)
3231
class ConnectionLifespan(AbstractConnectionLifespan):
3332
connection: AbstractConnection

packages/stompman/stompman/connection_manager.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import asyncio
2+
import time
23
from collections.abc import AsyncGenerator
34
from dataclasses import dataclass, field
45
from ssl import SSLContext
@@ -39,6 +40,7 @@ class ConnectionManager:
3940
read_timeout: int
4041
read_max_chunk_size: int
4142
write_retry_attempts: int
43+
check_server_alive_interval_factor: int
4244

4345
_active_connection_state: ActiveConnectionState | None = field(default=None, init=False)
4446
_reconnect_lock: asyncio.Lock = field(default_factory=asyncio.Lock)
@@ -118,6 +120,15 @@ async def _get_active_connection_state(self) -> ActiveConnectionState:
118120
def _clear_active_connection_state(self) -> None:
119121
self._active_connection_state = None
120122

123+
def is_alive(self) -> bool:
124+
if not self._connection_manager._active_connection_state:
125+
return False
126+
if not (last_read_time_ms := self._connection_manager._active_connection_state.connection.last_read_time_ms):
127+
return True
128+
if (time.time() - last_read_time_ms) > server_heartbeat_interval_seconds:
129+
...
130+
return None
131+
121132
async def write_heartbeat_reconnecting(self) -> None:
122133
for _ in range(self.write_retry_attempts):
123134
connection_state = await self._get_active_connection_state()

0 commit comments

Comments
 (0)