socket-function 0.12.1 → 0.12.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json
CHANGED
package/src/CallFactory.ts
CHANGED
|
@@ -16,7 +16,10 @@ import pako from "pako";
|
|
|
16
16
|
import { setFlag } from "../require/compileFlags";
|
|
17
17
|
setFlag(require, "pako", "allowclient", true);
|
|
18
18
|
|
|
19
|
-
|
|
19
|
+
// NOTE: If it is too low, and too many servers disconnect, we can easily spend 100% of our time
|
|
20
|
+
// trying to reconnect.
|
|
21
|
+
// (Or... maybe the delay is just waiting, and we aren't actually overloading the server?)
|
|
22
|
+
const MIN_RETRY_DELAY = 5000;
|
|
20
23
|
|
|
21
24
|
type InternalCallType = FullCallType & {
|
|
22
25
|
seqNum: number;
|
|
@@ -91,7 +91,11 @@ export class JSONLACKS {
|
|
|
91
91
|
if (extendedParsing) {
|
|
92
92
|
obj = measureBlock(function JSONextendedParse() { return parser.parse(text); });
|
|
93
93
|
} else {
|
|
94
|
-
|
|
94
|
+
try {
|
|
95
|
+
obj = measureBlock(function JSONparse() { return JSON.parse(text); });
|
|
96
|
+
} catch {
|
|
97
|
+
obj = measureBlock(function JSONextendedParse() { return parser.parse(text); });
|
|
98
|
+
}
|
|
95
99
|
}
|
|
96
100
|
|
|
97
101
|
return JSONLACKS.hydrateSpecialObjects(obj, hydrateState, config) as T;
|
package/src/batching.ts
CHANGED
|
@@ -29,6 +29,8 @@ export function delay(delayTime: DelayType): Promise<void> {
|
|
|
29
29
|
// NOTE: setTimeout can't wait this short of a time, so just setImmediate. This should be hard to distinguish
|
|
30
30
|
// anyways, as setImmediate (at least in nodejs), should happen after io, so... it should just work
|
|
31
31
|
// (the only difference is there will be less unnecessary delay).
|
|
32
|
+
// NOTE: THIS DOES break certain cases where io is depending on true delay, and by only waiting a microtick
|
|
33
|
+
// we don't give it a chance. But... we should just handle those cases explicitly, via an explicit "afterio".
|
|
32
34
|
if (delayTime < 10) {
|
|
33
35
|
return delay("immediate");
|
|
34
36
|
}
|
|
@@ -68,7 +70,7 @@ export function batchFunction<Arg, Result = void>(
|
|
|
68
70
|
let curDelay = config.delay;
|
|
69
71
|
let delayRamp = 0;
|
|
70
72
|
if (config.throttleWindow && typeof curDelay === "number") {
|
|
71
|
-
delayRamp = curDelay / config.throttleWindow;
|
|
73
|
+
delayRamp = curDelay / (config.throttleWindow / curDelay);
|
|
72
74
|
}
|
|
73
75
|
let delayTime = 0;
|
|
74
76
|
if (typeof curDelay === "number") {
|
|
@@ -80,6 +82,10 @@ export function batchFunction<Arg, Result = void>(
|
|
|
80
82
|
return arg => {
|
|
81
83
|
let now = Date.now();
|
|
82
84
|
if (delayRamp) {
|
|
85
|
+
// The time since the last call (started) is how much budget we will have received to
|
|
86
|
+
// run values. If it is === delayTime, then we subtract 1, as we are right on track.
|
|
87
|
+
// If it is > delayTime, then we are running below the rate, so it is fine.
|
|
88
|
+
// If it is < delayTime, we are running too fast, and have to slow down.
|
|
83
89
|
let savedCount = (now - lastCall) / delayTime;
|
|
84
90
|
if (savedCount >= 1) {
|
|
85
91
|
countSinceBreak -= savedCount;
|
package/src/profiling/measure.ts
CHANGED
|
@@ -6,7 +6,7 @@ import { getOpenTimesBase, getOwnTime, OwnTimeObj } from "./getOwnTime";
|
|
|
6
6
|
import { addToStats, addToStatsValue, createStatsValue, getStatsTop, StatsValue } from "./stats";
|
|
7
7
|
import { white } from "../formatting/logColors";
|
|
8
8
|
import { isNode } from "../misc";
|
|
9
|
-
import { formatStats } from "./statsFormat";
|
|
9
|
+
import { formatStats, percent } from "./statsFormat";
|
|
10
10
|
|
|
11
11
|
let measurementsDisabled = false;
|
|
12
12
|
/** NOTE: Must be called BEFORE anything else is imported!
|
|
@@ -73,13 +73,21 @@ export function measureBlock<T extends (...args: any[]) => any>(fnc: T, name?: s
|
|
|
73
73
|
return measureWrap(fnc, name)();
|
|
74
74
|
}
|
|
75
75
|
|
|
76
|
+
let extraInfoGetters: (() => string | undefined)[] = [];
|
|
77
|
+
export function registerMeasureInfo(getInfo: () => string | undefined) {
|
|
78
|
+
extraInfoGetters.push(getInfo);
|
|
79
|
+
}
|
|
80
|
+
|
|
76
81
|
export function startMeasure(): {
|
|
77
82
|
finish: () => MeasureProfile;
|
|
78
83
|
} {
|
|
79
84
|
if (!measurementsEnabled && !measurementsDisabled) {
|
|
80
85
|
console.warn(red(`To capture measurements enableMeasurements() must be called before any other imports in your entry point`));
|
|
81
86
|
}
|
|
87
|
+
let now = Date.now();
|
|
82
88
|
let profile: MeasureProfile = {
|
|
89
|
+
startTime: now,
|
|
90
|
+
endTime: now,
|
|
83
91
|
entries: Object.create(null),
|
|
84
92
|
};
|
|
85
93
|
let openAtStart = new Set(getOpenTimesBase());
|
|
@@ -103,6 +111,7 @@ export function startMeasure(): {
|
|
|
103
111
|
addToProfile(profile, timeObj);
|
|
104
112
|
}
|
|
105
113
|
outstandingProfiles.splice(outstandingProfiles.indexOf(profile), 1);
|
|
114
|
+
profile.endTime = Date.now();
|
|
106
115
|
return profile;
|
|
107
116
|
}
|
|
108
117
|
};
|
|
@@ -111,6 +120,7 @@ export function startMeasure(): {
|
|
|
111
120
|
export interface LogMeasureTableConfig {
|
|
112
121
|
useTotalTime?: boolean;
|
|
113
122
|
name?: string;
|
|
123
|
+
setTitle?: boolean;
|
|
114
124
|
// Defaults to 0.05
|
|
115
125
|
thresholdInTable?: number;
|
|
116
126
|
// Details to 50
|
|
@@ -157,8 +167,19 @@ export function logMeasureTable(
|
|
|
157
167
|
entries.sort((a, b) => getTime(b).sum - getTime(a).sum);
|
|
158
168
|
}
|
|
159
169
|
|
|
170
|
+
let timeRunFor = profile.endTime - profile.startTime;
|
|
171
|
+
let fraction = totalTime / timeRunFor;
|
|
172
|
+
|
|
160
173
|
console.log();
|
|
161
|
-
let
|
|
174
|
+
let extraInfos = extraInfoGetters.map(x => x());
|
|
175
|
+
|
|
176
|
+
if (config?.setTitle && isNode()) {
|
|
177
|
+
let title = `${percent(fraction)} CPU`;
|
|
178
|
+
title += extraInfos.map(x => x ? ` // ${x}` : "").join("");
|
|
179
|
+
process.stdout.write(`\x1b]0;${title}\x07`);
|
|
180
|
+
}
|
|
181
|
+
let pid = isNode() ? `(${process.pid}) ` : "";
|
|
182
|
+
let title = yellow(`${pid}Profiled ${formatTime(totalTime)} (${percent(fraction)} CPU)${extraInfos.map(x => x ? ` (${x})` : "")} (logged at ${new Date().toISOString()}, profile for ${formatTime(timeRunFor)})`);
|
|
162
183
|
if (name) {
|
|
163
184
|
title = `(${blue(name)}) ${title}`;
|
|
164
185
|
}
|
|
@@ -229,12 +250,17 @@ function finishProfile(measure: { finish(): MeasureProfile }, config?: LogMeasur
|
|
|
229
250
|
|
|
230
251
|
|
|
231
252
|
export interface MeasureProfile {
|
|
253
|
+
startTime: number;
|
|
254
|
+
endTime: number;
|
|
232
255
|
entries: {
|
|
233
256
|
[name: string]: ProfileEntry;
|
|
234
257
|
};
|
|
235
258
|
}
|
|
236
259
|
export function createMeasureProfile(): MeasureProfile {
|
|
260
|
+
let now = Date.now();
|
|
237
261
|
return {
|
|
262
|
+
startTime: now,
|
|
263
|
+
endTime: now,
|
|
238
264
|
entries: Object.create(null),
|
|
239
265
|
};
|
|
240
266
|
}
|
package/src/profiling/stats.ts
CHANGED
|
@@ -10,6 +10,7 @@ export function percent(value: number) {
|
|
|
10
10
|
export function formatStats(stats: StatsValue, config?: {
|
|
11
11
|
noColor?: boolean;
|
|
12
12
|
noSum?: boolean;
|
|
13
|
+
noSpaces?: boolean;
|
|
13
14
|
}) {
|
|
14
15
|
function p(count: number, text: string | number) {
|
|
15
16
|
return String(text).padStart(count, " ");
|
|
@@ -37,5 +38,8 @@ export function formatStats(stats: StatsValue, config?: {
|
|
|
37
38
|
}
|
|
38
39
|
equation = (!config?.noSum && `${p(6, sumText)} = ` || "") + `${p(6, topPart)} + ${bottomPart}`;
|
|
39
40
|
}
|
|
41
|
+
if (config?.noSpaces) {
|
|
42
|
+
equation = equation.replace(/\s+/g, " ").trim();
|
|
43
|
+
}
|
|
40
44
|
return equation;
|
|
41
45
|
}
|