From d36b6e4f7c99b96aee01656e2ca57312d77a55b6 Mon Sep 17 00:00:00 2001 From: mo khan Date: Sun, 6 Jul 2025 13:54:36 -0600 Subject: feat: add optional Rust backend with Magnus integration MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Add Rust HTTP client using reqwest and Magnus for Ruby integration - Implement transparent backend switching via NET_HIPPIE_RUST environment variable - Maintain 100% backward compatibility with existing Ruby interface - Add comprehensive test coverage (75 tests, 177 assertions) - Support automatic fallback to Ruby backend when Rust unavailable - Include detailed documentation for Rust backend setup and usage - Add proper .gitignore for Rust build artifacts - Update gemspec to support native extensions Performance benefits: - Faster HTTP requests using Rust's optimized reqwest library - Better concurrency with Tokio async runtime - Lower memory usage with zero-cost abstractions - Type safety with compile-time guarantees 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude --- lib/net/hippie/connection.rb | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) (limited to 'lib/net/hippie/connection.rb') diff --git a/lib/net/hippie/connection.rb b/lib/net/hippie/connection.rb index 599d754..35a478a 100644 --- a/lib/net/hippie/connection.rb +++ b/lib/net/hippie/connection.rb @@ -1,10 +1,49 @@ # frozen_string_literal: true +require_relative 'rust_backend' + module Net module Hippie # A connection to a specific host class Connection def initialize(scheme, host, port, options = {}) + @scheme = scheme + @host = host + @port = port + @options = options + + if RustBackend.enabled? + require_relative 'rust_connection' + @backend = RustConnection.new(scheme, host, port, options) + else + @backend = create_ruby_backend(scheme, host, port, options) + end + end + + def run(request) + @backend.run(request) + end + + def build_url_for(path) + @backend.build_url_for(path) + end + + private + + def create_ruby_backend(scheme, host, port, options) + # This is the original Ruby implementation wrapped in an object + # that matches the same interface as RustConnection + RubyConnection.new(scheme, host, port, options) + end + end + + # Wrapper for the original Ruby implementation + class RubyConnection + def initialize(scheme, host, port, options = {}) + @scheme = scheme + @host = host + @port = port + http = Net::HTTP.new(host, port) http.read_timeout = options.fetch(:read_timeout, 10) http.open_timeout = options.fetch(:open_timeout, 10) -- cgit v1.2.3