Skip to content

Commit

Permalink
Merge pull request #394 from PyPlanet/feature/386
Browse files Browse the repository at this point in the history
[TASK] Implement retry in the GBX connector, total of 5 retries maximum.
  • Loading branch information
tomvlk committed Jul 1, 2017
2 parents a67f374 + 5a03f78 commit f7adc18
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 8 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ Core
More info: https://github.com/PyPlanet/PyPlanet/issues/392

**The old way will break your app from version 0.8.0**

* Improvement: Retry 5 times when connecting to the dedicated server, making it possible to start both at the same time.

Apps
~~~~
Expand Down
32 changes: 24 additions & 8 deletions pyplanet/core/gbx/remote.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ class GbxRemote:
"""
The GbxClient holds the connection to the dedicated server. Maintains the queries and the handlers it got.
"""
MAX_REQUEST_SIZE = 2000000 # 2MB
MAX_RESPONSE_SIZE = 4000000 # 4MB
MAX_REQUEST_SIZE = 2000000 # 2MB
MAX_RESPONSE_SIZE = 4000000 # 4MB

def __init__(self, host, port, event_pool=None, user=None, password=None, api_version='2013-04-16', instance=None):
"""
Expand Down Expand Up @@ -94,12 +94,28 @@ async def connect(self):
"""
logger.debug('Trying to connect to the dedicated server...')

# Create socket (produces coroutine).
self.reader, self.writer = await asyncio.open_connection(
host=self.host,
port=self.port,
loop=self.event_loop,
)
# Create socket (+ retry few times if not successful.
retries = 0
while True:
try:
self.reader, self.writer = await asyncio.open_connection(
host=self.host,
port=self.port,
loop=self.event_loop,
)
break
except Exception as exc:
if retries >= 5:
raise
retries += 1

logger.info('Coudn\'t connect to Dedicated Server. Retry {} of {} (Error: {}'.format(
retries,
5,
str(exc)
))
await asyncio.sleep(2)

_, header = struct.unpack_from('<L11s', await self.reader.readexactly(15))
if header.decode() != 'GBXRemote 2':
raise TransportException('Server is not a valid GBXRemote 2 server.')
Expand Down

0 comments on commit f7adc18

Please sign in to comment.