@@ -4,6 +4,8 @@ import { type Client } from "./client";
44import { reuseInFlight } from "@cocalc/util/reuse-in-flight" ;
55import { getLogger } from "@cocalc/conat/client" ;
66
7+ const DEBUG = true ;
8+
79const logger = getLogger ( "conat:core:sticky" ) ;
810
911export 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
119137const stickyRequest = reuseInFlight (
0 commit comments