-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Cache mx_server_is_in? to speed up disposable_mx_server? #1
base: main
Are you sure you want to change the base?
Changes from all commits
0c8aa89
8f26a01
652740f
42984d0
1374975
74017a0
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,7 +4,7 @@ module ValidEmail2 | |
class Dns | ||
MAX_CACHE_SIZE = 1_000 | ||
CACHE = {} | ||
|
||
MX_SERVERS_CACHE = {} | ||
CacheEntry = Struct.new(:records, :cached_at, :ttl) | ||
|
||
def self.prune_cache | ||
|
@@ -26,6 +26,26 @@ def mx_servers(domain) | |
fetch(domain, Resolv::DNS::Resource::IN::MX) | ||
end | ||
|
||
def mx_servers_disposable?(domain, domain_list) | ||
servers = mx_servers(domain) | ||
cache_key = generate_mx_cache_key(domain, domain_list, servers) | ||
return MX_SERVERS_CACHE[cache_key] if !cache_key.nil? && MX_SERVERS_CACHE.key?(cache_key) | ||
|
||
result = servers.any? do |mx_server| | ||
return false unless mx_server.respond_to?(:exchange) | ||
|
||
mx_server = mx_server.exchange.to_s | ||
|
||
domain_list.any? do |disposable_domain| | ||
mx_server.end_with?(disposable_domain) && mx_server.match?(/\A(?:.+\.)*?#{disposable_domain}\z/) | ||
end | ||
end | ||
|
||
MX_SERVERS_CACHE[cache_key] = result unless cache_key.nil? | ||
|
||
result | ||
end | ||
|
||
def a_servers(domain) | ||
fetch(domain, Resolv::DNS::Resource::IN::A) | ||
end | ||
|
@@ -67,5 +87,14 @@ def resolv_config | |
config[:nameserver] = @dns_nameserver if @dns_nameserver | ||
config | ||
end | ||
|
||
def generate_mx_cache_key(domain, domain_list, mx_servers) | ||
return if mx_servers.empty? || domain_list.empty? | ||
|
||
mx_servers_str = mx_servers.map(&:exchange).map(&:to_s).sort.join | ||
return domain if mx_servers_str.empty? | ||
|
||
"#{domain_list.object_id}_#{domain_list.length}_#{mx_servers_str.downcase}" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we use hashing function here instead? I might be missing the idea of this cache key function tho.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In the case of checking against disposable MX servers, I also thought about some kind of consistent sampling of the domain list, but maybe we don't need to go there? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think the current approach seems reasonable and we can see what the maintainers think when you open a PR with the open source project. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Btw, There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah, I'm aware of that and... that's the compromise. In my book, as long as the cache is either valid or invalidated, it's fine. |
||
end | ||
end | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -111,14 +111,55 @@ | |
let(:email_instance) { described_class.new(email_address, dns_instance) } | ||
let(:ttl) { 1_000 } | ||
let(:mock_resolv_dns) { instance_double(Resolv::DNS) } | ||
let(:mock_mx_records) { [double("MX", exchange: "mx.ymail.com", preference: 10, ttl: ttl)] } | ||
let(:mock_mx_records) { [double("MX", exchange: "mx.ymail.com", preference: 10, ttl:)] } | ||
|
||
before do | ||
allow(email_instance).to receive(:null_mx?).and_return(false) | ||
allow(Resolv::DNS).to receive(:open).and_yield(mock_resolv_dns) | ||
allow(mock_resolv_dns).to receive(:timeouts=) | ||
end | ||
|
||
describe "#disposable_mx_server?" do | ||
let(:disposable_email_address) { "[email protected]" } | ||
let(:disposable_mx_server) { ValidEmail2.disposable_emails.select { |domain| domain.count(".") == 1 }.sample } | ||
let(:disposable_email_instance) { described_class.new(disposable_email_address, dns_instance) } | ||
let(:mock_disposable_mx_records) { [double("MX", exchange: "mx.#{disposable_mx_server}", preference: 10, ttl:)] } | ||
|
||
before do | ||
allow(mock_resolv_dns).to receive(:getresources) | ||
.with(disposable_email_instance.address.domain, Resolv::DNS::Resource::IN::MX) | ||
.and_return(mock_disposable_mx_records) | ||
|
||
allow(mock_resolv_dns).to receive(:getresources) | ||
.with(email_instance.address.domain, Resolv::DNS::Resource::IN::MX) | ||
.and_return(mock_mx_records) | ||
end | ||
|
||
it "is false if the MX server is not in the disposable_emails list" do | ||
expect(email_instance).not_to be_disposable_mx_server | ||
end | ||
|
||
it "is true if the MX server is in the disposable_emails list" do | ||
expect(disposable_email_instance).to be_disposable_mx_server | ||
end | ||
|
||
it "is false and then true when the MX record changes from non-disposable to disposable" do | ||
allow(mock_resolv_dns).to receive(:getresources) | ||
.with(disposable_email_instance.address.domain, Resolv::DNS::Resource::IN::MX) | ||
.and_return(mock_mx_records) # non-disposable MX records | ||
|
||
expect(disposable_email_instance).not_to be_disposable_mx_server | ||
|
||
ValidEmail2::Dns.clear_cache | ||
|
||
allow(mock_resolv_dns).to receive(:getresources) | ||
.with(disposable_email_instance.address.domain, Resolv::DNS::Resource::IN::MX) | ||
.and_return(mock_disposable_mx_records) # disposable MX records | ||
|
||
expect(disposable_email_instance).to be_disposable_mx_server | ||
end | ||
end | ||
|
||
describe "#valid_strict_mx?" do | ||
let(:cached_at) { Time.now } | ||
let(:mock_cache_data) { { [email_instance.address.domain, Resolv::DNS::Resource::IN::MX] => ValidEmail2::Dns::CacheEntry.new(mock_mx_records, cached_at, ttl) } } | ||
|
@@ -251,7 +292,7 @@ | |
describe "#valid_mx?" do | ||
let(:cached_at) { Time.now } | ||
let(:mock_cache_data) { { [email_instance.address.domain, Resolv::DNS::Resource::IN::MX] => ValidEmail2::Dns::CacheEntry.new(mock_a_records, cached_at, ttl) } } | ||
let(:mock_a_records) { [double("A", address: "192.168.1.1", ttl: ttl)] } | ||
let(:mock_a_records) { [double("A", address: "192.168.1.1", ttl:)] } | ||
|
||
before do | ||
allow(email_instance).to receive(:mx_servers).and_return(mock_mx_records) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,14 +5,25 @@ | |
describe "Performance testing" do | ||
let(:disposable_domain) { ValidEmail2.disposable_emails.first } | ||
|
||
it "has acceptable lookup performance" do | ||
it "disposable_domain? has acceptable lookup performance" do | ||
address = ValidEmail2::Address.new("[email protected]") | ||
|
||
# preload list and check size | ||
expect(ValidEmail2.disposable_emails).to be_a(Set) | ||
expect(ValidEmail2.disposable_emails.count).to be > 30000 | ||
|
||
# check lookup timing | ||
expect { address.disposable_domain? }.to perform_under(0.0001).sample(10).times | ||
expect { address.disposable_domain? }.to perform_under(0.0001).sec.sample(10).times | ||
end | ||
|
||
it "disposable_mx_server? has acceptable lookup performance" do | ||
address = ValidEmail2::Address.new("[email protected]") | ||
|
||
# preload list and check size | ||
expect(ValidEmail2.disposable_emails).to be_a(Set) | ||
expect(ValidEmail2.disposable_emails.count).to be > 30000 | ||
|
||
# check lookup timing | ||
expect { address.disposable_mx_server? }.to perform_under(0.0001).sec.warmup(1).times.sample(10).times | ||
end | ||
end |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is not thread-safe, which might lead to multiple threads trying to calculate value for the same key. Probably not a big deal though.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If the inputs are the same (i.e., no edge case in DNS record change happening during execution), the result will be the same so it doesn't matter if two threads write at the same time... I think 🤔