summaryrefslogtreecommitdiff
path: root/lib/net/hippie/connection_pool.rb
diff options
context:
space:
mode:
authormo khan <mo@mokhan.ca>2026-01-31 23:57:00 -0700
committermo khan <mo@mokhan.ca>2026-01-31 23:57:00 -0700
commit43fe420b419dee4e760288761a45ba47eb28ab2e (patch)
tree8278476993599682af7489193d6b514056917775 /lib/net/hippie/connection_pool.rb
parentb8c1171c332a574c7c0a68538471daf82c386867 (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/connection_pool.rb')
-rw-r--r--lib/net/hippie/connection_pool.rb59
1 files changed, 59 insertions, 0 deletions
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