Merge pull request #3193 from quay/ipresolver-perf

Make IP resolver performance a bit better by skipping an unnecessary set
This commit is contained in:
Joseph Schorr 2018-08-08 17:28:47 -04:00 committed by GitHub
commit f6902f8096
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 8 additions and 9 deletions

View file

@ -45,7 +45,7 @@ def track_and_log(event_name, repo_obj, analytics_name=None, analytics_sample=1,
if analytics_name is None:
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)
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)
# 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)
if resolved_ip is not None:
metadata['resolved_ip'] = resolved_ip._asdict()
logger.debug('Resolved IP address %s', request.remote_addr)
# Log the action to the database.
logger.debug('Logging the %s to logs system', event_name)
model.log.log_action(event_name, namespace_name, performer=get_authenticated_user(),

View file

@ -127,12 +127,11 @@ class IPResolver(IPResolverInterface):
return None
sync_token = aws_ip_range_json['syncToken']
all_amazon, regions, services = IPResolver._parse_amazon_ranges(aws_ip_range_json)
return IPResolver._build_location_function(sync_token, all_amazon, regions, services,
self.geoip_db)
all_amazon, regions = IPResolver._parse_amazon_ranges(aws_ip_range_json)
return IPResolver._build_location_function(sync_token, all_amazon, regions, self.geoip_db)
@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)
def _get_location(ip_address):
try:
@ -167,15 +166,12 @@ class IPResolver(IPResolverInterface):
def _parse_amazon_ranges(ranges):
all_amazon = IPSet()
regions = defaultdict(IPSet)
services = defaultdict(IPSet)
for service_description in ranges['prefixes']:
cidr = IPNetwork(service_description['ip_prefix'])
service = service_description['service']
region = service_description['region']
all_amazon.add(cidr)
regions[region].add(cidr)
services[service].add(cidr)
return all_amazon, regions, services
return all_amazon, regions