33# Licensed under the MIT License. See License.txt in the project root for license information.
44# --------------------------------------------------------------------------------------------
55
6+ import uuid
67import asyncio
78
89from uamqp import constants , errors
@@ -17,7 +18,7 @@ class AsyncSender(Sender):
1718 Implements the async API of a Sender.
1819 """
1920
20- def __init__ (self , client , target , partition = None , loop = None ): # pylint: disable=super-init-not-called
21+ def __init__ (self , client , target , partition = None , keep_alive = None , auto_reconnect = True , loop = None ): # pylint: disable=super-init-not-called
2122 """
2223 Instantiate an EventHub event SenderAsync handler.
2324
@@ -31,18 +32,23 @@ def __init__(self, client, target, partition=None, loop=None): # pylint: disabl
3132 self .client = client
3233 self .target = target
3334 self .partition = partition
35+ self .keep_alive = keep_alive
36+ self .auto_reconnect = auto_reconnect
3437 self .retry_policy = errors .ErrorPolicy (max_retries = 3 , on_error = _error_handler )
38+ self .name = "EHSender-{}" .format (uuid .uuid4 ())
3539 self .redirected = None
3640 self .error = None
3741 if partition :
3842 self .target += "/Partitions/" + partition
43+ self .name += "-partition{}" .format (partition )
3944 self ._handler = SendClientAsync (
4045 self .target ,
4146 auth = self .client .get_auth (),
4247 debug = self .client .debug ,
4348 msg_timeout = Sender .TIMEOUT ,
4449 error_policy = self .retry_policy ,
45- keep_alive_interval = 30 ,
50+ keep_alive_interval = self .keep_alive ,
51+ client_name = self .name ,
4652 properties = self .client .create_properties (),
4753 loop = self .loop )
4854 self ._outcome = None
@@ -65,7 +71,8 @@ async def open_async(self):
6571 debug = self .client .debug ,
6672 msg_timeout = Sender .TIMEOUT ,
6773 error_policy = self .retry_policy ,
68- keep_alive_interval = 30 ,
74+ keep_alive_interval = self .keep_alive ,
75+ client_name = self .name ,
6976 properties = self .client .create_properties (),
7077 loop = self .loop )
7178 await self ._handler .open_async ()
@@ -85,7 +92,8 @@ async def reconnect_async(self):
8592 debug = self .client .debug ,
8693 msg_timeout = Sender .TIMEOUT ,
8794 error_policy = self .retry_policy ,
88- keep_alive_interval = 30 ,
95+ keep_alive_interval = self .keep_alive ,
96+ client_name = self .name ,
8997 properties = self .client .create_properties (),
9098 loop = self .loop )
9199 await self ._handler .open_async ()
@@ -158,14 +166,19 @@ async def send(self, event_data):
158166 if self ._outcome != constants .MessageSendResult .Ok :
159167 raise Sender ._error (self ._outcome , self ._condition )
160168 except (errors .LinkDetach , errors .ConnectionClose ) as shutdown :
161- if shutdown .action .retry :
169+ if shutdown .action .retry and self .auto_reconnect :
170+ await self .reconnect_async ()
171+ else :
172+ error = EventHubError (str (shutdown ), shutdown )
173+ await self .close_async (exception = error )
174+ raise error
175+ except errors .MessageHandlerError as shutdown :
176+ if self .auto_reconnect :
162177 await self .reconnect_async ()
163178 else :
164179 error = EventHubError (str (shutdown ), shutdown )
165180 await self .close_async (exception = error )
166181 raise error
167- except errors .MessageHandlerError :
168- await self .reconnect_async ()
169182 except Exception as e :
170183 error = EventHubError ("Send failed: {}" .format (e ))
171184 await self .close_async (exception = error )
@@ -182,13 +195,18 @@ async def wait_async(self):
182195 try :
183196 await self ._handler .wait_async ()
184197 except (errors .LinkDetach , errors .ConnectionClose ) as shutdown :
185- if shutdown .action .retry :
198+ if shutdown .action .retry and self .auto_reconnect :
199+ await self .reconnect_async ()
200+ else :
201+ error = EventHubError (str (shutdown ), shutdown )
202+ await self .close_async (exception = error )
203+ raise error
204+ except errors .MessageHandlerError as shutdown :
205+ if self .auto_reconnect :
186206 await self .reconnect_async ()
187207 else :
188208 error = EventHubError (str (shutdown ), shutdown )
189209 await self .close_async (exception = error )
190210 raise error
191- except errors .MessageHandlerError :
192- await self .reconnect_async ()
193211 except Exception as e :
194212 raise EventHubError ("Send failed: {}" .format (e ))
0 commit comments