Skip to content

Commit c39f90d

Browse files
committed
feat: use ttl to remove inactive room
1 parent ec69cf4 commit c39f90d

File tree

2 files changed

+11
-12
lines changed

2 files changed

+11
-12
lines changed

src/api.js

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ if (ydocUpdateCallback != null && ydocUpdateCallback.slice(-1) !== '/') {
2020
ydocUpdateCallback += '/'
2121
}
2222
const WORKER_DISABLED = env.getConf('y-worker-disabled') === 'true'
23+
const ROOM_STREAM_TTL = number.parseInt(env.getConf('y-room-stream-ttl') || '300')
2324

2425
/**
2526
* @param {string} a
@@ -125,14 +126,18 @@ export class Api {
125126
this.persistWorker = null
126127

127128
const addScript = WORKER_DISABLED
128-
? 'redis.call("XADD", KEYS[1], "*", "m", ARGV[1])'
129+
? `
130+
redis.call("XADD", KEYS[1], "*", "m", ARGV[1])
131+
redis.call("EXPIRE", KEYS[1], ${ROOM_STREAM_TTL})
132+
`
129133
: `
130134
if redis.call("EXISTS", KEYS[1]) == 0 then
131135
redis.call("XADD", "${this.redisWorkerStreamName}", "*", "compact", KEYS[1])
132136
elseif redis.call("XLEN", KEYS[1]) > 100 then
133137
redis.call("SADD", "${this.prefix}:worker:checklist", KEYS[1])
134138
end
135139
redis.call("XADD", KEYS[1], "*", "m", ARGV[1])
140+
redis.call("EXPIRE", KEYS[1], ${ROOM_STREAM_TTL})
136141
`
137142

138143
this.redis = redis.createClient({
@@ -294,20 +299,15 @@ export class Api {
294299
/**
295300
* @param {string} room
296301
* @param {string} docid
297-
* @param {boolean} [remove=false]
298302
*/
299-
async trimRoomStream (room, docid, remove = false) {
303+
async trimRoomStream (room, docid) {
300304
const roomName = computeRedisRoomStreamName(room, docid, this.prefix)
301305
const redisLastId = await this.getRedisLastId(room, docid)
302306
const lastId = number.parseInt(redisLastId.split('-')[0])
303-
if (remove) {
304-
await this.redis.del(roomName)
305-
} else {
306-
await this.redis.multi()
307-
.xTrim(roomName, 'MINID', lastId - this.redisMinMessageLifetime)
308-
.xDelIfEmpty(roomName)
309-
.exec()
310-
}
307+
await this.redis.multi()
308+
.xTrim(roomName, 'MINID', lastId - this.redisMinMessageLifetime)
309+
.xDelIfEmpty(roomName)
310+
.exec()
311311
}
312312

313313
/**

src/y-socket-io/y-socket-io.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -687,6 +687,5 @@ export class YSocketIO {
687687
this.namespaceDocMap.get(namespace)?.ydoc.destroy()
688688
this.namespaceDocMap.delete(namespace)
689689
this.namespacePersistentMap.delete(namespace)
690-
this.client?.trimRoomStream(namespace, 'index', true)
691690
}
692691
}

0 commit comments

Comments
 (0)