Make IP resolver performance a bit better by skipping an unnecessary set
This will still hang for a second or two on first call
This commit is contained in:
parent
c7baf6cf58
commit
b7d8bb227e
2 changed files with 8 additions and 9 deletions
|
@ -45,7 +45,7 @@ def track_and_log(event_name, repo_obj, analytics_name=None, analytics_sample=1,
|
||||||
if analytics_name is None:
|
if analytics_name is None:
|
||||||
analytics_name = event_name
|
analytics_name = event_name
|
||||||
|
|
||||||
logger.debug('Logging the %s to Mixpanel', analytics_name)
|
logger.debug('Logging the %s to analytics engine', analytics_name)
|
||||||
|
|
||||||
request_parsed = urlparse(request.url_root)
|
request_parsed = urlparse(request.url_root)
|
||||||
extra_params = {
|
extra_params = {
|
||||||
|
@ -57,10 +57,13 @@ def track_and_log(event_name, repo_obj, analytics_name=None, analytics_sample=1,
|
||||||
analytics.track(analytics_id, analytics_name, extra_params)
|
analytics.track(analytics_id, analytics_name, extra_params)
|
||||||
|
|
||||||
# Add the resolved information to the metadata.
|
# Add the resolved information to the metadata.
|
||||||
|
logger.debug('Resolving IP address %s', request.remote_addr)
|
||||||
resolved_ip = ip_resolver.resolve_ip(request.remote_addr)
|
resolved_ip = ip_resolver.resolve_ip(request.remote_addr)
|
||||||
if resolved_ip is not None:
|
if resolved_ip is not None:
|
||||||
metadata['resolved_ip'] = resolved_ip._asdict()
|
metadata['resolved_ip'] = resolved_ip._asdict()
|
||||||
|
|
||||||
|
logger.debug('Resolved IP address %s', request.remote_addr)
|
||||||
|
|
||||||
# Log the action to the database.
|
# Log the action to the database.
|
||||||
logger.debug('Logging the %s to logs system', event_name)
|
logger.debug('Logging the %s to logs system', event_name)
|
||||||
model.log.log_action(event_name, namespace_name, performer=get_authenticated_user(),
|
model.log.log_action(event_name, namespace_name, performer=get_authenticated_user(),
|
||||||
|
|
|
@ -127,12 +127,11 @@ class IPResolver(IPResolverInterface):
|
||||||
return None
|
return None
|
||||||
|
|
||||||
sync_token = aws_ip_range_json['syncToken']
|
sync_token = aws_ip_range_json['syncToken']
|
||||||
all_amazon, regions, services = IPResolver._parse_amazon_ranges(aws_ip_range_json)
|
all_amazon, regions = IPResolver._parse_amazon_ranges(aws_ip_range_json)
|
||||||
return IPResolver._build_location_function(sync_token, all_amazon, regions, services,
|
return IPResolver._build_location_function(sync_token, all_amazon, regions, self.geoip_db)
|
||||||
self.geoip_db)
|
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def _build_location_function(sync_token, all_amazon, regions, country, country_db):
|
def _build_location_function(sync_token, all_amazon, regions, country_db):
|
||||||
@lru_cache(maxsize=4096)
|
@lru_cache(maxsize=4096)
|
||||||
def _get_location(ip_address):
|
def _get_location(ip_address):
|
||||||
try:
|
try:
|
||||||
|
@ -167,15 +166,12 @@ class IPResolver(IPResolverInterface):
|
||||||
def _parse_amazon_ranges(ranges):
|
def _parse_amazon_ranges(ranges):
|
||||||
all_amazon = IPSet()
|
all_amazon = IPSet()
|
||||||
regions = defaultdict(IPSet)
|
regions = defaultdict(IPSet)
|
||||||
services = defaultdict(IPSet)
|
|
||||||
|
|
||||||
for service_description in ranges['prefixes']:
|
for service_description in ranges['prefixes']:
|
||||||
cidr = IPNetwork(service_description['ip_prefix'])
|
cidr = IPNetwork(service_description['ip_prefix'])
|
||||||
service = service_description['service']
|
|
||||||
region = service_description['region']
|
region = service_description['region']
|
||||||
|
|
||||||
all_amazon.add(cidr)
|
all_amazon.add(cidr)
|
||||||
regions[region].add(cidr)
|
regions[region].add(cidr)
|
||||||
services[service].add(cidr)
|
|
||||||
|
|
||||||
return all_amazon, regions, services
|
return all_amazon, regions
|
||||||
|
|
Reference in a new issue