diff --git a/app/controllers/click.cr b/app/controllers/click.cr index 1665461..a71f413 100644 --- a/app/controllers/click.cr +++ b/app/controllers/click.cr @@ -30,7 +30,7 @@ module App::Controllers click = App::Models::Click.new click.link_id = link_id - click.country = client_ip ? IpLookup.country(client_ip).try(&.code) : nil + click.country = client_ip ? IpLookup.country(client_ip) : nil click.user_agent = user_agent_str click.browser = ua_parser.try(&.family) click.os = ua_parser.try(&.os.try(&.family)) diff --git a/app/lib/ip_lookup.cr b/app/lib/ip_lookup.cr index 08f021b..290ead6 100644 --- a/app/lib/ip_lookup.cr +++ b/app/lib/ip_lookup.cr @@ -1,48 +1,45 @@ require "maxminddb" require "log" -struct IpLookup - MMDB_PATH = "data/GeoLite2-Country.mmdb" +module App::Lib + struct IpLookup + MMDB_PATH = "data/GeoLite2-Country.mmdb" - record Country, code : String? = nil, name : String? = nil + @@reader : MaxMindDB::Reader? = nil + @@reader_mutex = Mutex.new - def self.country(ip_address : String) : Country? - return nil if ip_address == "Unknown" || ip_address.empty? + private def self.get_reader : MaxMindDB::Reader + @@reader_mutex.synchronize do + @@reader ||= MaxMindDB.open(MMDB_PATH) + end + end - begin - reader = MaxMindDB.open(MMDB_PATH) - lookup = reader.get(ip_address) + def self.country(ip_address : String) : String? + return nil if ip_address == "Unknown" || ip_address.empty? - country_code = lookup["country"]?.try &.["iso_code"]?.try &.as_s - country_name = lookup["country"]?.try &.["names"]?.try &.["en"]?.try &.as_s - - if country_code || country_name - Country.new( - code: country_code, - name: country_name - ) - else + begin + lookup = get_reader.get(ip_address) + lookup["country"]?.try &.["iso_code"]?.try &.as_s + rescue ex + Log.error { "IP lookup failed: #{ex.message}" } nil end - rescue ex - Log.error { "IP lookup failed: #{ex.message}" } - nil end - end - def self.ip_from_address(address_string : String?) : String? - return nil if address_string.nil? + def self.ip_from_address(address_string : String?) : String? + return nil if address_string.nil? - if address_string.includes?('[') # IPv6 with port: [2001:db8::1]:8080 - address_string.split(']').first.sub('[', '\'') - elsif address_string.includes?(':') - if address_string.count(':') > 1 # IPv6 without port + if address_string.includes?('[') # IPv6 with port: [2001:db8::1]:8080 + address_string.split(']').first.sub('[', '\'') + elsif address_string.includes?(':') + if address_string.count(':') > 1 # IPv6 without port + address_string + else # IPv4 with port: 192.168.1.1:8080 + address_string.split(':').first + end + else # Address without port address_string - else # IPv4 with port: 192.168.1.1:8080 - address_string.split(':').first end - else # Address without port - address_string end end end diff --git a/benchmark.cr b/benchmark.cr index 859cead..75672f4 100755 --- a/benchmark.cr +++ b/benchmark.cr @@ -8,7 +8,7 @@ require "file_utils" SERVER_URL = "http://localhost:4000" API_URL = "#{SERVER_URL}/api/links" API_KEY = "secure_api_key_1" -TIME = "59s" +NUMBER_OF_REQUESTS = 1000 CONTAINER_NAME = "bit" STATS_FILE = "resource_usage.txt" @@ -157,7 +157,7 @@ def run_benchmark sleep 2.seconds process = Process.new( "bombardier", - ["-d", TIME.to_s, "-c", "30", "-l", "--disableKeepAlives", random_link], + ["-n", NUMBER_OF_REQUESTS.to_s, "-c", "30", "-l", "--disableKeepAlives", random_link], output: Process::Redirect::Inherit, error: Process::Redirect::Inherit )