Skip to content

Commit c16f737

Browse files
committed
small internal refactors
1 parent 86bb4ba commit c16f737

File tree

14 files changed

+132
-67
lines changed

14 files changed

+132
-67
lines changed

src/api/constants.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,11 @@ export const CLIENT_USER_AGENT = LIB_NAME + '/' + LIB_VERSION;
2222
/**
2323
* @internal
2424
*/
25-
export const enum HTTP_METHODS {
26-
Get = 'GET',
27-
Post = 'POST',
28-
Delete = 'DELETE',
29-
}
25+
export const HttpMethods = {
26+
Get: 'GET',
27+
Post: 'POST',
28+
Delete: 'DELETE',
29+
} as const;
3030

3131
/**
3232
* @internal

src/api/data-api-http-client.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,15 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15-
import { DEFAULT_NAMESPACE, DEFAULT_TIMEOUT, HTTP_METHODS, HttpClient, RawDataApiResponse } from '@/src/api';
15+
import { DEFAULT_NAMESPACE, DEFAULT_TIMEOUT, HttpClient, HttpMethods, RawDataApiResponse } from '@/src/api';
1616
import { DataAPIResponseError, DataAPITimeout, mkRespErrorFromResponse, ObjectId, UUID } from '@/src/data-api';
1717
import { logger } from '@/src/logger';
1818
import {
1919
MkTimeoutError,
2020
MultiCallTimeoutManager,
2121
SingleCallTimeoutManager,
22-
TimeoutManager, TimeoutOptions,
22+
TimeoutManager,
23+
TimeoutOptions,
2324
} from '@/src/api/timeout-managers';
2425

2526
interface DataApiRequestInfo {
@@ -71,7 +72,7 @@ export class DataApiHttpClient extends HttpClient {
7172
url: url,
7273
data: JSON.stringify(info.command, replacer),
7374
timeoutManager: info.timeoutManager,
74-
method: HTTP_METHODS.Post,
75+
method: HttpMethods.Post,
7576
reviver: reviver,
7677
});
7778

@@ -109,7 +110,7 @@ const mkTimeoutManager = (constructor: new (maxMs: number, mkTimeoutError: MkTim
109110
}
110111

111112
const mkTimeoutErrorMaker = (timeout: number): MkTimeoutError => {
112-
return (info) => new DataAPITimeout(info.data!, timeout)
113+
return (info) => new DataAPITimeout(info.data!, timeout);
113114
}
114115

115116
const mkFauxErroredResponse = (message: string): RawDataApiResponse => {

src/api/devops-api-http-client.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,8 @@
1313
// limitations under the License.
1414

1515
import { HttpClient } from '@/src/api/http-client';
16-
import { HTTP_METHODS } from '@/src/api/constants';
1716
import { AxiosError, AxiosResponse } from 'axios';
18-
import { HTTPClientOptions } from '@/src/api/types';
17+
import { HTTPClientOptions, HttpMethodStrings } from '@/src/api/types';
1918
import { HTTP1AuthHeaderFactories, HTTP1Strategy } from '@/src/api/http1';
2019
import { DevopsApiResponseError, DevopsApiTimeout, DevopsUnexpectedStateError } from '@/src/devops/errors';
2120
import { AdminBlockingOptions, PollBlockingOptions } from '@/src/devops/types';
@@ -26,10 +25,11 @@ import {
2625
TimeoutManager,
2726
TimeoutOptions,
2827
} from '@/src/api/timeout-managers';
28+
import { HttpMethods } from '@/src/api/constants';
2929

3030
interface DevopsApiRequestInfo {
3131
path: string,
32-
method: HTTP_METHODS,
32+
method: HttpMethodStrings,
3333
data?: Record<string, any>,
3434
params?: Record<string, any>,
3535
}
@@ -86,7 +86,7 @@ export class DevopsApiHttpClient extends HttpClient {
8686
private async _awaitStatus(id: string, target: string, legalStates: string[], options: PollBlockingOptions | undefined, defaultPollInterval: number, timeoutManager: TimeoutManager): Promise<void> {
8787
for (;;) {
8888
const resp = await this.request({
89-
method: HTTP_METHODS.Get,
89+
method: HttpMethods.Get,
9090
path: `/databases/${id}`,
9191
}, {
9292
timeoutManager: timeoutManager,

src/api/timeout-managers.ts

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,13 @@
1414

1515
import { InternalHTTPRequestInfo } from '@/src/api/types';
1616

17+
/**
18+
* Internal representation of timeout options, allowing a shorthand for a single call timeout manager via
19+
* `maxTimeMS`, with an explicit {@link TimeoutManager} being allowed to be passed if necessary, generally
20+
* for multi-call timeout management.
21+
*
22+
* @internal
23+
*/
1724
export type TimeoutOptions = {
1825
maxTimeMS?: number,
1926
timeoutManager?: never,
@@ -22,13 +29,48 @@ export type TimeoutOptions = {
2229
maxTimeMS?: never,
2330
}
2431

32+
/**
33+
* Represents a function that creates a timeout error for a given request context.
34+
*
35+
* @example
36+
* ```typescript
37+
* const mkTimeoutError: MkTimeoutError = (info) => {
38+
*   return new DevopsApiTimeout(info.url, timeout);
39+
* }
40+
* ```
41+
*
42+
* @internal
43+
*/
2544
export type MkTimeoutError = (ctx: InternalHTTPRequestInfo) => Error;
2645

46+
/**
47+
* Represents a timeout manager, which is responsible for tracking the remaining time before a timeout occurs.
48+
*
49+
* See {@link SingleCallTimeoutManager} and {@link MultiCallTimeoutManager} for concrete implementations.
50+
*
51+
* @internal
52+
*/
2753
export interface TimeoutManager {
54+
/**
55+
* The remaining time before a timeout occurs, in milliseconds. May return `Infinity`, which
56+
* the underlying http request strategies should interpret as no timeout.
57+
*
58+
* However, the implementing TimeoutManager classes should accept `0` to also indicate no timeout,
59+
* just translating it to `Infinity` for the purposes of the `msRemaining` getter, so it's easier to
60+
* reason about.
61+
*/
2862
msRemaining: number,
63+
/**
64+
* A function that creates a timeout error for the given request context ({@link InternalHTTPRequestInfo}).
65+
*/
2966
mkTimeoutError: MkTimeoutError,
3067
}
3168

69+
/**
70+
* A basic timeout manager that only holds a fixed timeout value, intended for, of course, single-request operations.
71+
*
72+
* @internal
73+
*/
3274
export class SingleCallTimeoutManager implements TimeoutManager {
3375
public readonly msRemaining: number;
3476

@@ -37,6 +79,13 @@ export class SingleCallTimeoutManager implements TimeoutManager {
3779
}
3880
}
3981

82+
/**
83+
* A more complex timeout manager that tracks the remaining time for multiple calls, starting from the first call.
84+
* This is useful for scenarios where multiple calls are made in sequence, and the timeout should be shared among them,
85+
* e.g. {@link Collection.insertMany}.
86+
*
87+
* @internal
88+
*/
4089
export class MultiCallTimeoutManager implements TimeoutManager {
4190
private _deadline!: number;
4291
private _started: boolean;

src/api/types.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15-
import type { HTTP_METHODS } from '@/src/api/index';
1615
import { Caller } from '@/src/client';
1716
import { TimeoutManager } from '@/src/api/timeout-managers';
17+
import { HttpMethods } from '@/src/api/constants';
1818

1919
/**
2020
* @internal
@@ -44,14 +44,19 @@ export interface GuaranteedAPIResponse {
4444
status: number,
4545
}
4646

47+
/**
48+
* @internal
49+
*/
50+
export type HttpMethodStrings = typeof HttpMethods[keyof typeof HttpMethods];
51+
4752
/**
4853
* @internal
4954
*/
5055
export interface HTTPRequestInfo {
5156
url: string,
5257
data?: unknown,
5358
params?: Record<string, string>,
54-
method: HTTP_METHODS,
59+
method: HttpMethodStrings,
5560
reviver?: (key: string, value: any) => any,
5661
timeoutManager: TimeoutManager,
5762
}
@@ -61,7 +66,7 @@ export interface HTTPRequestInfo {
6166
*/
6267
export interface InternalHTTPRequestInfo extends HTTPRequestInfo {
6368
token: string,
64-
method: HTTP_METHODS,
69+
method: HttpMethodStrings,
6570
userAgent: string,
6671
}
6772

src/common/types.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,16 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15+
/**
16+
* Represents options related to timeouts. Note that this means "the max time the client will wait for a response
17+
* from the server"—**an operation timing out does not necessarily mean the operation failed on the server**.
18+
*
19+
* On paginated operations, the timeout applies across all network requests. For example, if you set a timeout of 5
20+
* seconds and the operation requires 3 network requests, each request must complete in less than 5 seconds total.
21+
*/
1522
export interface WithTimeout {
23+
/**
24+
* The maximum time to wait for a response from the server, in milliseconds.
25+
*/
1626
maxTimeMS?: number;
1727
}

0 commit comments

Comments
 (0)