Skip to content

Commit

Permalink
fix connection failure when consul's leader running locally
Browse files Browse the repository at this point in the history
  • Loading branch information
amenezes committed Jun 29, 2019
1 parent 3a4b741 commit b8dead5
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 9 deletions.
10 changes: 6 additions & 4 deletions discovery/aioclient.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,14 @@ def __init__(self, host, port, app):
"""Create a instance for async consul client."""
super().__init__()
self.__discovery = consul.Consul(host, port)
self.__ensure_leader_connection(port, app)
self.__ensure_leader_connection(host, port, app)

def __ensure_leader_connection(self, port, loop):
def __ensure_leader_connection(self, host, port, loop):
leader = self.__discovery.status.leader()
leader = leader.split(':')
self.__discovery = consul.aio.Consul(f"{leader[0]}", port, loop)
leader = leader.split(':')[0]
if leader in ['127.0.0.1', 'localhost', '127.0.1.1']:
leader = host
self.__discovery = consul.aio.Consul(f"{leader}", port, loop)

def __create_service(self, service_name, service_port, healthcheck_path):
"""Adjust the data of the service to be managed."""
Expand Down
10 changes: 6 additions & 4 deletions discovery/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,14 @@ def __init__(self, host, port):
"""Create a instance for standard consul client."""
super().__init__()
self.__discovery = consul.Consul(host, port)
self.__ensure_leader_connection(port)
self.__ensure_leader_connection(host, port)

def __ensure_leader_connection(self, port):
def __ensure_leader_connection(self, host, port):
leader = self.__discovery.status.leader()
leader = leader.split(':')
self.__discovery = consul.Consul(f"{leader[0]}", port)
leader = leader.split(':')[0]
if leader in ['127.0.0.1', 'localhost', '127.0.1.1']:
leader = host
self.__discovery = consul.Consul(f"{leader}", port)

def __create_service(self, service_name, service_port, healthcheck_path):
"""Adjust the data of the service to be managed."""
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

setuptools.setup(
name="discovery-client",
version="0.2.4",
version="0.2.5",
author="alexandre menezes",
author_email="alexandre.fmenezes@gmail.com",
description="discovery service client",
Expand Down

0 comments on commit b8dead5

Please sign in to comment.