From 43fe420b419dee4e760288761a45ba47eb28ab2e Mon Sep 17 00:00:00 2001 From: mo khan Date: Sat, 31 Jan 2026 23:57:00 -0700 Subject: 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 --- lib/net/hippie/connection_pool.rb | 59 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 59 insertions(+) create mode 100644 lib/net/hippie/connection_pool.rb (limited to 'lib/net/hippie/connection_pool.rb') diff --git a/lib/net/hippie/connection_pool.rb b/lib/net/hippie/connection_pool.rb new file mode 100644 index 0000000..2e220b7 --- /dev/null +++ b/lib/net/hippie/connection_pool.rb @@ -0,0 +1,59 @@ +# frozen_string_literal: true + +module Net + module Hippie + # Thread-safe connection pool with LRU eviction. + class ConnectionPool + def initialize(max_size: 100, dns_ttl: 300) + @max_size = max_size + @dns_ttl = dns_ttl + @connections = {} + @monitor = Monitor.new + end + + def checkout(key, &block) + reuse(key) || create(key, &block) + end + + def close_all + @monitor.synchronize do + @connections.each_value(&:close) + @connections.clear + end + end + + private + + def reuse(key) + @monitor.synchronize do + return nil unless @connections.key?(key) + + conn = @connections.delete(key) + return @connections[key] = conn unless conn.stale?(@dns_ttl) + + conn.close + nil + end + end + + def create(key) + conn = yield + @monitor.synchronize do + existing = reuse(key) + if existing + conn.close + return existing + end + evict_lru if @connections.size >= @max_size + @connections[key] = conn + end + end + + def evict_lru + key, conn = @connections.first + conn.close + @connections.delete(key) + end + end + end +end -- cgit v1.2.3