Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions handwritten/pubsub/src/default-options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,9 @@ export const defaultOptions = {
maxDelayMillis: 10,

hedging: {
// Whether publish hedging is enabled when hedging options are provided.
enabled: true,

// The default delay before sending a hedged publish RPC.
hedgeDelay: Duration.from({milliseconds: 1000}),

Expand Down
13 changes: 13 additions & 0 deletions handwritten/pubsub/src/publisher/hedging.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,12 @@ const HEDGE_TOKEN_SCALE = 1000;
* Settings for configuring publish hedging.
*/
export interface HedgingOptions {
/**
* Whether publish hedging is enabled.
* Default: true when a HedgingOptions object is provided.
*/
enabled?: boolean;

/**
* Delay before sending a hedged (backup) publish RPC.
* Default: 1000ms. Must satisfy 100ms <= hedgeDelay <= 10s.
Expand All @@ -103,6 +109,7 @@ export interface HedgingOptions {
* Validated and defaulted hedging configuration.
*/
export interface ResolvedHedgingOptions {
readonly enabled?: boolean;

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.

medium

Since validateAndResolveHedgingOptions always returns { enabled: true, ... } when hedging is enabled, and returns undefined otherwise, the enabled property in ResolvedHedgingOptions will always be present and true. It should be defined as a required boolean (readonly enabled: boolean;) rather than optional to avoid unnecessary null/undefined checks for consumers of ResolvedHedgingOptions.

Suggested change
readonly enabled?: boolean;
readonly enabled: boolean;

readonly hedgeDelay: Duration;
readonly maxTokens: number;
readonly refillRatio: number;
Expand Down Expand Up @@ -164,6 +171,11 @@ export class HedgingTokenBucket {
return undefined;
}

const enabled = hedging.enabled ?? defaultOptions.publish.hedging.enabled;
if (!enabled) {
return undefined;
}

if (messageOrdering) {
throw new Error(
'Publish hedging and message ordering cannot be enabled at the same time.',
Expand Down Expand Up @@ -224,6 +236,7 @@ export class HedgingTokenBucket {
}

return {
enabled: true,
hedgeDelay,
maxTokens,
refillRatio,
Expand Down
8 changes: 8 additions & 0 deletions handwritten/pubsub/system-test/hedging.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,14 @@ describe('Publish Hedging System Tests', () => {

assert.strictEqual(topic.publisher.tokenBucket?.getTokenBalance(), 1.0);

topic.setPublishOptions({
batching: {maxMessages: 1},
hedging: {
enabled: false,
},
});
assert.strictEqual(topic.publisher.tokenBucket, undefined);

const received = await waitForMessages(subscription, {
count: 6,
timeoutMs: 20000,
Expand Down
18 changes: 18 additions & 0 deletions handwritten/pubsub/test/publisher/hedging.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,27 @@ describe('Publisher Hedging', () => {
);
});

it('returns undefined when hedging.enabled is false', () => {
assert.strictEqual(
HedgingTokenBucket.validateAndResolveHedgingOptions({enabled: false}),
undefined,
);
assert.strictEqual(
HedgingTokenBucket.validateAndResolveHedgingOptions(
{enabled: false},
true,
),
undefined,
);
});

it('applies default settings when an empty hedging object is provided', () => {
const resolved = HedgingTokenBucket.validateAndResolveHedgingOptions({});
assert.ok(resolved);
assert.strictEqual(
resolved.enabled,
defaultOptions.publish.hedging.enabled,
);
assert.strictEqual(
resolved.hedgeDelay.milliseconds,
defaultOptions.publish.hedging.hedgeDelay.milliseconds,
Expand Down
Loading