Skip to content

Commit

Permalink
[BOS-4577] Discovery aiohttp client session fix (#235)
Browse files Browse the repository at this point in the history
* [BOS-4577] Open and close discovery sessions

* Linter

* Remove noauth (as eventstore 5 does not allow this)

* Revert no auth test and container
  • Loading branch information
shaunickmistry authored Jul 14, 2020
1 parent b2ca80b commit 6aa6b29
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 18 deletions.
12 changes: 3 additions & 9 deletions photonpump/discovery.py
Original file line number Diff line number Diff line change
Expand Up @@ -286,17 +286,13 @@ def record_failure(self, node):


class ClusterDiscovery:
def __init__(self, seed_finder, http_session, retry_policy, selector):
self.session = http_session
def __init__(self, seed_finder, retry_policy, selector):
self.seeds = seed_finder
self.last_gossip = []
self.best_node = None
self.retry_policy = retry_policy
self.selector = selector

def close(self):
self.session.close()

def mark_failed(self, node):
self.seeds.mark_failed(node)

Expand All @@ -318,7 +314,8 @@ async def get_gossip(self):
raise DiscoveryFailed()

await self.retry_policy.wait(seed)
gossip = await fetch_new_gossip(self.session, seed)
async with aiohttp.ClientSession() as session:
gossip = await fetch_new_gossip(session, seed)

if gossip:
self.record_gossip(seed, gossip)
Expand Down Expand Up @@ -411,14 +408,12 @@ def get_discoverer(
NodeService(host or "localhost", port, None), retry_policy
)

session = aiohttp.ClientSession()
try:
socket.inet_aton(discovery_host)
LOG.info("Using cluster node discovery with a static seed")

return ClusterDiscovery(
StaticSeedFinder([NodeService(discovery_host, discovery_port, None)]),
session,
retry_policy or DiscoveryRetryPolicy(),
selector,
)
Expand All @@ -428,7 +423,6 @@ def get_discoverer(

return ClusterDiscovery(
DnsSeedFinder(discovery_host, resolver, discovery_port),
session,
retry_policy or DiscoveryRetryPolicy(),
selector,
)
11 changes: 2 additions & 9 deletions test/discovery_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,6 @@ async def test_discovery_with_a_static_seed():
assert await discoverer.next_node() == NodeService(first_node_ip, 1113, None)
assert await discoverer.next_node() == NodeService(second_node_ip, 1113, None)
assert discoverer.retry_policy.retries_per_node == 3
discoverer.close()


@pytest.mark.asyncio
Expand All @@ -216,11 +215,8 @@ async def wait(self, seed):
seed = NodeService("1.2.3.4", 2113, None)
gossip = data.make_gossip("2.3.4.5")
retry = always_succeed()
session = aiohttp.ClientSession()
with aioresponses() as mock:
successful_discoverer = ClusterDiscovery(
StaticSeedFinder([seed]), session, retry, None
)
successful_discoverer = ClusterDiscovery(StaticSeedFinder([seed]), retry, None)

mock.get("http://1.2.3.4:2113/gossip", status=500)
mock.get("http://1.2.3.4:2113/gossip", payload=gossip)
Expand All @@ -234,7 +230,6 @@ async def wait(self, seed):
assert stats.successes == 1
assert stats.failures == 1
assert stats.consecutive_failures == 0
await session.close()


@pytest.mark.asyncio
Expand All @@ -258,9 +253,7 @@ async def wait(self, seed):
retry = always_fail()
gossip = data.make_gossip("2.3.4.5")
with aioresponses() as mock:
successful_discoverer = ClusterDiscovery(
StaticSeedFinder([seed]), aiohttp.ClientSession(), retry, None
)
successful_discoverer = ClusterDiscovery(StaticSeedFinder([seed]), retry, None)

mock.get("http://1.2.3.4:2113/gossip", status=500)
mock.get("http://1.2.3.4:2113/gossip", payload=gossip)
Expand Down

0 comments on commit 6aa6b29

Please sign in to comment.