mirror of
https://github.com/clusterio/clusterio.git
synced 2026-11-19 07:33:12 +09:00
Merge pull request #1055 from bbassie/fix/subscription-limits
Limit the number of subscription filters per subscriber
This commit is contained in:
5 files changed
+126
-5
No files matched your search
@@ -83,7 +83,7 @@ export default class Controller {
|
||||
private _events = new events.EventEmitter<ControllerEvents>();
|
||||
|
||||
/** Event subscription controller */
|
||||
subscriptions = new lib.SubscriptionController();
|
||||
subscriptions: lib.SubscriptionController;
|
||||
/** User manager for the cluster, also contains access to the user records */
|
||||
users: UserManager;
|
||||
/** Instance manager for the cluster, also contains access to the user records */
|
||||
@@ -273,6 +273,7 @@ export default class Controller {
|
||||
});
|
||||
|
||||
// Handle subscriptions for all internal properties
|
||||
this.subscriptions = new lib.SubscriptionController(config.get("controller.max_subscription_filters"));
|
||||
this.subscriptions.handle(lib.SystemInfoUpdateEvent, this.handleSystemInfoSubscription.bind(this));
|
||||
this.subscriptions.handle(lib.HostUpdatesEvent, this.handleHostSubscription.bind(this));
|
||||
this.subscriptions.handle(lib.InstanceDetailsUpdatesEvent, this.handleInstanceDetailsSubscription.bind(this));
|
||||
|
||||
@@ -26,6 +26,7 @@ export interface ControllerConfigFields {
|
||||
"controller.heartbeat_interval": number;
|
||||
"controller.session_timeout": number;
|
||||
"controller.metrics_timeout": number;
|
||||
"controller.max_subscription_filters": number;
|
||||
"controller.system_metrics_interval": number;
|
||||
"controller.proxy_stream_timeout": number;
|
||||
"controller.factorio_username": string | null;
|
||||
@@ -208,6 +209,14 @@ export class ControllerConfig extends classes.Config<ControllerConfigFields> {
|
||||
initialValue: 8,
|
||||
validator: validators.greaterThanZero,
|
||||
},
|
||||
"controller.max_subscription_filters": {
|
||||
title: "Max Subscription Filters",
|
||||
description: "Maximum number of filters a connection can hold for one subscribed event, 0 for no limit.",
|
||||
type: "number",
|
||||
initialValue: 10000,
|
||||
restartRequired: true,
|
||||
validator: validators.all(validators.integer, validators.greaterThanEqualZero),
|
||||
},
|
||||
"controller.system_metrics_interval": {
|
||||
title: "System Metrics Interval",
|
||||
description: "Interval in seconds to collect and update system metrics for the Web UI",
|
||||
|
||||
@@ -255,6 +255,14 @@ type EventData = {
|
||||
export class SubscriptionController {
|
||||
_events = new Map<string, EventData>();
|
||||
|
||||
/**
|
||||
* @param _maxFilters -
|
||||
* Max number of filters a subscriber can hold for one event, 0 for no limit.
|
||||
*/
|
||||
constructor(
|
||||
private _maxFilters = 0,
|
||||
) { }
|
||||
|
||||
/**
|
||||
* Allow clients to subscribe to an event by telling the subscription controller to accept them
|
||||
* Has an optional subscription update handler which is called when a client subscribes
|
||||
@@ -329,6 +337,24 @@ export class SubscriptionController {
|
||||
}
|
||||
}
|
||||
|
||||
private _checkFilterCount(filters: SubscriptionFilters, existing?: SubscriptionFilters) {
|
||||
if (this._maxFilters === 0 || existing?.isAll()) {
|
||||
return; // union into all is a noop
|
||||
}
|
||||
let count = filters.size;
|
||||
if (existing) {
|
||||
count = existing.size;
|
||||
for (const filter of filters) {
|
||||
if (!existing.has(filter)) {
|
||||
count += 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (count > this._maxFilters) {
|
||||
throw new RequestError(`Subscription exceeds the limit of ${this._maxFilters} filters`);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Handle incoming subscription requests on a link
|
||||
* @param link - Link message was received on
|
||||
@@ -359,6 +385,7 @@ export class SubscriptionController {
|
||||
break;
|
||||
|
||||
case "subscribe":
|
||||
this._checkFilterCount(request.filters, subscriber?.filters);
|
||||
if (!subscriber) {
|
||||
eventData.subscriptions.set(index, { link, address, filters: request.filters });
|
||||
} else {
|
||||
@@ -367,6 +394,7 @@ export class SubscriptionController {
|
||||
break;
|
||||
|
||||
case "replace":
|
||||
this._checkFilterCount(request.filters);
|
||||
if (request.filters.isEmpty()) {
|
||||
if (!subscriber) {
|
||||
return false;
|
||||
|
||||
Reference in new issue
Block a user