Skip to content

feat(http): add configurable retry middleware - #222

Merged
Vincent Biret (baywet) merged 10 commits into
microsoft:mainfrom
Daiyuanranfeng:feat/retry-handler
Nov 13, 2026
Merged

Vincent Biret (baywet) merged 10 commits into
microsoft:mainfrom
Daiyuanranfeng:feat/retry-handler

Conversation

@Daiyuanranfeng

@Daiyuanranfeng Ziqiang Zhang (Daiyuanranfeng) commented Nov 12, 2026 •

Copy link
Copy Markdown
Contributor

Closes #126. Refs #136. Supersedes #150 at the maintainer's invitation.

Adds a Kiota-owned retry handler to the default HTTP pipeline, with configurable retry policy, bounded exponential delays, and HTTP-date/delay-seconds Retry-After support.

Continues voidstack (@voidstackloop)'s implementation from #150, preserving the original commits and authorship. Discarded readable response bodies are drained and closed, including on read failure. Retry-count exhaustion raises a dedicated RetryExhaustedError containing a message and no retained response history; delay-budget exhaustion returns the final response to its caller. The handler preserves upload stream positions, avoids replaying non-seekable bodies, and bypasses parallel requests and response-streaming callbacks.

Validation on Windows for the latest change:

  • HTTP RSpec: 124 examples, 0 failures on MRI 3.4.9 and JRuby 10.1.2.0, each with Faraday 2.15.1 and the minimum supported Faraday 2.7.2.
  • RuboCop: 194 files, no offenses.
  • git diff --check: clean.
  • Regression coverage includes response cleanup without retained history, retry-count reporting, stream read failures, upload offsets, non-seekable bodies, and delay-budget boundaries.

Other OS/runtime combinations are left to upstream CI. CHANGELOG is managed by release-please and is not modified.

Latest review follow-up: drain response streams in 16 KiB chunks without retaining contents. Four regressions cover multiple chunks, nil/empty EOF, and a read failure after one successful chunk; the existing ensure still closes the stream. All 124 HTTP examples pass locally on MRI 3.4.9 and JRuby 10.1.2.0 with Faraday 2.15.1 and 2.7.2. Latest remote CI is reported separately.

- add a peer RetryHandlerOption class so retry behavior follows the
  same request-context option pattern as the other Kiota handlers,
  instead of unreachable constructor kwargs
- add YARD doc comments to the class and its public method
- narrow the Retry-After rescue to just the HTTP-date parse and
  explain why a malformed header falls back to backoff instead of
  raising
- add coverage for exponential backoff, retry exhaustion,
  non-retryable status codes, and malformed Retry-After values,
  plus validation tests for RetryHandlerOption's bounds

Co-Authored-By: Claude Sonnet 5 <[email protected]>
Addresses the remaining review feedback on PR microsoft#150:
- scope the default RetryHandlerOption to the handler instance instead of
  a class-level singleton, and accept one via the constructor
- move the retryable-status check onto RetryHandlerOption as a
  configurable should_retry predicate instead of hardcoding it
- raise RetryExhaustedError (carrying every retryable response) once the
  retry budget is exhausted, instead of silently returning the last
  failed response
- add a retries_time_limit option bounding cumulative retry delay
- build a fresh env per retry instead of mutating the original request
  headers/body in place
- read the retryable response body before each retry

Co-Authored-By: Claude Sonnet 5 <[email protected]>
Co-authored-by: Copilot Autofix powered by AI <[email protected]>
Copilot AI balanced review requested due to automatic review settings November 12, 2026 08:48

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

Retries can replay different bytes from streams initially positioned past zero, leaving Content-Length inconsistent, and delay-budget checks can suppress the expected RetryExhaustedError after retries are exhausted.

2 open issues
What changed in this PR

Adds configurable retries to Kiota Ruby’s default HTTP pipeline, addressing HTTP-date Retry-After support in #126 and advancing the middleware work in #136.

Changes:

  • Add configurable retry policies, bounded backoff, and response history on exhaustion.
  • Handle response-stream cleanup and bypass retries for non-replayable requests.
  • Document configuration and add unit and regression coverage.
File Description
README.md Documents retry settings, exhaustion, and replay limits.
components/​http/​spec/​retry_handler_spec.rb Tests configuration, delays, policies, and pipeline integration.
components/​http/​spec/​retry_handler_regression_spec.rb Covers stream cleanup, replay, date boundaries, and delay caps.
components/​http/​lib/​microsoft_kiota_faraday/​middleware/​retry_handler.rb Implements retries, delay calculation, and response cleanup.
components/​http/​lib/​microsoft_kiota_faraday/​middleware/​retry_handler_option.rb Defines retry defaults, limits, and custom policies.
components/​http/​lib/​microsoft_kiota_faraday/​middleware/​retry_exhausted_error.rb Exposes response history when retries are exhausted.
components/​http/​lib/​microsoft_kiota_faraday/​kiota_client_factory.rb Adds retries to the default middleware pipeline.
components/​http/​lib/​microsoft_kiota_faraday.rb Loads the new retry classes.

Review effort: Balanced

Copilot AI balanced review requested due to automatic review settings November 12, 2026 09:07

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 Approval recommended

No active review issues block the retry middleware changes.

No open issues 🎉

2 resolved since last review

Review effort: Balanced

Copilot AI balanced review requested due to automatic review settings November 13, 2026 06:32

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔵 Needs a closer look

The retry handler drains response streams with an unbounded read, causing potentially large transient allocations on every retry; it should read bounded chunks while preserving guaranteed closure.

No open issues 🎉

1 previously missed issue

In code that hasn't changed since last review

Medium severity Drain response bodies in bounded chunks

components/​http/​lib/​microsoft_kiota_faraday/​middleware/​retry_handler.rb:120

Calling read without a length materializes the entire remaining response body before discarding it. For large streamed error responses, this defeats streaming and can cause a large transient allocation on every retry (a 1 MiB stream produces a single 1 MiB read here). Drain in bounded chunks while retaining the ensure that closes the stream.

Review effort: Balanced

Copilot AI balanced review requested due to automatic review settings November 13, 2026 12:17

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔵 Needs a closer look

Human assessment is needed because redirect-handler buffering can allow non-seekable uploads to be retried despite the documented guarantee.

No open issues 🎉

Review effort: Balanced

@baywet
Vincent Biret (baywet) added this pull request to the merge queue Nov 13, 2026
Merged via the queue into microsoft:main with commit 50de2c1 Nov 13, 2026
120 checks passed
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.

http - RetryHandler MUST respect retry-after: <date>

4 participants