Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 11 additions & 11 deletions src/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ if (ydocUpdateCallback != null && ydocUpdateCallback.slice(-1) !== '/') {
ydocUpdateCallback += '/'
}
const WORKER_DISABLED = env.getConf('y-worker-disabled') === 'true'
const ROOM_STREAM_TTL = number.parseInt(env.getConf('y-room-stream-ttl') || '300')

/**
* @param {string} a
Expand Down Expand Up @@ -125,14 +126,18 @@ export class Api {
this.persistWorker = null

const addScript = WORKER_DISABLED
? 'redis.call("XADD", KEYS[1], "*", "m", ARGV[1])'
? `
redis.call("XADD", KEYS[1], "*", "m", ARGV[1])
redis.call("EXPIRE", KEYS[1], ${ROOM_STREAM_TTL})
`
: `
if redis.call("EXISTS", KEYS[1]) == 0 then
redis.call("XADD", "${this.redisWorkerStreamName}", "*", "compact", KEYS[1])
elseif redis.call("XLEN", KEYS[1]) > 100 then
redis.call("SADD", "${this.prefix}:worker:checklist", KEYS[1])
end
redis.call("XADD", KEYS[1], "*", "m", ARGV[1])
redis.call("EXPIRE", KEYS[1], ${ROOM_STREAM_TTL})
`

this.redis = redis.createClient({
Expand Down Expand Up @@ -294,20 +299,15 @@ export class Api {
/**
* @param {string} room
* @param {string} docid
* @param {boolean} [remove=false]
*/
async trimRoomStream (room, docid, remove = false) {
async trimRoomStream (room, docid) {
const roomName = computeRedisRoomStreamName(room, docid, this.prefix)
const redisLastId = await this.getRedisLastId(room, docid)
const lastId = number.parseInt(redisLastId.split('-')[0])
if (remove) {
await this.redis.del(roomName)
} else {
await this.redis.multi()
.xTrim(roomName, 'MINID', lastId - this.redisMinMessageLifetime)
.xDelIfEmpty(roomName)
.exec()
}
await this.redis.multi()
.xTrim(roomName, 'MINID', lastId - this.redisMinMessageLifetime)
.xDelIfEmpty(roomName)
Copy link
Member

@jackycute jackycute Jan 25, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe not remove stream even the room messages is empty to avoid race?

.exec()
}

/**
Expand Down
1 change: 0 additions & 1 deletion src/y-socket-io/y-socket-io.js
Original file line number Diff line number Diff line change
Expand Up @@ -687,6 +687,5 @@ export class YSocketIO {
this.namespaceDocMap.get(namespace)?.ydoc.destroy()
this.namespaceDocMap.delete(namespace)
this.namespacePersistentMap.delete(namespace)
this.client?.trimRoomStream(namespace, 'index', true)
}
}
Loading