-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathheadroom.ts
More file actions
162 lines (152 loc) · 5.34 KB
/
headroom.ts
File metadata and controls
162 lines (152 loc) · 5.34 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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
export type TransactionMetrics = {
bytesRead: number;
bytesWritten: number;
databaseQueries: number;
documentsRead: number;
documentsWritten: number;
functionsScheduled: number;
scheduledFunctionArgsBytes: number;
};
const MiB = 1 << 20;
const DEFAULT_TRANSACTION_LIMITS: TransactionMetrics = {
bytesRead: 16 * MiB,
bytesWritten: 16 * MiB,
documentsRead: 32_000,
databaseQueries: 4_096,
documentsWritten: 16_000,
functionsScheduled: 1000,
scheduledFunctionArgsBytes: 16 * MiB,
};
export class HeadroomTracker {
private _metrics: TransactionMetrics;
private _limits: TransactionMetrics;
private _enforceLimits: boolean;
constructor(limits: Partial<TransactionMetrics> | boolean) {
this._metrics = {
bytesRead: 0,
bytesWritten: 0,
documentsRead: 0,
documentsWritten: 0,
databaseQueries: 0,
functionsScheduled: 0,
scheduledFunctionArgsBytes: 0,
};
if (limits === false) {
this._enforceLimits = false;
this._limits = DEFAULT_TRANSACTION_LIMITS;
} else if (limits === true) {
this._enforceLimits = true;
this._limits = DEFAULT_TRANSACTION_LIMITS;
} else {
this._enforceLimits = true;
this._limits = {
...DEFAULT_TRANSACTION_LIMITS,
...limits,
};
}
}
trackRead(docSizeBytes: number) {
this._metrics.bytesRead += docSizeBytes;
this._metrics.documentsRead += 1;
if (this._enforceLimits) {
if (this._metrics.bytesRead > this._limits.bytesRead) {
throw new Error(
`Read too much data in a single function execution (limit: ${this._limits.bytesRead} bytes). ` +
`This is a Convex limit: https://docs.convex.dev/production/state/limits`,
);
}
if (this._metrics.documentsRead > this._limits.documentsRead) {
throw new Error(
`Scanned too many documents in a single function execution (limit: ${this._limits.documentsRead}). ` +
`This is a Convex limit: https://docs.convex.dev/production/state/limits`,
);
}
}
}
trackWrite(docSizeBytes: number) {
this._metrics.bytesWritten += docSizeBytes;
this._metrics.documentsWritten += 1;
if (this._enforceLimits) {
if (this._metrics.bytesWritten > this._limits.bytesWritten) {
throw new Error(
`Wrote too much data in a single function execution (limit: ${this._limits.bytesWritten} bytes). ` +
`This is a Convex limit: https://docs.convex.dev/production/state/limits`,
);
}
if (this._metrics.documentsWritten > this._limits.documentsWritten) {
throw new Error(
`Wrote too many documents in a single function execution (limit: ${this._limits.documentsWritten}). ` +
`This is a Convex limit: https://docs.convex.dev/production/state/limits`,
);
}
}
}
trackIndexRange() {
this._metrics.databaseQueries += 1;
if (this._enforceLimits) {
if (this._metrics.databaseQueries > this._limits.databaseQueries) {
throw new Error(
`Too many index ranges read in a single function execution (limit: ${this._limits.databaseQueries}). ` +
`This is a Convex limit: https://docs.convex.dev/production/state/limits`,
);
}
}
}
trackScheduledFunction(argsSizeBytes: number) {
this._metrics.functionsScheduled += 1;
this._metrics.scheduledFunctionArgsBytes += argsSizeBytes;
if (this._enforceLimits) {
if (this._metrics.functionsScheduled > this._limits.functionsScheduled) {
throw new Error(
`Scheduled too many functions in a single function execution (limit: ${this._limits.functionsScheduled}). ` +
`This is a Convex limit: https://docs.convex.dev/production/state/limits`,
);
}
if (
this._metrics.scheduledFunctionArgsBytes >
this._limits.scheduledFunctionArgsBytes
) {
throw new Error(
`Scheduled function arguments too large in a single function execution (limit: ${this._limits.scheduledFunctionArgsBytes} bytes). ` +
`This is a Convex limit: https://docs.convex.dev/production/state/limits`,
);
}
}
}
getTransactionHeadroom() {
return {
bytesRead: {
used: this._metrics.bytesRead,
remaining: this._limits.bytesRead - this._metrics.bytesRead,
},
bytesWritten: {
used: this._metrics.bytesWritten,
remaining: this._limits.bytesWritten - this._metrics.bytesWritten,
},
databaseQueries: {
used: this._metrics.databaseQueries,
remaining: this._limits.databaseQueries - this._metrics.databaseQueries,
},
documentsRead: {
used: this._metrics.documentsRead,
remaining: this._limits.documentsRead - this._metrics.documentsRead,
},
documentsWritten: {
used: this._metrics.documentsWritten,
remaining:
this._limits.documentsWritten - this._metrics.documentsWritten,
},
functionsScheduled: {
used: this._metrics.functionsScheduled,
remaining:
this._limits.functionsScheduled - this._metrics.functionsScheduled,
},
scheduledFunctionArgsBytes: {
used: this._metrics.scheduledFunctionArgsBytes,
remaining:
this._limits.scheduledFunctionArgsBytes -
this._metrics.scheduledFunctionArgsBytes,
},
};
}
}