6
6
from types import TracebackType
7
7
from typing import TYPE_CHECKING , Literal , Self
8
8
9
- from stompman .config import ConnectionParameters
9
+ from stompman .config import ConnectionParameters , Heartbeat
10
10
from stompman .connection import AbstractConnection
11
+ from stompman .connection_lifespan import EstablishedConnectionResult
11
12
from stompman .errors import (
12
13
AllServersUnavailable ,
13
14
AnyConnectionIssue ,
26
27
class ActiveConnectionState :
27
28
connection : AbstractConnection
28
29
lifespan : "AbstractConnectionLifespan"
30
+ server_heartbeat : Heartbeat
29
31
30
32
31
33
@dataclass (kw_only = True , slots = True )
@@ -60,7 +62,9 @@ async def __aexit__(
60
62
return
61
63
await self ._active_connection_state .connection .close ()
62
64
63
- async def _create_connection_to_one_server (self , server : ConnectionParameters ) -> ActiveConnectionState | None :
65
+ async def _create_connection_to_one_server (
66
+ self , server : ConnectionParameters
67
+ ) -> tuple [AbstractConnection , ConnectionParameters ] | None :
64
68
if connection := await self .connection_class .connect (
65
69
host = server .host ,
66
70
port = server .port ,
@@ -69,31 +73,35 @@ async def _create_connection_to_one_server(self, server: ConnectionParameters) -
69
73
read_timeout = self .read_timeout ,
70
74
ssl = self .ssl ,
71
75
):
72
- return ActiveConnectionState (
73
- connection = connection ,
74
- lifespan = self .lifespan_factory (connection = connection , connection_parameters = server ),
75
- )
76
+ return (connection , server )
76
77
return None
77
78
78
- async def _create_connection_to_any_server (self ) -> ActiveConnectionState | None :
79
+ async def _create_connection_to_any_server (self ) -> tuple [ AbstractConnection , ConnectionParameters ] | None :
79
80
for maybe_connection_future in asyncio .as_completed (
80
81
[self ._create_connection_to_one_server (server ) for server in self .servers ]
81
82
):
82
- if connection_state := await maybe_connection_future :
83
- return connection_state
83
+ if connection_and_server := await maybe_connection_future :
84
+ return connection_and_server
84
85
return None
85
86
86
87
async def _connect_to_any_server (self ) -> ActiveConnectionState | AnyConnectionIssue :
87
- if not (active_connection_state := await self ._create_connection_to_any_server ()):
88
+ if not (connection_and_server := await self ._create_connection_to_any_server ()):
88
89
return AllServersUnavailable (servers = self .servers , timeout = self .connect_timeout )
90
+ connection , connection_parameters = connection_and_server
91
+ lifespan = self .lifespan_factory (connection = connection , connection_parameters = connection_parameters )
89
92
90
93
try :
91
- if connection_issue := await active_connection_state .lifespan .enter ():
92
- return connection_issue
94
+ connection_result = await lifespan .enter ()
93
95
except ConnectionLostError :
94
96
return ConnectionLost ()
95
97
96
- return active_connection_state
98
+ return (
99
+ ActiveConnectionState (
100
+ connection = connection , lifespan = lifespan , server_heartbeat = connection_result .server_heartbeat
101
+ )
102
+ if isinstance (connection_result , EstablishedConnectionResult )
103
+ else connection_result
104
+ )
97
105
98
106
async def _get_active_connection_state (self ) -> ActiveConnectionState :
99
107
if self ._active_connection_state :
0 commit comments