Skip to content

Commit a231ec9

Browse files
committed
feat: make retry strategies strictly typed for all resource types
1 parent dbe2ccc commit a231ec9

File tree

5 files changed

+111
-38
lines changed

5 files changed

+111
-38
lines changed

packages/cli/src/constructs/check-group-v1.ts

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import { ApiCheckDefaultConfig } from './api-check'
99
import type { Region } from '..'
1010
import { type Frequency } from './frequency'
1111
import {
12-
type RetryStrategy,
1312
// eslint-disable-next-line @typescript-eslint/no-unused-vars
1413
type RetryStrategyBuilder, // Used for @links in comments.
1514
} from './retry-strategy'
@@ -28,6 +27,8 @@ import { PrivateLocationGroupAssignment } from './private-location-group-assignm
2827
import { Ref } from './ref'
2928
import { Session } from './project'
3029
import { validateDeprecatedDoubleCheck } from './internal/common-diagnostics'
30+
import { CheckRetryStrategy } from './check'
31+
import { MonitorRetryStrategy } from './monitor'
3132

3233
const defaultApiCheckDefaults: ApiCheckDefaultConfig = {
3334
headers: [],
@@ -53,6 +54,13 @@ type MultiStepCheckConfig = CheckConfigDefaults & {
5354
testMatch: string | string[],
5455
}
5556

57+
/**
58+
* Retry strategies supported by groups.
59+
*/
60+
export type GroupRetryStrategy =
61+
| CheckRetryStrategy
62+
| MonitorRetryStrategy
63+
5664
export interface CheckGroupV1Props {
5765
/**
5866
* The name of the check group.
@@ -236,7 +244,7 @@ export interface CheckGroupV1Props {
236244
*
237245
* If not set, retries are disabled for all checks in the group.
238246
*/
239-
retryStrategy?: RetryStrategy
247+
retryStrategy?: GroupRetryStrategy
240248

241249
/**
242250
* Determines whether the checks in the group should run on all selected
@@ -298,7 +306,7 @@ export class CheckGroupV1 extends Construct {
298306
apiCheckDefaults: ApiCheckDefaultConfig
299307
browserChecks?: BrowserCheckConfig
300308
multiStepChecks?: MultiStepCheckConfig
301-
retryStrategy?: RetryStrategy
309+
retryStrategy?: GroupRetryStrategy
302310
runParallel?: boolean
303311
alertSettings?: AlertEscalation
304312
useGlobalAlertSettings?: boolean

packages/cli/src/constructs/check-group-v2.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import { PrivateLocation, PrivateLocationRef } from './private-location'
22
import type { Region } from '..'
33
import {
4-
type RetryStrategy,
54
// eslint-disable-next-line @typescript-eslint/no-unused-vars
65
type RetryStrategyBuilder, // Used for @links in comments.
76
} from './retry-strategy'
@@ -11,7 +10,7 @@ import {
1110
type AlertEscalationBuilder, // Used for @links in comments.
1211
} from './alert-escalation-policy'
1312
import { Diagnostics } from './diagnostics'
14-
import { CheckGroupV1, CheckGroupV1Props } from './check-group-v1'
13+
import { CheckGroupV1, CheckGroupV1Props, GroupRetryStrategy } from './check-group-v1'
1514
import { validateRemovedDoubleCheck } from './internal/common-diagnostics'
1615

1716
export interface CheckGroupV2Props extends Omit<CheckGroupV1Props, 'alertEscalationPolicy'> {
@@ -73,7 +72,7 @@ export interface CheckGroupV2Props extends Omit<CheckGroupV1Props, 'alertEscalat
7372
*
7473
* If not set, individual check settings are used.
7574
*/
76-
retryStrategy?: RetryStrategy
75+
retryStrategy?: GroupRetryStrategy
7776

7877
/**
7978
* Determines whether the checks in the group should run on all selected

packages/cli/src/constructs/check.ts

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,29 @@ import type { Region } from '..'
99
import type { CheckGroupV1, CheckGroupV2, CheckGroupRef } from './check-group'
1010
import { PrivateLocation, PrivateLocationRef } from './private-location'
1111
import { PrivateLocationCheckAssignment } from './private-location-check-assignment'
12-
import { RetryStrategy } from './retry-strategy'
12+
import {
13+
ExponentialRetryStrategy,
14+
FixedRetryStrategy,
15+
LinearRetryStrategy,
16+
NoRetriesRetryStrategy,
17+
SingleRetryStrategy,
18+
} from './retry-strategy'
1319
import { AlertEscalation } from './alert-escalation-policy'
1420
import { IncidentTrigger } from './incident'
1521
import { ConfigDefaultsGetter, makeConfigDefaultsGetter } from './check-config'
1622
import { Diagnostics } from './diagnostics'
1723
import { validateDeprecatedDoubleCheck } from './internal/common-diagnostics'
1824

25+
/**
26+
* Retry strategies supported by checks.
27+
*/
28+
export type CheckRetryStrategy =
29+
| LinearRetryStrategy
30+
| ExponentialRetryStrategy
31+
| FixedRetryStrategy
32+
| SingleRetryStrategy
33+
| NoRetriesRetryStrategy
34+
1935
/**
2036
* Base configuration properties for all check types.
2137
* These properties are inherited by ApiCheck, BrowserCheck, and other check types.
@@ -194,7 +210,7 @@ export interface CheckProps {
194210
* })
195211
* ```
196212
*/
197-
retryStrategy?: RetryStrategy
213+
retryStrategy?: CheckRetryStrategy
198214

199215
/**
200216
* Determines whether the check should run on all selected locations in parallel or round-robin.
@@ -227,7 +243,7 @@ export abstract class Check extends Construct {
227243
groupId?: Ref
228244
alertChannels?: Array<AlertChannel|AlertChannelRef>
229245
testOnly?: boolean
230-
retryStrategy?: RetryStrategy
246+
retryStrategy?: CheckRetryStrategy
231247
alertSettings?: AlertEscalation
232248
useGlobalAlertSettings?: boolean
233249
runParallel?: boolean

packages/cli/src/constructs/monitor.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import { CheckGroupV2 } from './check-group-v2'
77
import { Frequency } from './frequency'
88
import { IncidentTrigger } from './incident'
99
import { PrivateLocation, PrivateLocationRef } from './private-location'
10-
import { RetryStrategyType, SingleRetryStrategy } from './retry-strategy'
10+
import { NoRetriesRetryStrategy, RetryStrategyType, SingleRetryStrategy } from './retry-strategy'
1111
import { Check, CheckProps } from './check'
1212
import { Diagnostics } from './diagnostics'
1313
import { validateRemovedDoubleCheck } from './internal/common-diagnostics'
@@ -16,7 +16,9 @@ import { InvalidPropertyValueDiagnostic } from './construct-diagnostics'
1616
/**
1717
* Retry strategies supported by monitors.
1818
*/
19-
export type MonitorRetryStrategy = SingleRetryStrategy
19+
export type MonitorRetryStrategy =
20+
| SingleRetryStrategy
21+
| NoRetriesRetryStrategy
2022

2123
export interface MonitorProps extends Omit<CheckProps, 'doubleCheck'> {
2224
/**

packages/cli/src/constructs/retry-strategy.ts

Lines changed: 75 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@
22
export type RetryStrategyType = 'LINEAR' | 'EXPONENTIAL' | 'FIXED' | 'SINGLE' | 'NO_RETRIES'
33

44
/**
5-
* Configuration for check retry behavior.
6-
* Defines how and when to retry failed checks before marking them as permanently failed.
5+
* Configuration for check and monitor retry behavior.
6+
* Defines how and when to retry failed checks or monitors before marking
7+
* them as permanently failed.
78
*/
89
export interface RetryStrategy {
910
/** The retry strategy type */
@@ -68,9 +69,45 @@ export interface RetryStrategy {
6869
export type RetryStrategyOptions = Pick<RetryStrategy, 'baseBackoffSeconds' | 'maxRetries' | 'maxDurationSeconds' | 'sameRegion'>
6970

7071
/**
71-
* Configuration for single retry behavior.
72+
* Configuration for linear retry strategies.
7273
*/
73-
export interface SingleRetryStrategy extends Pick<RetryStrategy, 'baseBackoffSeconds' | 'sameRegion'> {
74+
export interface LinearRetryStrategy extends RetryStrategy {
75+
type: 'LINEAR'
76+
}
77+
78+
/**
79+
* Options for configuring linear retry strategy behavior.
80+
*/
81+
export type LinearRetryStrategyOptions = RetryStrategyOptions
82+
83+
/**
84+
* Configuration for exponential retry strategies.
85+
*/
86+
export interface ExponentialRetryStrategy extends RetryStrategy {
87+
type: 'EXPONENTIAL'
88+
}
89+
90+
/**
91+
* Options for configuring exponential retry strategy behavior.
92+
*/
93+
export type ExponentialRetryStrategyOptions = RetryStrategyOptions
94+
95+
/**
96+
* Configuration for fixed retry strategies.
97+
*/
98+
export interface FixedRetryStrategy extends RetryStrategy {
99+
type: 'FIXED'
100+
}
101+
102+
/**
103+
* Options for configuring fixed retry strategy behavior.
104+
*/
105+
export type FixedRetryStrategyOptions = RetryStrategyOptions
106+
107+
/**
108+
* Configuration for single retry strategies.
109+
*/
110+
export interface SingleRetryStrategy extends Pick<RetryStrategy, 'type' | 'baseBackoffSeconds' | 'sameRegion'> {
74111
type: 'SINGLE'
75112
}
76113

@@ -79,6 +116,13 @@ export interface SingleRetryStrategy extends Pick<RetryStrategy, 'baseBackoffSec
79116
*/
80117
export type SingleRetryStrategyOptions = Pick<RetryStrategyOptions, 'baseBackoffSeconds' | 'sameRegion'>
81118

119+
/**
120+
* Configuration for no retry retry strategies.
121+
*/
122+
export interface NoRetriesRetryStrategy extends Pick<RetryStrategy, 'type'> {
123+
type: 'NO_RETRIES'
124+
}
125+
82126
/**
83127
* Builder class for creating retry strategies.
84128
* Provides convenient methods to create different types of retry strategies.
@@ -121,8 +165,11 @@ export class RetryStrategyBuilder {
121165
/**
122166
* Each retry is run with the same backoff between attempts.
123167
*/
124-
static fixedStrategy (options?: RetryStrategyOptions): RetryStrategy {
125-
return RetryStrategyBuilder.retryStrategy('FIXED', options)
168+
static fixedStrategy (options?: FixedRetryStrategyOptions): FixedRetryStrategy {
169+
return {
170+
type: 'FIXED',
171+
...RetryStrategyBuilder.defaults(options),
172+
}
126173
}
127174

128175
/**
@@ -131,8 +178,11 @@ export class RetryStrategyBuilder {
131178
* The delay between retries is calculated using `baseBackoffSeconds * attempt`.
132179
* For example, retries will be run with a backoff of 10s, 20s, 30s, and so on.
133180
*/
134-
static linearStrategy (options?: RetryStrategyOptions): RetryStrategy {
135-
return RetryStrategyBuilder.retryStrategy('LINEAR', options)
181+
static linearStrategy (options?: LinearRetryStrategyOptions): LinearRetryStrategy {
182+
return {
183+
type: 'LINEAR',
184+
...RetryStrategyBuilder.defaults(options),
185+
}
136186
}
137187

138188
/**
@@ -141,38 +191,36 @@ export class RetryStrategyBuilder {
141191
* The delay between retries is calculated using `baseBackoffSeconds ^ attempt`.
142192
* For example, retries will be run with a backoff of 10s, 100s, 1000s, and so on.
143193
*/
144-
static exponentialStrategy (options?: RetryStrategyOptions): RetryStrategy {
145-
return RetryStrategyBuilder.retryStrategy('EXPONENTIAL', options)
194+
static exponentialStrategy (options?: ExponentialRetryStrategyOptions): ExponentialRetryStrategy {
195+
return {
196+
type: 'EXPONENTIAL',
197+
...RetryStrategyBuilder.defaults(options),
198+
}
146199
}
147200

148201
/**
149202
* A single retry will be performed.
150203
*/
151-
static singleRetry (options?: SingleRetryStrategyOptions): RetryStrategy {
152-
const {
153-
// eslint-disable-next-line @typescript-eslint/no-unused-vars
154-
maxRetries,
155-
// eslint-disable-next-line @typescript-eslint/no-unused-vars
156-
maxDurationSeconds,
157-
...strategy
158-
} = RetryStrategyBuilder.retryStrategy('SINGLE', {
159-
baseBackoffSeconds: options?.baseBackoffSeconds,
160-
sameRegion: options?.sameRegion,
161-
})
162-
163-
return strategy
204+
static singleRetry (options?: SingleRetryStrategyOptions): SingleRetryStrategy {
205+
const { baseBackoffSeconds, sameRegion } = RetryStrategyBuilder.defaults(options)
206+
return {
207+
type: 'SINGLE',
208+
baseBackoffSeconds,
209+
sameRegion,
210+
}
164211
}
165212

166213
/**
167214
* No retries are performed.
168215
*/
169-
static noRetries (): RetryStrategy {
170-
return RetryStrategyBuilder.retryStrategy('NO_RETRIES')
216+
static noRetries (): NoRetriesRetryStrategy {
217+
return {
218+
type: 'NO_RETRIES'
219+
}
171220
}
172221

173-
private static retryStrategy (type: RetryStrategyType, options?: RetryStrategyOptions): RetryStrategy {
222+
private static defaults (options?: RetryStrategyOptions): RetryStrategyOptions {
174223
return {
175-
type,
176224
baseBackoffSeconds: options?.baseBackoffSeconds ?? RetryStrategyBuilder.DEFAULT_BASE_BACKOFF_SECONDS,
177225
maxRetries: options?.maxRetries ?? RetryStrategyBuilder.DEFAULT_MAX_RETRIES,
178226
maxDurationSeconds: options?.maxDurationSeconds ?? RetryStrategyBuilder.DEFAULT_MAX_DURATION_SECONDS,

0 commit comments

Comments
 (0)