Repository navigation
feat(pubsub): add HedgingOptions validation and token bucket #9463
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
feywind
wants to merge
1
commit into
pubsub/hedging-gax-abort
from
pubsub/hedging-options-and-token-bucket
+615
−6
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,250 @@ | ||
| /*! | ||
| * Copyright 2026 Google LLC | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| import {CallOptions} from 'google-gax'; | ||
| import {defaultOptions} from '../default-options'; | ||
| import {Duration} from '../temporal'; | ||
|
|
||
| /** | ||
| * Minimum allowed hedging delay (100ms). | ||
| */ | ||
| const MIN_HEDGE_DELAY = Duration.from({milliseconds: 100}); | ||
|
|
||
| /** | ||
| * Maximum allowed hedging delay (10s). | ||
| */ | ||
| const MAX_HEDGE_DELAY = Duration.from({seconds: 10}); | ||
|
|
||
| /** | ||
| * Minimum allowed maxTokens value. | ||
| */ | ||
| const MIN_MAX_TOKENS = 1; | ||
|
|
||
| /** | ||
| * Maximum allowed maxTokens value. | ||
| */ | ||
| const MAX_MAX_TOKENS = 250; | ||
|
|
||
| /** | ||
| * Minimum allowed refill ratio. | ||
| */ | ||
| const MIN_REFILL_RATIO = 0.001; | ||
|
|
||
| /** | ||
| * Maximum allowed refill ratio. | ||
| */ | ||
| const MAX_REFILL_RATIO = 0.2; | ||
|
|
||
| /** | ||
| * Default initial RPC timeout for Publish calls (from publisher_client_config.json). | ||
| */ | ||
| const DEFAULT_INITIAL_RPC_TIMEOUT = Duration.from({seconds: 60}); | ||
|
|
||
| /** | ||
| * Default total timeout for Publish calls (from publisher_client_config.json). | ||
| */ | ||
| const DEFAULT_TOTAL_TIMEOUT = Duration.from({seconds: 600}); | ||
|
|
||
| /** | ||
| * Scale factor to represent decimal token values (e.g. 0.1 refill ratio) as | ||
| * integers inside the token bucket. A scale of 1000 allows representing decimal | ||
| * ratios down to 0.001 (1.0 logical token = 1000). | ||
| */ | ||
| const HEDGE_TOKEN_SCALE = 1000; | ||
|
|
||
| /** | ||
| * Settings for configuring publish hedging. | ||
| */ | ||
| export interface HedgingOptions { | ||
| /** | ||
| * Delay before sending a hedged (backup) publish RPC. | ||
| * Default: 1000ms. Must satisfy 100ms <= hedgeDelay <= 10s. | ||
| */ | ||
| hedgeDelay?: Duration; | ||
|
|
||
| /** | ||
| * Maximum number of tokens in the token bucket rate limiter. | ||
| * Default: 50. Must satisfy 0 < maxTokens <= 250 (integer). | ||
| */ | ||
| maxTokens?: number; | ||
|
|
||
| /** | ||
| * Token bucket refill ratio per successful publish batch. | ||
| * Default: 0.1. Must satisfy 0.001 <= refillRatio <= 0.2. | ||
| */ | ||
| refillRatio?: number; | ||
| } | ||
|
|
||
| /** | ||
| * Validated and defaulted hedging configuration. | ||
| */ | ||
| export interface ResolvedHedgingOptions { | ||
| readonly hedgeDelay: Duration; | ||
| readonly maxTokens: number; | ||
| readonly refillRatio: number; | ||
| } | ||
|
|
||
| /** | ||
| * Fixed-point token bucket rate limiter for publish hedging. | ||
| * | ||
| * Starts empty (0 tokens), refills by `refillRatio` on each successful publish | ||
| * batch (capped at `maxTokens`), and requires 1.0 token per hedged attempt. | ||
| */ | ||
| export class HedgingTokenBucket { | ||
| private hedgeTokenBucket = 0; | ||
| private readonly scaledMaxHedgeTokens: number; | ||
| private readonly scaledHedgeRefillAmount: number; | ||
|
|
||
| constructor(options: ResolvedHedgingOptions) { | ||
| this.scaledMaxHedgeTokens = options.maxTokens * HEDGE_TOKEN_SCALE; | ||
| this.scaledHedgeRefillAmount = Math.round( | ||
| options.refillRatio * HEDGE_TOKEN_SCALE, | ||
| ); | ||
| } | ||
|
|
||
| /** | ||
| * Resolves the effective initial RPC timeout Duration from CallOptions. | ||
| */ | ||
| static resolveInitialRpcTimeout(gaxOpts?: CallOptions): Duration { | ||
| const initialRpcTimeoutMillis = | ||
| gaxOpts?.retry?.backoffSettings?.initialRpcTimeoutMillis ?? | ||
| gaxOpts?.timeout; | ||
| if (typeof initialRpcTimeoutMillis === 'number') { | ||
| return Duration.from({milliseconds: initialRpcTimeoutMillis}); | ||
| } | ||
| return DEFAULT_INITIAL_RPC_TIMEOUT; | ||
| } | ||
|
|
||
| /** | ||
| * Resolves the effective total timeout Duration from CallOptions. | ||
| */ | ||
| static resolveTotalTimeout(gaxOpts?: CallOptions): Duration { | ||
| const totalTimeoutMillis = | ||
| gaxOpts?.retry?.backoffSettings?.totalTimeoutMillis ?? gaxOpts?.timeout; | ||
| if (typeof totalTimeoutMillis === 'number') { | ||
| return Duration.from({milliseconds: totalTimeoutMillis}); | ||
| } | ||
| return DEFAULT_TOTAL_TIMEOUT; | ||
| } | ||
|
|
||
| /** | ||
| * Validates user-supplied HedgingOptions, applies defaults, and verifies | ||
| * compatibility with messageOrdering and RPC/total timeouts. | ||
| */ | ||
| static validateAndResolveHedgingOptions( | ||
| hedging?: HedgingOptions, | ||
| messageOrdering?: boolean, | ||
| gaxOpts?: CallOptions, | ||
| ): ResolvedHedgingOptions | undefined { | ||
| if (!hedging) { | ||
| return undefined; | ||
| } | ||
|
|
||
| if (messageOrdering) { | ||
| throw new Error( | ||
| 'Publish hedging and message ordering cannot be enabled at the same time.', | ||
| ); | ||
| } | ||
|
|
||
| const hedgeDelay = hedging.hedgeDelay | ||
| ? Duration.from(hedging.hedgeDelay) | ||
| : defaultOptions.publish.hedging.hedgeDelay; | ||
|
|
||
| if ( | ||
| Duration.compare(hedgeDelay, MIN_HEDGE_DELAY) < 0 || | ||
| Duration.compare(hedgeDelay, MAX_HEDGE_DELAY) > 0 | ||
| ) { | ||
| throw new RangeError( | ||
| 'hedgeDelay must be greater than or equal to 100ms and less than or equal to 10s', | ||
| ); | ||
| } | ||
|
|
||
| const maxTokens = | ||
| hedging.maxTokens ?? defaultOptions.publish.hedging.maxTokens; | ||
| if ( | ||
| !Number.isInteger(maxTokens) || | ||
| maxTokens < MIN_MAX_TOKENS || | ||
| maxTokens > MAX_MAX_TOKENS | ||
| ) { | ||
| throw new RangeError( | ||
| 'maxTokens must be greater than 0 and less than or equal to 250', | ||
| ); | ||
| } | ||
|
|
||
| const refillRatio = | ||
| hedging.refillRatio ?? defaultOptions.publish.hedging.refillRatio; | ||
| if ( | ||
| typeof refillRatio !== 'number' || | ||
| Number.isNaN(refillRatio) || | ||
| refillRatio < MIN_REFILL_RATIO || | ||
| refillRatio > MAX_REFILL_RATIO | ||
| ) { | ||
| throw new RangeError( | ||
| `refillRatio must be greater than or equal to ${MIN_REFILL_RATIO} and less than or equal to ${MAX_REFILL_RATIO}`, | ||
| ); | ||
| } | ||
|
|
||
| const initialRpcTimeout = | ||
| HedgingTokenBucket.resolveInitialRpcTimeout(gaxOpts); | ||
| if (Duration.compare(hedgeDelay, initialRpcTimeout) >= 0) { | ||
| throw new RangeError( | ||
| `hedgeDelay (${hedgeDelay.milliseconds}ms) must be strictly less than the initial RPC timeout duration (${initialRpcTimeout.milliseconds}ms)`, | ||
| ); | ||
| } | ||
|
|
||
| const totalTimeout = HedgingTokenBucket.resolveTotalTimeout(gaxOpts); | ||
| if (Duration.compare(hedgeDelay, totalTimeout) >= 0) { | ||
| throw new RangeError( | ||
| `hedgeDelay (${hedgeDelay.milliseconds}ms) must be strictly less than the total timeout duration (${totalTimeout.milliseconds}ms)`, | ||
| ); | ||
| } | ||
|
|
||
| return { | ||
| hedgeDelay, | ||
| maxTokens, | ||
| refillRatio, | ||
| }; | ||
| } | ||
|
|
||
| /** | ||
| * Refills the token bucket after a successful publish batch. | ||
| */ | ||
| refillTokenBucket(): void { | ||
| this.hedgeTokenBucket = Math.min( | ||
| this.scaledMaxHedgeTokens, | ||
| this.hedgeTokenBucket + this.scaledHedgeRefillAmount, | ||
| ); | ||
| } | ||
|
|
||
| /** | ||
| * Attempts to consume 1.0 token for a hedged request. | ||
| * Returns true if a token was available and consumed, false otherwise. | ||
| */ | ||
| tryAcquireHedgeToken(): boolean { | ||
| if (this.hedgeTokenBucket < HEDGE_TOKEN_SCALE) { | ||
| return false; | ||
| } | ||
| this.hedgeTokenBucket -= HEDGE_TOKEN_SCALE; | ||
| return true; | ||
| } | ||
|
|
||
| /** | ||
| * Returns the current logical token balance (for testing/inspection). | ||
| */ | ||
| getTokenBalance(): number { | ||
| return this.hedgeTokenBucket / HEDGE_TOKEN_SCALE; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.