Skip to content

Commit

Permalink
lib/scylla_cloud: use functool.cache to cache identify_cloud_async()
Browse files Browse the repository at this point in the history
this change is a follow-up of d97e311, which caches the result
of `identify_cloud_async()` using a global variable. it would be
more idiomatic to use `functool.cache` for caching a costy function
like `identify_cloud_async()`, so in this change we use it for
caching. simpler this way. and more importantly, it is more readable.

Signed-off-by: Kefu Chai <kefu.chai@scylladb.com>
  • Loading branch information
tchaikov authored and yaronkaikov committed Mar 27, 2024
1 parent 5d264db commit c70ef65
Showing 1 changed file with 4 additions and 5 deletions.
9 changes: 4 additions & 5 deletions lib/scylla_cloud.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#
# SPDX-License-Identifier: Apache-2.0

import functools
import json
import logging
import os
Expand Down Expand Up @@ -906,17 +907,15 @@ async def identify_cloud_async():
break
return result

_identify_cloud_result = None

@functools.cache
def identify_cloud():
global _identify_cloud_result
if _identify_cloud_result:
return _identify_cloud_result
time_start = datetime.datetime.now()
result = asyncio.run(identify_cloud_async())
time_end = datetime.datetime.now()
_identify_cloud_result = result
return result


def is_ec2():
return identify_cloud() == aws_instance

Expand Down

0 comments on commit c70ef65

Please sign in to comment.