Skip to content

Add percentage-based brownout strategy - #10

Open
rflavien wants to merge 1 commit into
mainfrom
feature/percentage-brownout
Open

rflavien wants to merge 1 commit into
mainfrom
feature/percentage-brownout

Conversation

@rflavien

Copy link
Copy Markdown
Contributor

Summary

  • Add PercentagePhase class for probabilistic request rejection during brownouts
  • Support type: percentage in phase configuration with reject_percentage (1-100)
  • Existing type: cron behavior unchanged (backward compatible, defaults to cron)
  • Mixed strategies supported (cron and percentage phases in same strategy)

Configuration example

api_deprecation:
    brownout_strategies:
        progressive:
            phases:
                - starts_before: "30 days"
                  type: percentage
                  reject_percentage: 5
                - starts_before: "7 days"
                  type: percentage
                  reject_percentage: 50
                - starts_before: "1 day"
                  type: cron
                  cron: "* * * * *"
                  duration: 55

Test plan

  • Percentage phase rejects when random <= threshold
  • Percentage phase allows when random > threshold
  • Percentage phase at exact threshold boundary
  • Percentage phase not active before startsBefore
  • Full reject (100%) and minimal reject (1%) edge cases
  • Mixed cron/percentage strategies work correctly
  • Property-based boundary testing for all percentages 1-100
  • Mixed phase sorting is idempotent (property-based)
  • Configuration validation for both types
  • Existing cron-based tests unchanged and passing
  • PHPStan passes at max level
  • Infection 100% MSI on Brownout directory

Closes #10

🤖 Generated with Claude Code

Introduce PercentagePhase alongside existing cron-based Phase to support
probabilistic request rejection during brownout windows. Configuration
uses `type: percentage` with `reject_percentage` (1-100), while existing
`type: cron` behavior remains unchanged for backward compatibility.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Copilot AI review requested due to automatic review settings March 13, 2026 11:57

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Pull request overview

Adds a new brownout “percentage” phase type to support probabilistic request rejection as an alternative (or complement) to cron-window-based brownouts, while keeping existing cron behavior as the default.

Changes:

  • Introduces PercentagePhase and updates brownout evaluation to reject based on random_int(1, 100) <= reject_percentage when a percentage phase is active.
  • Extends bundle configuration to support type: percentage with reject_percentage (1–100) and validates required options per type.
  • Adds/extends tests covering percentage-phase behavior and mixed cron/percentage strategies (plus sorting idempotence with mixed types).

Reviewed changes

Copilot reviewed 7 out of 7 changed files in this pull request and generated 3 comments.

Show a summary per file
File Description
src/Brownout/PercentagePhase.php New phase model for percentage-based rejection.
src/Brownout/BrownoutChecker.php Evaluates percentage phases via RNG; adds overrideable randomInt() for test determinism.
src/Brownout/Strategy.php Broadens strategy phases to allow `Phase
src/ApiDeprecationBundle.php Builds strategies from config, supporting type: percentage and mixed phase lists.
config/definition.php Adds phase type and reject_percentage, plus conditional validation per type.
tests/Brownout/BrownoutCheckerTest.php Adds deterministic tests for percentage-phase boundaries and mixed strategies.
tests/ApiDeprecationBundleTest.php Adds property-based test for mixed-phase sorting idempotence.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

You can also share your feedback on Copilot code review. Take the survey.

Comment thread config/definition.php
->end()
->validate()
->ifTrue(function (array $v): bool {
return $v['type'] === 'cron' && ($v['cron'] === null || $v['duration'] === null);
Comment on lines +47 to +61
\assert(\is_int($phaseConfig['reject_percentage']));

$phases[] = new PercentagePhase(
$interval,
$phaseConfig['reject_percentage'],
);
} else {
\assert(\is_string($phaseConfig['cron']));
\assert(\is_int($phaseConfig['duration']));

$phases[] = new Phase(
$interval,
$phaseConfig['cron'],
$phaseConfig['duration'],
);
Comment thread config/definition.php
Comment on lines +51 to 62
->validate()
->ifTrue(function (array $v): bool {
return $v['type'] === 'cron' && ($v['cron'] === null || $v['duration'] === null);
})
->thenInvalid('Phase type "cron" requires "cron" and "duration" options.')
->end()
->validate()
->ifTrue(function (array $v): bool {
return $v['type'] === 'percentage' && $v['reject_percentage'] === null;
})
->thenInvalid('Phase type "percentage" requires "reject_percentage" option.')
->end()
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