Skip to content

Commit

Permalink
Merge pull request #18 from SomberNight/fix_no_cost_limit_outgoing
Browse files Browse the repository at this point in the history
cost accounting: fix "By default, do not limit outgoing connections"
  • Loading branch information
Neil committed Apr 30, 2019
2 parents 707c989 + 5e77259 commit 142bcd9
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 2 deletions.
4 changes: 2 additions & 2 deletions aiorpcx/session.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,6 @@ def __init__(self, session_factory, host=None, port=None, proxy=None,
self.protocol = None
self.loop = kwargs.get('loop', asyncio.get_event_loop())
self.kwargs = kwargs
# By default, do not limit outgoing connections
self.cost_hard_limit = 0

async def create_connection(self):
'''Initiate a connection.'''
Expand All @@ -78,6 +76,8 @@ async def create_connection(self):

async def __aenter__(self):
_transport, self.protocol = await self.create_connection()
# By default, do not limit outgoing connections
self.protocol.cost_hard_limit = 0
return self.protocol

async def __aexit__(self, exc_type, exc_value, traceback):
Expand Down
20 changes: 20 additions & 0 deletions tests/test_session.py
Original file line number Diff line number Diff line change
Expand Up @@ -356,6 +356,8 @@ async def test_concurrency(self, server):
async with Connector(RPCSession, 'localhost', server.port) as client:
# Prevent this interfering
client.cost_decay_per_sec = 0
# Outgoing sessions by default have no cost limits
client.cost_hard_limit = RPCSession.cost_hard_limit
# Test usage below soft limit
client.cost = client.cost_soft_limit - 10
client.recalc_concurrency()
Expand All @@ -382,6 +384,22 @@ async def test_concurrency(self, server):
async with client._incoming_concurrency:
pass

@pytest.mark.asyncio
async def test_concurrency_no_limit_for_outgoing(self, server):
async with Connector(RPCSession, 'localhost', server.port) as client:
# Prevent this interfering
client.cost_decay_per_sec = 0
# Test usage half-way
client.cost = (RPCSession.cost_soft_limit + RPCSession.cost_hard_limit) // 2
client.recalc_concurrency()
assert client._incoming_concurrency.max_concurrent == client.initial_concurrent
assert client._cost_fraction == 0
# Test above hard limit does not disconnect
client.cost = RPCSession.cost_hard_limit + 1
client.recalc_concurrency()
async with client._incoming_concurrency:
pass

@pytest.mark.asyncio
async def test_concurrency_decay(self, server):
async with Connector(RPCSession, 'localhost', server.port) as client:
Expand All @@ -402,6 +420,8 @@ async def test_concurrency_hard_limit_0(self, server):
@pytest.mark.asyncio
async def test_extra_cost(self, server):
async with Connector(RPCSession, 'localhost', server.port) as client:
# Outgoing sessions by default have no cost limits
client.cost_hard_limit = RPCSession.cost_hard_limit
client.extra_cost = lambda: client.cost_soft_limit + 1
client.recalc_concurrency()
assert 1 > client._cost_fraction > 0
Expand Down

0 comments on commit 142bcd9

Please sign in to comment.