Skip to content

Commit 4252e75

Browse files
committed
feat: add env for disabling worker
1 parent da3e114 commit 4252e75

File tree

2 files changed

+21
-16
lines changed

2 files changed

+21
-16
lines changed

src/api.js

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ let ydocUpdateCallback = env.getConf('ydoc-update-callback')
1919
if (ydocUpdateCallback != null && ydocUpdateCallback.slice(-1) !== '/') {
2020
ydocUpdateCallback += '/'
2121
}
22+
const WORKER_DISABLED = env.getConf('y-worker-disabled') === 'true'
2223

2324
/**
2425
* @param {string} a
@@ -117,20 +118,25 @@ export class Api {
117118
this.redisWorkerGroupName = this.prefix + ':worker'
118119
this.workerSetName = `${this.prefix}:worker:${this.consumername}:idset`
119120
this._destroyed = false
121+
122+
const addScript = WORKER_DISABLED
123+
? 'redis.call("XADD", KEYS[1], "*", "m", ARGV[1])'
124+
: `
125+
if redis.call("EXISTS", KEYS[1]) == 0 then
126+
redis.call("XADD", "${this.redisWorkerStreamName}", "*", "compact", KEYS[1])
127+
elseif redis.call("XLEN", KEYS[1]) > 100 then
128+
redis.call("SADD", "${this.prefix}:worker:checklist", KEYS[1])
129+
end
130+
redis.call("XADD", KEYS[1], "*", "m", ARGV[1])
131+
`
132+
120133
this.redis = redis.createClient({
121134
url,
122135
// scripting: https://github.com/redis/node-redis/#lua-scripts
123136
scripts: {
124137
addMessage: redis.defineScript({
125138
NUMBER_OF_KEYS: 1,
126-
SCRIPT: `
127-
-- if redis.call("EXISTS", KEYS[1]) == 0 then
128-
-- redis.call("XADD", "${this.redisWorkerStreamName}", "*", "compact", KEYS[1])
129-
-- elseif redis.call("XLEN", KEYS[1]) > 100 then
130-
-- redis.call("SADD", "${this.prefix}:worker:checklist", KEYS[1])
131-
-- end
132-
redis.call("XADD", KEYS[1], "*", "m", ARGV[1])
133-
`,
139+
SCRIPT: addScript,
134140
/**
135141
* @param {string} key
136142
* @param {Buffer} message

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

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import toobusy from 'toobusy-js'
1717
const logSocketIO = createModuleLogger('@y/socket-io/server')
1818
const PERSIST_INTERVAL = number.parseInt(env.getConf('y-socket-io-server-persist-interval') || '3000')
1919
const REVALIDATE_TIMEOUT = number.parseInt(env.getConf('y-socket-io-server-revalidate-timeout') || '60000')
20+
const WORKER_DISABLED = env.getConf('y-worker-disabled') === 'true'
2021

2122
process.on('SIGINT', function () {
2223
// calling .shutdown allows your process to exit normally
@@ -200,10 +201,8 @@ export class YSocketIO {
200201
;(async () => {
201202
assert(this.client)
202203
assert(socket.user)
203-
const doc =
204-
this.namespaceDocMap.get(namespace) ||
205-
(await this.client.getDoc(namespace, 'index'))
206-
this.namespaceDocMap.set(namespace, doc)
204+
const doc = WORKER_DISABLED && this.namespaceDocMap.get(namespace) || (await this.client.getDoc(namespace, 'index'))
205+
if (WORKER_DISABLED) this.namespaceDocMap.set(namespace, doc)
207206

208207
if (
209208
api.isSmallerRedisId(doc.redisLastId, socket.user.initialRedisSubId)
@@ -248,10 +247,8 @@ export class YSocketIO {
248247
) => {
249248
assert(this.client)
250249
const namespace = this.getNamespaceString(socket.nsp)
251-
const doc =
252-
this.namespaceDocMap.get(namespace) ||
253-
(await this.client.getDoc(namespace, 'index'))
254-
this.namespaceDocMap.set(namespace, doc)
250+
const doc = WORKER_DISABLED && this.namespaceDocMap.get(namespace) || (await this.client.getDoc(namespace, 'index'))
251+
if (WORKER_DISABLED) this.namespaceDocMap.set(namespace, doc)
255252
assert(doc)
256253
syncStep2(Y.encodeStateAsUpdate(doc.ydoc, stateVector))
257254
}
@@ -404,6 +401,8 @@ export class YSocketIO {
404401
nsp.emit('awareness-update', msg)
405402
}
406403

404+
if (!WORKER_DISABLED) return
405+
407406
let changed = false
408407
const existDoc = this.namespaceDocMap.get(namespace)
409408
if (existDoc) {

0 commit comments

Comments
 (0)