File tree Expand file tree Collapse file tree 3 files changed +46
-2
lines changed
rabbitmq_amqp_python_client Expand file tree Collapse file tree 3 files changed +46
-2
lines changed Original file line number Diff line number Diff line change @@ -37,6 +37,11 @@ def __init__(
37
37
self ._on_disconnection_handler = on_disconnection_handler
38
38
self ._conf_ssl_context : Optional [SslConfigurationContext ] = ssl_context
39
39
self ._ssl_domain = None
40
+ self ._connections = [] # type: ignore
41
+ self ._index : int = - 1
42
+
43
+ def _set_environment_connection_list (self , connections : []): # type: ignore
44
+ self ._connections = connections
40
45
41
46
def dial (self ) -> None :
42
47
logger .debug ("Establishing a connection to the amqp server" )
@@ -72,9 +77,11 @@ def management(self) -> Management:
72
77
return self ._management
73
78
74
79
# closes the connection to the AMQP 1.0 server.
80
+ # This method should be called just from Environment and not from the user
75
81
def _close (self ) -> None :
76
82
logger .debug ("Closing connection" )
77
83
self ._conn .close ()
84
+ self ._connections .remove (self )
78
85
79
86
def publisher (self , destination : str ) -> Publisher :
80
87
if validate_address (destination ) is False :
Original file line number Diff line number Diff line change
1
+ # For the moment this is just a Connection pooler to keep compatibility with other clients
2
+ import logging
1
3
from typing import Annotated , Callable , Optional , TypeVar
2
4
3
5
from .connection import Connection
4
6
from .ssl_configuration import SslConfigurationContext
5
7
8
+ logger = logging .getLogger (__name__ )
9
+
6
10
MT = TypeVar ("MT" )
7
11
CB = Annotated [Callable [[MT ], None ], "Message callback type" ]
8
12
@@ -11,7 +15,7 @@ class Environment:
11
15
12
16
def __init__ (self ): # type: ignore
13
17
14
- self ._connections = []
18
+ self ._connections : list [ Connection ] = []
15
19
16
20
def connection (
17
21
self ,
@@ -28,10 +32,15 @@ def connection(
28
32
ssl_context = ssl_context ,
29
33
on_disconnection_handler = on_disconnection_handler ,
30
34
)
31
-
35
+ logger . debug ( "Environment: Creating and returning a new connection" )
32
36
self ._connections .append (connection )
37
+ connection ._set_environment_connection_list (self ._connections )
33
38
return connection
34
39
35
40
def close (self ) -> None :
41
+ logger .debug ("Environment: Closing all pending connections" )
36
42
for connection in self ._connections :
37
43
connection ._close ()
44
+
45
+ def connections (self ) -> list [Connection ]:
46
+ return self ._connections
Original file line number Diff line number Diff line change @@ -43,6 +43,34 @@ def test_connection_ssl() -> None:
43
43
environment .close ()
44
44
45
45
46
+ def test_environment_connections_management () -> None :
47
+
48
+ environment = Environment ()
49
+ connection = environment .connection ("amqp://guest:guest@localhost:5672/" )
50
+ connection .dial ()
51
+ connection2 = environment .connection ("amqp://guest:guest@localhost:5672/" )
52
+ connection2 .dial ()
53
+ connection3 = environment .connection ("amqp://guest:guest@localhost:5672/" )
54
+ connection3 .dial ()
55
+
56
+ assert len (environment .connections ()) == 3
57
+
58
+ # this shouldn't happen but we test it anyway
59
+ connection ._close ()
60
+
61
+ assert len (environment .connections ()) == 2
62
+
63
+ connection2 ._close ()
64
+
65
+ assert len (environment .connections ()) == 1
66
+
67
+ connection3 ._close ()
68
+
69
+ assert len (environment .connections ()) == 0
70
+
71
+ environment .close ()
72
+
73
+
46
74
def test_connection_reconnection () -> None :
47
75
48
76
reconnected = False
You can’t perform that action at this time.
0 commit comments