Skip to content

Cache configured Faraday connections per client - #12

Merged
atesgoral merged 1 commit into
mainfrom
ag/reuse-faraday-connections
Aug 25, 2026
Merged

atesgoral merged 1 commit into
mainfrom
ag/reuse-faraday-connections

Conversation

@atesgoral

Copy link
Copy Markdown
  • Followed the guidelines in the Contributing document
  • There is no other open pull request in this fork for the same change

Summary

  • cache one normal and one multipart Faraday connection per client
  • serialize first construction and fully build the middleware/adapter stack before concurrent requests can race Faraday's lazy builder
  • give duplicated beta and admin clients fresh connections and endpoint delegates
  • keep cached connection internals out of Client#inspect

Reason for Change

ShopifAi::Client is cached by Agent Server, but ShopifAi::HTTP#conn currently creates a new Faraday connection and middleware stack for every API call. That means a configured persistent adapter cannot retain its socket pool across requests.

This is the transport-policy-neutral foundation for shop/issues-sidekick#6707. It does not change the default Faraday adapter or add a dependency; a separate Agent Server PR can opt selected AI Proxy clients into net_http_persistent with bounded pool policy and rollout controls.

The base connection-reuse pattern is adapted from alexrudall/ruby-openai#636, with concurrency, duplicated-helper, and inspect-safety hardening. alexrudall/ruby-openai#509 provides the persistent-adapter precedent.

Compatibility

  • the default net_http request behavior is unchanged apart from avoiding repeated connection-object and middleware-stack construction
  • configured persistent adapters can now retain their pools on a cached client
  • beta and admin duplicates remain isolated and cannot retain helpers bound to the original credentials
  • Usage's per-call admin-client lifetime is unchanged; Agent Server does not use that API

Validation

  • BUNDLE_FROZEN=true bundle _2.4.5_ exec rake — 166 examples, 0 failures; 56 files, 0 RuboCop offenses
  • Faraday 1.10.4 compatibility smoke — normal/multipart reuse, eager stack construction, and duplicate isolation passed
  • live proxy.shopify.ai/ping proof with net_http_persistent — 176.4 ms cold, then 22.0–31.9 ms warm across four repeated calls on the same cached connection

PR authored by Qlaw

Co-authored-by: Chris Hasiński <krzysztof.hasinski@gmail.com>
Co-authored-by: Qlaw <noreply@qlaw.quick.shopify.io>
@atesgoral
atesgoral merged commit 6c27780 into main Aug 25, 2026
1 check passed
@atesgoral
atesgoral deleted the ag/reuse-faraday-connections branch August 25, 2026 21:15
@atesgoral atesgoral mentioned this pull request Aug 25, 2026
2 tasks
atesgoral added a commit that referenced this pull request Aug 25, 2026
* [x] Followed the guidelines in the [Contributing
document](../blob/main/CONTRIBUTING.md)
* [x] There is no other open pull request in this fork for the same
change

## Summary

- bump `ShopifAi::VERSION` from `9.0.0` to `9.1.0`
- update the path-gem version in `Gemfile.lock`
- release the connection-reuse changelog entry as `9.1.0`

## Reason for Change

[PR #12](#12) adds
backward-compatible connection lifecycle behavior: cached clients can
now retain configured persistent Faraday adapters across requests. A
minor version communicates that new capability more accurately than a
patch release.

Agent Server consumes this fork by Git tag. After this PR merges, the
release will be completed with an annotated `v9.1.0` tag and GitHub
Release. The fork will not be published to public RubyGems.

## Validation

- `BUNDLE_FROZEN=true bundle _2.4.5_ exec rake` — 166 examples, 0
failures; 56 files, 0 RuboCop offenses
- `git diff --check`

---
*PR authored by Qlaw*

Co-authored-by: Qlaw <noreply@qlaw.quick.shopify.io>
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.

2 participants