PERF: avoid DNS lookups when getting IP info

Also cleans up interface in DiscourseIpInfo
grew cache to 2000 entries
This commit is contained in:
Sam 2018-10-31 12:38:57 +11:00
parent df62b48d51
commit 32b1f34910
3 changed files with 17 additions and 12 deletions

View File

@ -436,7 +436,7 @@ class Admin::UsersController < Admin::AdminController
def ip_info def ip_info
params.require(:ip) params.require(:ip)
render json: DiscourseIpInfo.get(params[:ip]) render json: DiscourseIpInfo.get(params[:ip], resolve_hostname: true)
end end
def sync_sso def sync_sso

View File

@ -20,7 +20,7 @@ module UserAuthTokensMixin
end end
def location def location
ipinfo = DiscourseIpInfo.get(client_ip, I18n.locale) ipinfo = DiscourseIpInfo.get(client_ip, locale: I18n.locale)
location = [ipinfo[:city], ipinfo[:region], ipinfo[:country]].reject { |x| x.blank? }.join(", ") location = [ipinfo[:city], ipinfo[:region], ipinfo[:country]].reject { |x| x.blank? }.join(", ")
return I18n.t('staff_action_logs.unknown') if location.blank? return I18n.t('staff_action_logs.unknown') if location.blank?

View File

@ -11,7 +11,7 @@ class DiscourseIpInfo
def open_db(path) def open_db(path)
@loc_mmdb = mmdb_load(File.join(path, 'GeoLite2-City.mmdb')) @loc_mmdb = mmdb_load(File.join(path, 'GeoLite2-City.mmdb'))
@asn_mmdb = mmdb_load(File.join(path, 'GeoLite2-ASN.mmdb')) @asn_mmdb = mmdb_load(File.join(path, 'GeoLite2-ASN.mmdb'))
@cache = LruRedux::ThreadSafeCache.new(1000) @cache = LruRedux::ThreadSafeCache.new(2000)
end end
def mmdb_load(filepath) def mmdb_load(filepath)
@ -24,7 +24,7 @@ class DiscourseIpInfo
end end
end end
def lookup(ip, locale = :en) def lookup(ip, locale: :en, resolve_hostname: false)
ret = {} ret = {}
if @loc_mmdb if @loc_mmdb
@ -57,27 +57,32 @@ class DiscourseIpInfo
end end
end end
# this can block for quite a while
# only use it explicitly when needed
if resolve_hostname
begin begin
result = Resolv::DNS.new.getname(ip) result = Resolv::DNS.new.getname(ip)
ret[:hostname] = result&.to_s ret[:hostname] = result&.to_s
rescue Resolv::ResolvError rescue Resolv::ResolvError
end end
end
ret ret
end end
def get(ip, locale = :en) def get(ip, locale: :en, resolve_hostname: false)
ip = ip.to_s ip = ip.to_s
locale = locale.to_s.sub('_', '-') locale = locale.to_s.sub('_', '-')
@cache["#{ip}-#{locale}"] ||= lookup(ip, locale) @cache["#{ip}-#{locale}-#{resolve_hostname}"] ||=
lookup(ip, locale: locale, resolve_hostname: resolve_hostname)
end end
def self.open_db(path) def self.open_db(path)
instance.open_db(path) instance.open_db(path)
end end
def self.get(ip, locale = :en) def self.get(ip, locale: :en, resolve_hostname: false)
instance.get(ip, locale) instance.get(ip, locale: locale, resolve_hostname: resolve_hostname)
end end
end end