@@ -11,7 +11,6 @@ import * as math from 'lib0/math'
11
11
import * as protocol from './protocol.js'
12
12
import * as env from 'lib0/environment'
13
13
import * as logging from 'lib0/logging'
14
- import * as time from 'lib0/time'
15
14
16
15
const logWorker = logging . createModuleLogger ( '@y/redis/api/worker' )
17
16
// const logApi = logging.createModuleLogger('@y/redis/api')
@@ -280,6 +279,7 @@ export class Api {
280
279
} )
281
280
}
282
281
tasks . length > 0 && logWorker ( 'Accepted tasks ' , { tasks } )
282
+ let reclaimCounts = 0
283
283
await promise . all ( tasks . map ( async task => {
284
284
const streamlen = await this . redis . xLen ( task . stream )
285
285
if ( streamlen === 0 ) {
@@ -289,6 +289,7 @@ export class Api {
289
289
. exec ( )
290
290
logWorker ( 'Stream still empty, removing recurring task from queue ' , { stream : task . stream } )
291
291
} else {
292
+ reclaimCounts ++
292
293
const { room, docid } = decodeRedisRoomStreamName ( task . stream , this . prefix )
293
294
const { ydoc, storeReferences, redisLastId } = await this . getDoc ( room , docid )
294
295
const lastId = math . max ( number . parseInt ( redisLastId . split ( '-' ) [ 0 ] ) , number . parseInt ( task . id . split ( '-' ) [ 0 ] ) )
@@ -321,7 +322,7 @@ export class Api {
321
322
ydoc . destroy ( )
322
323
}
323
324
} ) )
324
- return tasks
325
+ return { tasks, reclaimCounts }
325
326
}
326
327
327
328
async destroy ( ) {
@@ -353,14 +354,17 @@ export class Worker {
353
354
this . client = client
354
355
logWorker ( 'Created worker process ' , { id : client . consumername , prefix : client . prefix , minMessageLifetime : client . redisMinMessageLifetime } )
355
356
; ( async ( ) => {
356
- const startRedisTime = await client . redis . time ( )
357
- const timeDiff = startRedisTime . getTime ( ) - time . getUnixTime ( )
357
+ let prev = performance . now ( )
358
358
while ( ! client . _destroyed ) {
359
359
try {
360
- const tasks = await client . consumeWorkerQueue ( opts )
361
- if ( tasks . length === 0 || ( client . redisMinMessageLifetime > time . getUnixTime ( ) + timeDiff - number . parseInt ( tasks [ 0 ] . id . split ( '-' ) [ 0 ] ) ) ) {
360
+ const { reclaimCounts } = await client . consumeWorkerQueue ( opts )
361
+ const now = performance . now ( )
362
+ if ( reclaimCounts === 0 ) {
362
363
await promise . wait ( client . redisWorkerTimeout )
364
+ } else if ( now - prev < client . redisWorkerTimeout ) {
365
+ await promise . wait ( client . redisWorkerTimeout - ( now - prev ) )
363
366
}
367
+ prev = now
364
368
} catch ( e ) {
365
369
console . error ( e )
366
370
}
0 commit comments