mirror of
https://github.com/clusterio/clusterio.git
synced 2026-11-19 15:33:12 +09:00
Add rate limiting to storage updates
Limit the rate of storage updates broadcasted by subspace_storage to at most one per second.
This commit is contained in:
1 parent
3b659116db
commit
89cf89670f
1 file changed
+16
@@ -5,6 +5,7 @@ const path = require("path");
|
|||||||
const libDatabase = require("@clusterio/lib/database");
|
const libDatabase = require("@clusterio/lib/database");
|
||||||
const libPlugin = require("@clusterio/lib/plugin");
|
const libPlugin = require("@clusterio/lib/plugin");
|
||||||
const { Counter, Gauge } = require("@clusterio/lib/prometheus");
|
const { Counter, Gauge } = require("@clusterio/lib/prometheus");
|
||||||
|
const RateLimiter = require("@clusterio/lib/RateLimiter");
|
||||||
|
|
||||||
const routes = require("./routes");
|
const routes = require("./routes");
|
||||||
const dole = require("./dole");
|
const dole = require("./dole");
|
||||||
@@ -58,6 +59,16 @@ class MasterPlugin extends libPlugin.BaseMasterPlugin {
|
|||||||
async init() {
|
async init() {
|
||||||
|
|
||||||
this.items = await loadDatabase(this.master.config, this.logger);
|
this.items = await loadDatabase(this.master.config, this.logger);
|
||||||
|
this.itemUpdateRateLimiter = new RateLimiter({
|
||||||
|
maxRate: 1,
|
||||||
|
action: () => {
|
||||||
|
try {
|
||||||
|
this.broadcastStorage();
|
||||||
|
} catch (err) {
|
||||||
|
this.logger.error(`Unexpected error sending storage update:\n${err.stack}`);
|
||||||
|
}
|
||||||
|
},
|
||||||
|
});
|
||||||
this.itemsLastUpdate = new Map(this.items._items.entries());
|
this.itemsLastUpdate = new Map(this.items._items.entries());
|
||||||
this.autosaveId = setInterval(() => {
|
this.autosaveId = setInterval(() => {
|
||||||
saveDatabase(this.master.config, this.items, this.logger).catch(err => {
|
saveDatabase(this.master.config, this.items, this.logger).catch(err => {
|
||||||
@@ -78,6 +89,10 @@ class MasterPlugin extends libPlugin.BaseMasterPlugin {
|
|||||||
}
|
}
|
||||||
|
|
||||||
updateStorage() {
|
updateStorage() {
|
||||||
|
this.itemUpdateRateLimiter.activate();
|
||||||
|
}
|
||||||
|
|
||||||
|
broadcastStorage() {
|
||||||
let itemsToUpdate = new Map();
|
let itemsToUpdate = new Map();
|
||||||
for (let [name, count] of this.items._items) {
|
for (let [name, count] of this.items._items) {
|
||||||
if (this.itemsLastUpdate.get(name) === count) {
|
if (this.itemsLastUpdate.get(name) === count) {
|
||||||
@@ -209,6 +224,7 @@ class MasterPlugin extends libPlugin.BaseMasterPlugin {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async onShutdown() {
|
async onShutdown() {
|
||||||
|
this.itemUpdateRateLimiter.cancel();
|
||||||
clearInterval(this.autosaveId);
|
clearInterval(this.autosaveId);
|
||||||
clearInterval(this.doleMagicId);
|
clearInterval(this.doleMagicId);
|
||||||
await saveDatabase(this.master.config, this.items, this.logger);
|
await saveDatabase(this.master.config, this.items, this.logger);
|
||||||
|
|||||||
Reference in new issue
Block a user