Skip to content

Commit 9df0a38

Browse files
committed
conat core sticky router -- add logging so we know what is going on
1 parent 331aa86 commit 9df0a38

File tree

4 files changed

+309
-3
lines changed

4 files changed

+309
-3
lines changed

src/packages/conat/core/sticky.ts

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ import { type Client } from "./client";
44
import { reuseInFlight } from "@cocalc/util/reuse-in-flight";
55
import { getLogger } from "@cocalc/conat/client";
66

7+
const DEBUG = true;
8+
79
const logger = getLogger("conat:core:sticky");
810

911
export function consistentHashingChoice(
@@ -85,9 +87,13 @@ export async function createStickyRouter({
8587
choiceTtl?: number;
8688
clientTtl?: number;
8789
}) {
90+
logger.debug("Creating Sticky Router: ", { choiceTtl, clientTtl });
91+
await client.waitUntilConnected();
8892
const sub = await client.subscribe(SUBJECT);
93+
logger.debug("Creating Sticky Router: subscription created");
8994
const stickyCache: { [key: string]: { target: string; expire: number } } = {};
9095

96+
let counter = 0;
9197
const handle = async (mesg) => {
9298
try {
9399
const { pattern, subject, targets } = mesg.data;
@@ -97,11 +103,24 @@ export async function createStickyRouter({
97103
pattern,
98104
subject,
99105
});
106+
let madeChoice = false;
100107
if (target == null || !targets.includes(target)) {
101108
// make a new choice
109+
madeChoice = true;
102110
target = consistentHashingChoice(targets, subject);
103111
stickyCache[key] = { target, expire: Date.now() + choiceTtl };
104112
}
113+
counter++;
114+
if (DEBUG) {
115+
logger.debug("handle request", {
116+
counter,
117+
madeChoice,
118+
pattern,
119+
subject,
120+
targets,
121+
target,
122+
});
123+
}
105124
await mesg.respond({ target, ttl: clientTtl });
106125
} catch (err) {
107126
logger.debug("WARNING: unable to handle routing message", err);
@@ -113,7 +132,6 @@ export async function createStickyRouter({
113132
}
114133
};
115134
listen();
116-
return sub;
117135
}
118136

119137
const stickyRequest = reuseInFlight(

src/packages/conat/package.json

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,17 @@
2424
"test": "pnpm exec jest",
2525
"depcheck": "pnpx depcheck --ignores events,bufferutil,utf-8-validate"
2626
},
27-
"files": ["dist/**", "README.md", "package.json"],
27+
"files": [
28+
"dist/**",
29+
"README.md",
30+
"package.json"
31+
],
2832
"author": "SageMath, Inc.",
29-
"keywords": ["utilities", "conat", "cocalc"],
33+
"keywords": [
34+
"utilities",
35+
"conat",
36+
"cocalc"
37+
],
3038
"license": "SEE LICENSE.md",
3139
"dependencies": {
3240
"@cocalc/comm": "workspace:*",
@@ -37,13 +45,15 @@
3745
"ascii-table3": "^1.0.1",
3846
"awaiting": "^3.0.0",
3947
"bufferutil": "^4.0.9",
48+
"build": "^0.1.4",
4049
"consistent-hash": "^1.2.2",
4150
"dayjs": "^1.11.11",
4251
"events": "3.3.0",
4352
"immutable": "^4.3.0",
4453
"js-base64": "^3.7.7",
4554
"json-stable-stringify": "^1.0.1",
4655
"lodash": "^4.17.21",
56+
"pnpm": "^10.17.1",
4757
"socket.io": "^4.8.1",
4858
"socket.io-client": "^4.8.1",
4959
"utf-8-validate": "^6.0.5"

0 commit comments

Comments
 (0)