Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: timeout tesst and minor fix #9

Merged
merged 1 commit into from
Jun 7, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
20 changes: 11 additions & 9 deletions src/amplitude_experiment/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,18 +110,20 @@ def __do_fetch(self, user):
self.logger.warning(f"[Experiment] encoded user object length ${len(body)} "
f"cannot be cached by CDN; must be < 8KB")
self.logger.debug(f"[Experiment] Fetch variants for user: {str(user_context)}")
response = conn.request('POST', '/sdk/vardata', body, headers)
self._connection_pool.release(conn)
elapsed = '%.3f' % ((time.time() - start) * 1000)
self.logger.debug(f"[Experiment] Fetch complete in {elapsed} ms")
json_response = json.loads(response.read().decode("utf8"))
variants = self.__parse_json_variants(json_response)
self.logger.debug(f"[Experiment] Fetched variants: {json.dumps(variants, default=str)}")
return variants
try:
response = conn.request('POST', '/sdk/vardata', body, headers)
elapsed = '%.3f' % ((time.time() - start) * 1000)
self.logger.debug(f"[Experiment] Fetch complete in {elapsed} ms")
json_response = json.loads(response.read().decode("utf8"))
variants = self.__parse_json_variants(json_response)
self.logger.debug(f"[Experiment] Fetched variants: {json.dumps(variants, default=str)}")
return variants
finally:
self._connection_pool.release(conn)

def __setup_connection_pool(self):
scheme, _, host = self.config.server_url.split('/', 3)
timeout = int(self.config.fetch_timeout_millis / 1000)
timeout = self.config.fetch_timeout_millis / 1000
self._connection_pool = HTTPConnectionPool(host, max_size=1, idle_timeout=30,
read_timeout=timeout, scheme=scheme)

Expand Down
2 changes: 1 addition & 1 deletion src/amplitude_experiment/connection_pool.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ def close(self) -> None:
class HTTPConnectionPool:

def __init__(self, host: str, port: int = None, max_size: int = None, idle_timeout: int = None,
read_timeout: int = None, scheme: str = 'https') -> None:
read_timeout: float = None, scheme: str = 'https') -> None:
"""
A simple connection pool to reuse the http connections
:param host: pass
Expand Down
6 changes: 6 additions & 0 deletions tests/client_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,12 @@ def test_fetch_async(self):
user = User(user_id='test_user')
self.client.fetch_async(user, self.callback_for_async)

def test_fetch_failed_with_retry(self):
with Client(API_KEY, Config(debug=False, fetch_retries=1, fetch_timeout_millis=1)) as client:
user = User(user_id='test_user')
variants = client.fetch(user)
self.assertEqual({}, variants)


if __name__ == '__main__':
unittest.main()