17
17
from typing import TYPE_CHECKING , Callable , Dict , Optional , Protocol , Tuple
18
18
19
19
if TYPE_CHECKING :
20
+ from aws_advanced_python_wrapper .database_dialect import DatabaseDialect
20
21
from aws_advanced_python_wrapper .driver_dialect import DriverDialect
21
22
from aws_advanced_python_wrapper .hostinfo import HostInfo , HostRole
22
23
from aws_advanced_python_wrapper .pep249 import Connection
38
39
39
40
class ConnectionProvider (Protocol ):
40
41
41
- def accepts_host_info (self , host_info : HostInfo , properties : Properties ) -> bool :
42
+ def accepts_host_info (self , host_info : HostInfo , props : Properties ) -> bool :
42
43
"""
43
44
Indicates whether this ConnectionProvider can provide connections for the given host and
44
45
properties. Some :py:class:`ConnectionProvider` implementations may not be able to handle certain connection properties.
45
46
46
47
:param host_info: the :py:class:`HostInfo` containing the host-port information for the host to connect to.
47
- :param properties : the connection properties.
48
+ :param props : the connection properties.
48
49
:return: `True` if this :py:class:`ConnectionProvider` can provide connections for the given host. `False` otherwise.
49
50
"""
50
51
...
@@ -59,7 +60,8 @@ def accepts_strategy(self, role: HostRole, strategy: str) -> bool:
59
60
"""
60
61
...
61
62
62
- def get_host_info_by_strategy (self , hosts : Tuple [HostInfo , ...], role : HostRole , strategy : str , props : Optional [Properties ]) -> HostInfo :
63
+ def get_host_info_by_strategy (
64
+ self , hosts : Tuple [HostInfo , ...], role : HostRole , strategy : str , props : Optional [Properties ]) -> HostInfo :
63
65
"""
64
66
Return a reader or a writer host using the specified strategy.
65
67
@@ -68,6 +70,7 @@ def get_host_info_by_strategy(self, hosts: Tuple[HostInfo, ...], role: HostRole,
68
70
:param hosts: the list of hosts to select from.
69
71
:param role: determines if the connection provider should return a reader or a writer host.
70
72
:param strategy: the host selection strategy to use.
73
+ :param props: the connection properties.
71
74
:return: the host selected using the specified strategy.
72
75
"""
73
76
...
@@ -76,30 +79,34 @@ def connect(
76
79
self ,
77
80
target_func : Callable ,
78
81
driver_dialect : DriverDialect ,
82
+ database_dialect : DatabaseDialect ,
79
83
host_info : HostInfo ,
80
- properties : Properties ) -> Connection :
84
+ props : Properties ) -> Connection :
81
85
"""
82
86
Called once per connection that needs to be created.
83
87
84
88
:param target_func: the `Connect` method used by target driver dialect.
85
89
:param driver_dialect: a dialect that handles target driver specific implementation.
90
+ :param database_dialect: a dialect that handles database engine specific implementation.
86
91
:param host_info: the host details for the desired connection.
87
- :param properties : the connection properties.
92
+ :param props : the connection properties.
88
93
:return: the established connection resulting from the given connection information.
89
94
"""
90
95
...
91
96
92
97
93
98
class DriverConnectionProvider (ConnectionProvider ):
94
- _accepted_strategies : Dict [str , HostSelector ] = {"random" : RandomHostSelector (), "round_robin" : RoundRobinHostSelector ()}
99
+ _accepted_strategies : Dict [str , HostSelector ] = \
100
+ {"random" : RandomHostSelector (), "round_robin" : RoundRobinHostSelector ()}
95
101
96
- def accepts_host_info (self , host_info : HostInfo , properties : Properties ) -> bool :
102
+ def accepts_host_info (self , host_info : HostInfo , props : Properties ) -> bool :
97
103
return True
98
104
99
105
def accepts_strategy (self , role : HostRole , strategy : str ) -> bool :
100
106
return strategy in self ._accepted_strategies
101
107
102
- def get_host_info_by_strategy (self , hosts : Tuple [HostInfo , ...], role : HostRole , strategy : str , props : Optional [Properties ]) -> HostInfo :
108
+ def get_host_info_by_strategy (
109
+ self , hosts : Tuple [HostInfo , ...], role : HostRole , strategy : str , props : Optional [Properties ]) -> HostInfo :
103
110
host_selector : Optional [HostSelector ] = self ._accepted_strategies .get (strategy )
104
111
if host_selector is not None :
105
112
return host_selector .get_host (hosts , role , props )
@@ -111,9 +118,11 @@ def connect(
111
118
self ,
112
119
target_func : Callable ,
113
120
driver_dialect : DriverDialect ,
121
+ database_dialect : DatabaseDialect ,
114
122
host_info : HostInfo ,
115
- properties : Properties ) -> Connection :
116
- prepared_properties = driver_dialect .prepare_connect_info (host_info , properties )
123
+ props : Properties ) -> Connection :
124
+ prepared_properties = driver_dialect .prepare_connect_info (host_info , props )
125
+ database_dialect .prepare_conn_props (prepared_properties )
117
126
logger .debug ("DriverConnectionProvider.ConnectingToHost" , host_info .host ,
118
127
PropertiesUtils .log_properties (PropertiesUtils .mask_properties (prepared_properties )))
119
128
return target_func (** prepared_properties )
@@ -141,23 +150,23 @@ def set_connection_provider(connection_provider: ConnectionProvider):
141
150
with ConnectionProviderManager ._lock :
142
151
ConnectionProviderManager ._conn_provider = connection_provider
143
152
144
- def get_connection_provider (self , host_info : HostInfo , properties : Properties ) -> ConnectionProvider :
153
+ def get_connection_provider (self , host_info : HostInfo , props : Properties ) -> ConnectionProvider :
145
154
"""
146
155
Get the :py:class:`ConnectionProvider` to use to establish a connection using the given host details and properties.
147
156
If a non-default :py:class:`ConnectionProvider` has been set using :py:method:`ConnectionProvider.set_connection_provider`
148
157
and :py:method:`ConnectionProvider.accepts_host_info` returns `True`, the non-default :py:class:`ConnectionProvider` will be returned.
149
158
Otherwise, the default :py:class:`ConnectionProvider` will be returned.
150
159
151
160
:param host_info: the host info for the connection that will be established.
152
- :param properties : the connection properties.
161
+ :param props : the connection properties.
153
162
:return: the :py:class:`ConnectionProvider` to use to establish a connection using the given host details and properties.
154
163
"""
155
164
if ConnectionProviderManager ._conn_provider is None :
156
165
return self .default_provider
157
166
158
167
with ConnectionProviderManager ._lock :
159
168
if ConnectionProviderManager ._conn_provider is not None \
160
- and ConnectionProviderManager ._conn_provider .accepts_host_info (host_info , properties ):
169
+ and ConnectionProviderManager ._conn_provider .accepts_host_info (host_info , props ):
161
170
return ConnectionProviderManager ._conn_provider
162
171
163
172
return self ._default_provider
@@ -180,7 +189,8 @@ def accepts_strategy(self, role: HostRole, strategy: str) -> bool:
180
189
181
190
return accepts_strategy
182
191
183
- def get_host_info_by_strategy (self , hosts : Tuple [HostInfo , ...], role : HostRole , strategy : str , props : Optional [Properties ]) -> HostInfo :
192
+ def get_host_info_by_strategy (
193
+ self , hosts : Tuple [HostInfo , ...], role : HostRole , strategy : str , props : Optional [Properties ]) -> HostInfo :
184
194
"""
185
195
Return a reader or a writer host using the specified strategy.
186
196
@@ -189,13 +199,15 @@ def get_host_info_by_strategy(self, hosts: Tuple[HostInfo, ...], role: HostRole,
189
199
:param hosts: the list of hosts to select from.
190
200
:param role: determines if the connection provider should return a reader or a writer host.
191
201
:param strategy: the host selection strategy to use.
202
+ :param props: the connection properties.
192
203
:return: the host selected using the specified strategy.
193
204
"""
194
205
if ConnectionProviderManager ._conn_provider is not None :
195
206
with ConnectionProviderManager ._lock :
196
207
if ConnectionProviderManager ._conn_provider is not None \
197
208
and ConnectionProviderManager ._conn_provider .accepts_strategy (role , strategy ):
198
- return ConnectionProviderManager ._conn_provider .get_host_info_by_strategy (hosts , role , strategy , props )
209
+ return ConnectionProviderManager ._conn_provider .get_host_info_by_strategy (
210
+ hosts , role , strategy , props )
199
211
200
212
return self ._default_provider .get_host_info_by_strategy (hosts , role , strategy , props )
201
213
0 commit comments