diff options
| author | mo khan <mo@mokhan.ca> | 2025-07-06 13:54:36 -0600 |
|---|---|---|
| committer | mo khan <mo@mokhan.ca> | 2025-07-06 13:54:36 -0600 |
| commit | d36b6e4f7c99b96aee01656e2ca57312d77a55b6 (patch) | |
| tree | e875bc14c907cbca92a8c36d9f15d4c946fceed0 /lib/net/hippie/connection.rb | |
| parent | 6ef050083b8519cfb8120246344514e1c8e27f49 (diff) | |
feat: add optional Rust backend with Magnus integration
- 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 <noreply@anthropic.com>
Diffstat (limited to 'lib/net/hippie/connection.rb')
| -rw-r--r-- | lib/net/hippie/connection.rb | 39 |
1 files changed, 39 insertions, 0 deletions
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) |
