Skip to content

Cache Faraday connection and auth tokens for improved performance#37

Open
stevenou wants to merge 2 commits intogbaptista:mainfrom
InvisibleCommerce:caching
Open

Cache Faraday connection and auth tokens for improved performance#37
stevenou wants to merge 2 commits intogbaptista:mainfrom
InvisibleCommerce:caching

Conversation

@stevenou
Copy link
Copy Markdown
Contributor

@stevenou stevenou commented Dec 2, 2025

Summary

  • Memoize Faraday connection to reuse HTTP connections across requests (eliminates TLS handshake overhead per request)
  • Use apply! instead of fetch_access_token! to leverage googleauth's built-in token caching (only refreshes when token is missing or expires within 60 seconds)

Problem

The current implementation has two performance issues:

  1. Creates a new Faraday connection on every request (TLS handshake overhead)
  2. Calls fetch_access_token! on every request (unnecessary network call to Google's auth service)

Solution

  1. Connection memoization: The Faraday connection is now cached in @connection and reused across all requests from a single client instance
  2. Token caching: Using @authorizer.apply!(request.headers) instead of manually setting the Authorization header with fetch_access_token!. The googleauth gem's apply! method has built-in token caching.

Test plan

  • Specs pass
  • Test with service account credentials to verify token caching works
  • Test with default credentials (ADC)
  • Test with API key (no auth changes needed)
  • Verify SSE streaming still works correctly

🤖 Generated with Claude Code

stevenou and others added 2 commits December 1, 2025 18:03
- Memoize Faraday connection to reuse HTTP connections across requests
- Use apply! instead of fetch_access_token! to leverage googleauth's
  built-in token caching (only refreshes when token is missing or
  expires within 60 seconds)
- Bump version to 4.3.0

This eliminates unnecessary per-request overhead:
- No longer creates a new Faraday connection on every request
- No longer fetches a new auth token on every request

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant