Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
47 commits
Select commit Hold shift + click to select a range
e62825f
Shared connection (sync) draft
Jul 15, 2019
84ae397
Shared connection (sync) draft 2
Jul 16, 2019
6e6c827
Shared connection (sync) test update
Jul 16, 2019
a86146e
Shared connection
Jul 17, 2019
c5a23c8
Fix an issue
Jul 17, 2019
b83264d
add retry exponential delay and timeout to exception handling
Jul 22, 2019
4f17781
put module method before class def
Jul 22, 2019
f0d98d1
fixed Client.get_properties error
Jul 22, 2019
ed7d414
new eph (draft)
Jul 27, 2019
ee228b4
new eph (draft2)
Jul 29, 2019
1d65719
remove in memory partition manager
Jul 29, 2019
4895385
EventProcessor draft 3
Jul 30, 2019
6415ac2
small format change
Jul 30, 2019
a685f87
Fix logging
Jul 30, 2019
6388f4a
Add EventProcessor example
Jul 30, 2019
1cd6275
Merge branch 'eventhubs_preview2' into eventhubs_eph
Jul 30, 2019
6394f19
use decorator to implement retry logic and update some tests (#6544)
yunhaoling Jul 30, 2019
56fdd1e
Update livetest (#6547)
yunhaoling Jul 30, 2019
10f0be6
Remove legacy code and update livetest (#6549)
yunhaoling Jul 30, 2019
fbb66bd
make sync longrunning multi-threaded
Jul 31, 2019
00ff723
small changes on async long running test
Jul 31, 2019
0f5180c
reset retry_count for iterator
Jul 31, 2019
6e0c238
Don't return early when open a ReceiveClient or SendClient
Jul 31, 2019
0efc95f
type annotation change
Jul 31, 2019
90fbafb
Update kwargs and remove unused import
yunhaoling Jul 31, 2019
e06bad8
Misc changes from EventProcessor PR review
Jul 31, 2019
dd1d7ae
raise asyncio.CancelledError out instead of supressing it.
Jul 31, 2019
9d18dd9
Merge branch 'eventhubs_dev' into eventhubs_eph
Jul 31, 2019
a31ee67
Update livetest and small fixed (#6594)
yunhaoling Jul 31, 2019
19a5539
Merge branch 'eventhubs_dev' into eventhubs_eph
Aug 1, 2019
997dacf
Fix feedback from PR (1)
Aug 2, 2019
d688090
Revert "Merge branch 'eventhubs_dev' into eventhubs_eph"
Aug 2, 2019
2399dcb
Fix feedback from PR (2)
Aug 2, 2019
5ad0255
Update code according to the review (#6623)
yunhaoling Aug 2, 2019
5679065
Fix feedback from PR (3)
Aug 2, 2019
35245a1
Merge branch 'eventhubs_dev' into eventhubs_eph
Aug 2, 2019
83d0ec2
small bug fixing
Aug 2, 2019
a2195ba
Remove old EPH
Aug 2, 2019
f8acc8b
Update decorator implementation (#6642)
yunhaoling Aug 2, 2019
6fe4533
Remove old EPH pytest
Aug 2, 2019
598245d
Revert "Revert "Merge branch 'eventhubs_dev' into eventhubs_eph""
Aug 2, 2019
97dfce5
Update sample codes and docstring (#6643)
yunhaoling Aug 2, 2019
64c5c7d
Check tablename to prevent sql injection
Aug 3, 2019
13014dd
PR review update
Aug 3, 2019
3e82e90
Removed old EPH stuffs.
Aug 3, 2019
0f670d8
Small fix (#6650)
yunhaoling Aug 3, 2019
2a8b34c
Merge branch 'eventhubs_dev' into eventhubs_eph
Aug 3, 2019
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Shared connection
  • Loading branch information
yijxie committed Jul 17, 2019
commit a86146e776d455795f53fe9f577188d1380e9a6b
25 changes: 19 additions & 6 deletions sdk/eventhub/azure-eventhubs/azure/eventhub/_connection_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@
# Licensed under the MIT License. See License.txt in the project root for license information.
# --------------------------------------------------------------------------------------------

import threading
from uamqp import Connection, TransportType
from threading import RLock
from uamqp import Connection, TransportType, c_uamqp


class _SharedConnectionManager(object):
def __init__(self, **kwargs):
self._lock = threading.Lock()
self._conn = None
self._lock = RLock()
self._conn = None # type: Connection

self._container_id = kwargs.get("container_id")
self._debug = kwargs.get("debug")
Expand Down Expand Up @@ -42,12 +42,22 @@ def get_connection(self, host, auth):
encoding=self._encoding)
return self._conn

def close_connection(self, conn=None):
def close_connection(self):
with self._lock:
if self._conn:
self._conn.destroy()
self._conn = None

def reset_connection_if_broken(self):
with self._lock:
if self._conn and self._conn._state in (
c_uamqp.ConnectionState.CLOSE_RCVD,
c_uamqp.ConnectionState.CLOSE_SENT,
c_uamqp.ConnectionState.DISCARDING,
c_uamqp.ConnectionState.END,
):
self._conn = None


class _SeparateConnectionManager(object):
def __init__(self, **kwargs):
Expand All @@ -59,6 +69,9 @@ def get_connection(self, host, auth):
def close_connection(self):
pass

def reset_connection_if_broken(self):
pass


def get_connection_manager(**kwargs):
return _SeparateConnectionManager(**kwargs)
return _SharedConnectionManager(**kwargs)
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
# --------------------------------------------------------------------------------------------
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License. See License.txt in the project root for license information.
# --------------------------------------------------------------------------------------------

from asyncio import Lock
from uamqp import TransportType, c_uamqp
from uamqp.async_ops import ConnectionAsync


class _SharedConnectionManager(object):
def __init__(self, **kwargs):
self._lock = Lock()
self._conn = None

self._container_id = kwargs.get("container_id")
self._debug = kwargs.get("debug")
self._error_policy = kwargs.get("error_policy")
self._properties = kwargs.get("properties")
self._encoding = kwargs.get("encoding") or "UTF-8"
self._transport_type = kwargs.get('transport_type') or TransportType.Amqp
self._http_proxy = kwargs.get('http_proxy')
self._max_frame_size = kwargs.get("max_frame_size")
self._channel_max = kwargs.get("channel_max")
self._idle_timeout = kwargs.get("idle_timeout")
self._remote_idle_timeout_empty_frame_send_ratio = kwargs.get("remote_idle_timeout_empty_frame_send_ratio")

async def get_connection(self, host, auth):
# type: (...) -> ConnectionAsync
async with self._lock:
if self._conn is None:
self._conn = ConnectionAsync(
host,
auth,
container_id=self._container_id,
max_frame_size=self._max_frame_size,
channel_max=self._channel_max,
idle_timeout=self._idle_timeout,
properties=self._properties,
remote_idle_timeout_empty_frame_send_ratio=self._remote_idle_timeout_empty_frame_send_ratio,
error_policy=self._error_policy,
debug=self._debug,
encoding=self._encoding)
return self._conn

async def close_connection(self):
async with self._lock:
if self._conn:
await self._conn.destroy_async()
self._conn = None

def reset_connection_if_broken(self):
with self._lock:
if self._conn and self._conn._state in (
c_uamqp.ConnectionState.CLOSE_RCVD,
c_uamqp.ConnectionState.CLOSE_SENT,
c_uamqp.ConnectionState.DISCARDING,
c_uamqp.ConnectionState.END,
):
self._conn = None

class _SeparateConnectionManager(object):
def __init__(self, **kwargs):
pass

async def get_connection(self, host, auth):
pass # return None

async def close_connection(self):
pass

def reset_connection_if_broken(self):
pass


def get_connection_manager(**kwargs):
return _SharedConnectionManager(**kwargs)
45 changes: 29 additions & 16 deletions sdk/eventhub/azure-eventhubs/azure/eventhub/aio/client_async.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@

from .producer_async import EventHubProducer
from .consumer_async import EventHubConsumer
from ._connection_manager_async import get_connection_manager
from .error_async import _handle_exception


log = logging.getLogger(__name__)
Expand All @@ -42,6 +44,16 @@ class EventHubClient(EventHubClientAbstract):

"""

def __init__(self, host, event_hub_path, credential, **kwargs):
super(EventHubClient, self).__init__(host, event_hub_path, credential, **kwargs)
self._conn_manager = get_connection_manager(**kwargs)

async def __aenter__(self):
return self

async def __aexit__(self, exc_type, exc_val, exc_tb):
await self.close()

def _create_auth(self, username=None, password=None):
"""
Create an ~uamqp.authentication.cbs_auth_async.SASTokenAuthAsync instance to authenticate
Expand Down Expand Up @@ -85,33 +97,31 @@ def _create_auth(self, username=None, password=None):
get_jwt_token, http_proxy=http_proxy,
transport_type=transport_type)

async def _handle_exception(self, exception, retry_count, max_retries):
await _handle_exception(exception, retry_count, max_retries, self, log)

async def _close_connection(self):
self._conn_manager.reset_connection_if_broken()

async def _management_request(self, mgmt_msg, op_type):
alt_creds = {
"username": self._auth_config.get("iot_username"),
"password": self._auth_config.get("iot_password")}
connect_count = 0
max_retries = self.config.max_retries
retry_count = 0
while True:
connect_count += 1
mgmt_auth = self._create_auth(**alt_creds)
mgmt_auth = self._create_auth()
mgmt_client = AMQPClientAsync(self.mgmt_target, auth=mgmt_auth, debug=self.config.network_tracing)
try:
await mgmt_client.open_async()
conn = await self._conn_manager.get_connection(self.host, mgmt_auth)
await mgmt_client.open_async(connection=conn)
response = await mgmt_client.mgmt_request_async(
mgmt_msg,
constants.READ_OPERATION,
op_type=op_type,
status_code_field=b'status-code',
description_fields=b'status-description')
return response
except (errors.AMQPConnectionError, errors.TokenAuthFailure, compat.TimeoutException) as failure:
if connect_count >= self.config.max_retries:
err = ConnectError(
"Can not connect to EventHubs or get management info from the service. "
"Please make sure the connection string or token is correct and retry. "
"Besides, this method doesn't work if you use an IoT connection string.",
failure
)
raise err
except Exception as exception:
await self._handle_exception(exception, retry_count, max_retries)
retry_count += 1
finally:
await mgmt_client.close_async()

Expand Down Expand Up @@ -263,3 +273,6 @@ def create_producer(
handler = EventHubProducer(
self, target, partition=partition_id, send_timeout=send_timeout, loop=loop)
return handler

async def close(self):
await self._conn_manager.close_connection()
Loading