diff options
| author | mo khan <mo@mokhan.ca> | 2026-01-31 23:57:00 -0700 |
|---|---|---|
| committer | mo khan <mo@mokhan.ca> | 2026-01-31 23:57:00 -0700 |
| commit | 43fe420b419dee4e760288761a45ba47eb28ab2e (patch) | |
| tree | 8278476993599682af7489193d6b514056917775 /lib/net/hippie/dns_cache.rb | |
| parent | b8c1171c332a574c7c0a68538471daf82c386867 (diff) | |
feat: add connection pooling and DNS caching for performance
- Persistent HTTP sessions avoid Connection: close overhead
- DNS pre-resolution with timeout prevents indefinite hangs
- Thread-safe connection pool with LRU eviction
- TLS certificates parsed once at init, not per-request
- Extract TlsParser, DnsCache, ConnectionPool for SRP
Bump to v1.5.0
Diffstat (limited to 'lib/net/hippie/dns_cache.rb')
| -rw-r--r-- | lib/net/hippie/dns_cache.rb | 45 |
1 files changed, 45 insertions, 0 deletions
diff --git a/lib/net/hippie/dns_cache.rb b/lib/net/hippie/dns_cache.rb new file mode 100644 index 0000000..bbf74f8 --- /dev/null +++ b/lib/net/hippie/dns_cache.rb @@ -0,0 +1,45 @@ +# frozen_string_literal: true + +module Net + module Hippie + # Thread-safe DNS resolution cache with TTL. + class DnsCache + def initialize(timeout: 5, ttl: 300, logger: nil) + @timeout = timeout + @ttl = ttl + @logger = logger + @cache = {} + @monitor = Monitor.new + end + + def resolve(hostname) + cached = get(hostname) + return cached if cached + + ip = Net::Hippie.resolve(hostname, timeout: @timeout) + set(hostname, ip) + ip + rescue Timeout::Error, Resolv::ResolvError => error + @logger&.warn("[Hippie] DNS resolution failed for #{hostname}: #{error.message}") + nil + end + + def clear + @monitor.synchronize { @cache.clear } + end + + private + + def get(hostname) + @monitor.synchronize do + entry = @cache[hostname] + entry[:ip] if entry && (Time.now - entry[:time]) < @ttl + end + end + + def set(hostname, ip_addr) + @monitor.synchronize { @cache[hostname] = { ip: ip_addr, time: Time.now } } + end + end + end +end |
