-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path08-abort-and-retry.ts
More file actions
56 lines (50 loc) · 1.75 KB
/
08-abort-and-retry.ts
File metadata and controls
56 lines (50 loc) · 1.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
/*
* Example 08 — AbortSignal + custom retry policy.
*
* Purpose: demonstrate aborting a slow write via AbortSignal, and customizing the retry
* policy (switching to conservative mode + tightening backoff).
*
* Required env: GreptimeDB at localhost:4001.
*
* Run: pnpm example 08-abort-and-retry
*/
import { Client, DataType, Precision, Table } from '../src/index.js';
async function main(): Promise<void> {
const endpoint = process.env.GREPTIMEDB_ENDPOINT ?? 'localhost:4001';
const client = new Client(
Client.create(endpoint)
.withDatabase('public')
.withRetry({ mode: 'conservative', maxAttempts: 2, initialBackoffMs: 50, maxBackoffMs: 200 })
.build(),
);
const table = Table.new('abort_demo')
.addTagColumn('host', DataType.String)
.addFieldColumn('value', DataType.Float64)
.addTimestampColumn('ts', Precision.Millisecond);
for (let i = 0; i < 100; i++) {
table.addRow([`h-${i}`, i * 0.5, Date.now() + i]);
}
try {
// Scenario A: normal write with a per-call 5s deadline via AbortSignal
const ac1 = AbortSignal.timeout(5_000);
const res = await client.write(table, { signal: ac1 });
console.log(`wrote ${res.value} rows within 5s deadline`);
// Scenario B: force an abort during the call and observe it surface as AbortedError
const ac2 = new AbortController();
setTimeout(() => {
ac2.abort();
}, 1);
try {
await client.write(table, { signal: ac2.signal });
console.log('unexpected success');
} catch (err: unknown) {
console.log(`aborted as expected:`, err instanceof Error ? err.message : err);
}
} finally {
await client.close();
}
}
main().catch((err: unknown) => {
console.error('example failed:', err);
process.exit(1);
});