-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #90 from nextcloud/feat/memory-optimization
memory optimization
- Loading branch information
Showing
25 changed files
with
3,283 additions
and
1,766 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
/* eslint-disable no-console */ | ||
|
||
/** | ||
* SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors | ||
* SPDX-License-Identifier: AGPL-3.0-or-later | ||
*/ | ||
|
||
import fetch from 'node-fetch' | ||
import https from 'https' | ||
import dotenv from 'dotenv' | ||
import Utils from './Utils.js' | ||
dotenv.config() | ||
|
||
export default class ApiService { | ||
|
||
constructor(tokenGenerator) { | ||
this.NEXTCLOUD_URL = process.env.NEXTCLOUD_URL | ||
this.IS_DEV = Utils.parseBooleanFromEnv(process.env.IS_DEV) | ||
this.agent = this.IS_DEV ? new https.Agent({ rejectUnauthorized: false }) : null | ||
this.tokenGenerator = tokenGenerator | ||
} | ||
|
||
fetchOptions(method, token, body = null, roomId = null, lastEditedUser = null) { | ||
return { | ||
method, | ||
headers: { | ||
'Content-Type': 'application/json', | ||
...(method === 'GET' && { Authorization: `Bearer ${token}` }), | ||
...(method === 'PUT' && { | ||
'X-Whiteboard-Auth': this.tokenGenerator.handle(roomId), | ||
'X-Whiteboard-User': lastEditedUser || 'unknown', | ||
}), | ||
}, | ||
...(body && { body: JSON.stringify(body) }), | ||
...(this.agent && { agent: this.agent }), | ||
} | ||
} | ||
|
||
async fetchData(url, options) { | ||
try { | ||
const response = await fetch(url, options) | ||
if (!response.ok) { | ||
throw new Error(`HTTP error! status: ${response.status}: ${await response.text()}`) | ||
} | ||
return response.json() | ||
} catch (error) { | ||
console.error(error) | ||
return null | ||
} | ||
} | ||
|
||
async getRoomDataFromServer(roomID, jwtToken) { | ||
const url = `${this.NEXTCLOUD_URL}/index.php/apps/whiteboard/${roomID}` | ||
const options = this.fetchOptions('GET', jwtToken) | ||
return this.fetchData(url, options) | ||
} | ||
|
||
async saveRoomDataToServer(roomID, roomData, lastEditedUser) { | ||
console.log('Saving room data to file') | ||
|
||
const url = `${this.NEXTCLOUD_URL}/index.php/apps/whiteboard/${roomID}` | ||
const body = { data: { elements: roomData } } | ||
const options = this.fetchOptions('PUT', null, body, roomID, lastEditedUser) | ||
return this.fetchData(url, options) | ||
} | ||
|
||
} |
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,46 @@ | ||
/** | ||
* SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors | ||
* SPDX-License-Identifier: AGPL-3.0-or-later | ||
*/ | ||
|
||
import dotenv from 'dotenv' | ||
import express from 'express' | ||
import PrometheusDataManager from './PrometheusDataManager.js' | ||
|
||
dotenv.config() | ||
|
||
export default class AppManager { | ||
|
||
constructor(storageManager) { | ||
this.app = express() | ||
this.storageManager = storageManager | ||
this.metricsManager = new PrometheusDataManager(storageManager) | ||
this.METRICS_TOKEN = process.env.METRICS_TOKEN | ||
this.setupRoutes() | ||
} | ||
|
||
setupRoutes() { | ||
this.app.get('/', this.homeHandler.bind(this)) | ||
this.app.get('/metrics', this.metricsHandler.bind(this)) | ||
} | ||
|
||
homeHandler(req, res) { | ||
res.send('Excalidraw collaboration server is up :)') | ||
} | ||
|
||
async metricsHandler(req, res) { | ||
const token = req.headers.authorization?.split(' ')[1] || req.query.token | ||
if (!this.METRICS_TOKEN || token !== this.METRICS_TOKEN) { | ||
return res.status(403).send('Unauthorized') | ||
} | ||
this.metricsManager.updateMetrics() | ||
const metrics = await this.metricsManager.getRegister().metrics() | ||
res.set('Content-Type', this.metricsManager.getRegister().contentType) | ||
res.end(metrics) | ||
} | ||
|
||
getApp() { | ||
return this.app | ||
} | ||
|
||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
/** | ||
* SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors | ||
* SPDX-License-Identifier: AGPL-3.0-or-later | ||
*/ | ||
|
||
import { register, Gauge } from 'prom-client' | ||
import SystemMonitor from './SystemMonitor.js' | ||
|
||
export default class PrometheusDataManager { | ||
|
||
constructor(storageManager) { | ||
this.systemMonitor = new SystemMonitor(storageManager) | ||
this.initializeGauges() | ||
} | ||
|
||
initializeGauges() { | ||
this.memoryUsageGauge = new Gauge({ | ||
name: 'whiteboard_memory_usage', | ||
help: 'Memory usage of the server', | ||
labelNames: ['type'], | ||
}) | ||
|
||
this.roomStatsGauge = new Gauge({ | ||
name: 'whiteboard_room_stats', | ||
help: 'Room statistics', | ||
labelNames: ['stat'], | ||
}) | ||
|
||
this.cacheInfoGauge = new Gauge({ | ||
name: 'whiteboard_cache_info', | ||
help: 'Cache information', | ||
labelNames: ['info'], | ||
}) | ||
} | ||
|
||
updateMetrics() { | ||
const overview = this.systemMonitor.getSystemOverview() | ||
|
||
Object.entries(overview.memoryUsage).forEach(([key, value]) => { | ||
this.memoryUsageGauge.set({ type: key }, parseFloat(value) || 0) | ||
}) | ||
|
||
this.roomStatsGauge.set({ stat: 'activeRooms' }, Number(overview.roomStats.activeRooms) || 0) | ||
this.roomStatsGauge.set({ stat: 'totalUsers' }, Number(overview.roomStats.totalUsers) || 0) | ||
this.roomStatsGauge.set({ stat: 'totalDataSize' }, parseFloat(overview.roomStats.totalDataSize) || 0) | ||
|
||
this.cacheInfoGauge.set({ info: 'size' }, Number(overview.cacheInfo.size) || 0) | ||
this.cacheInfoGauge.set({ info: 'maxSize' }, Number(overview.cacheInfo.maxSize) || 0) | ||
} | ||
|
||
getRegister() { | ||
return register | ||
} | ||
|
||
} |
Oops, something went wrong.