-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Hoang Pham <[email protected]>
- Loading branch information
Showing
22 changed files
with
3,684 additions
and
1,961 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -21,7 +21,24 @@ TLS_CERT= | |
# Turn off SSL certificate validation in development mode for easier testing | ||
IS_DEV=false | ||
|
||
# Storage strategy for whiteboard data and socket-related temporary data | ||
# Valid values are: 'redis' or 'lru' (Least Recently Used cache) | ||
# This strategy is used for: | ||
# 1. Whiteboard data storage | ||
# 2. Socket-related temporary data (e.g., cached tokens, bound data for each socket ID) | ||
# 3. Scaling the socket server across multiple nodes (when using 'redis') | ||
# We strongly recommend using 'redis' for production environments | ||
# 'lru' provides a balance of performance and memory usage for single-node setups | ||
STORAGE_STRATEGY=lru | ||
|
||
# Redis connection URL for data storage and socket server scaling | ||
# Required when STORAGE_STRATEGY is set to 'redis' | ||
# This URL is used for both persistent data and temporary socket-related data | ||
# Format: redis://[username:password@]host[:port][/database_number] | ||
# Example: redis://user:[email protected]:6379/0 | ||
REDIS_URL=redis://localhost:6379 | ||
|
||
# Prometheus metrics endpoint | ||
# Set this to access the monitoring endpoint at /metrics | ||
# either providing it as Bearer token or as ?token= query parameter | ||
# METRICS_TOKEN= | ||
# METRICS_TOKEN= |
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
/* eslint-disable no-console */ | ||
|
||
/** | ||
* SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors | ||
* SPDX-License-Identifier: AGPL-3.0-or-later | ||
*/ | ||
|
||
import StorageStrategy from './StorageStrategy.js' | ||
import { LRUCache } from 'lru-cache' | ||
|
||
export default class LRUCacheStrategy extends StorageStrategy { | ||
|
||
constructor(apiService) { | ||
super() | ||
this.apiService = apiService | ||
this.cache = new LRUCache({ | ||
max: 1000, | ||
ttl: 30 * 60 * 1000, | ||
ttlAutopurge: true, | ||
dispose: async (value, key) => { | ||
console.log('Disposing room', key) | ||
if (value?.data && value?.lastEditedUser) { | ||
try { | ||
await this.apiService.saveRoomDataToServer( | ||
key, | ||
value.data, | ||
value.lastEditedUser, | ||
) | ||
} catch (error) { | ||
console.error(`Failed to save room ${key} data:`, error) | ||
} | ||
} | ||
}, | ||
}) | ||
} | ||
|
||
async get(key) { | ||
return this.cache.get(key) | ||
} | ||
|
||
async set(key, value) { | ||
this.cache.set(key, value) | ||
} | ||
|
||
async delete(key) { | ||
this.cache.delete(key) | ||
} | ||
|
||
async clear() { | ||
this.cache.clear() | ||
} | ||
|
||
getRooms() { | ||
return this.cache | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
/* eslint-disable no-console */ | ||
|
||
/** | ||
* SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors | ||
* SPDX-License-Identifier: AGPL-3.0-or-later | ||
*/ | ||
|
||
import StorageStrategy from './StorageStrategy.js' | ||
import { createClient } from 'redis' | ||
import Room from './Room.js' | ||
|
||
export default class RedisStrategy extends StorageStrategy { | ||
|
||
constructor(apiService) { | ||
super() | ||
this.apiService = apiService | ||
this.client = createClient({ | ||
url: process.env.REDIS_URL || 'redis://localhost:6379', | ||
retry_strategy: (options) => { | ||
if (options.error && options.error.code === 'ECONNREFUSED') { | ||
return new Error('The server refused the connection') | ||
} | ||
if (options.total_retry_time > 1000 * 60 * 60) { | ||
return new Error('Retry time exhausted') | ||
} | ||
if (options.attempt > 10) { | ||
return undefined | ||
} | ||
return Math.min(options.attempt * 100, 3000) | ||
}, | ||
}) | ||
this.client.on('error', (err) => | ||
console.error('Redis Client Error', err), | ||
) | ||
this.connect() | ||
} | ||
|
||
async connect() { | ||
try { | ||
await this.client.connect() | ||
} catch (error) { | ||
console.error('Failed to connect to Redis:', error) | ||
throw error | ||
} | ||
} | ||
|
||
async get(key) { | ||
try { | ||
const data = await this.client.get(key) | ||
if (!data) return null | ||
return this.deserialize(data) | ||
} catch (error) { | ||
console.error(`Error getting data for key ${key}:`, error) | ||
return null | ||
} | ||
} | ||
|
||
async set(key, value) { | ||
try { | ||
const serializedData = this.serialize(value) | ||
await this.client.set(key, serializedData, { EX: 30 * 60 }) | ||
} catch (error) { | ||
console.error(`Error setting data for key ${key}:`, error) | ||
} | ||
} | ||
|
||
async delete(key) { | ||
try { | ||
const room = await this.get(key) | ||
if (room?.data && room?.lastEditedUser) { | ||
await this.apiService.saveRoomDataToServer( | ||
key, | ||
room.data, | ||
room.lastEditedUser, | ||
) | ||
} | ||
await this.client.del(key) | ||
} catch (error) { | ||
console.error(`Error deleting key ${key}:`, error) | ||
} | ||
} | ||
|
||
async clear() { | ||
try { | ||
await this.client.flushDb() | ||
} catch (error) { | ||
console.error('Error clearing Redis database:', error) | ||
} | ||
} | ||
|
||
async getRooms() { | ||
try { | ||
const keys = await this.client.keys('*') | ||
const rooms = new Map() | ||
for (const key of keys) { | ||
const room = await this.get(key) | ||
if (room) rooms.set(key, room) | ||
} | ||
return rooms | ||
} catch (error) { | ||
console.error('Error getting rooms:', error) | ||
return new Map() | ||
} | ||
} | ||
|
||
serialize(value) { | ||
return value instanceof Room | ||
? JSON.stringify(value.toJSON()) | ||
: JSON.stringify(value) | ||
} | ||
|
||
deserialize(data) { | ||
const parsedData = JSON.parse(data) | ||
return parsedData.id && parsedData.users | ||
? Room.fromJSON(parsedData) | ||
: parsedData | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
/** | ||
* SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors | ||
* SPDX-License-Identifier: AGPL-3.0-or-later | ||
*/ | ||
|
||
export default class Room { | ||
|
||
constructor(id, data = null, users = new Set(), lastEditedUser = null) { | ||
this.id = id | ||
this.data = data | ||
this.users = new Set(users) | ||
this.lastEditedUser = lastEditedUser | ||
} | ||
|
||
setUsers(users) { | ||
this.users = new Set(users) | ||
} | ||
|
||
updateLastEditedUser(userId) { | ||
this.lastEditedUser = userId | ||
} | ||
|
||
setData(data) { | ||
this.data = data | ||
} | ||
|
||
isEmpty() { | ||
return this.users.size === 0 | ||
} | ||
|
||
toJSON() { | ||
return { | ||
id: this.id, | ||
data: this.data, | ||
users: Array.from(this.users), | ||
lastEditedUser: this.lastEditedUser, | ||
} | ||
} | ||
|
||
static fromJSON(json) { | ||
return new Room( | ||
json.id, | ||
json.data, | ||
new Set(json.users), | ||
json.lastEditedUser, | ||
) | ||
} | ||
|
||
} |
Oops, something went wrong.