MCPcopy
hub / github.com/soketi/soketi / constructor

Method constructor

src/rate-limiters/cluster-rate-limiter.ts:21–56  ·  view source on GitHub ↗

* Initialize the local rate limiter driver.

(protected server: Server)

Source from the content-addressed store, hash-verified

19 * Initialize the local rate limiter driver.
20 */
21 constructor(protected server: Server) {
22 super(server);
23
24 if (cluster.isPrimary || typeof cluster.isPrimary === 'undefined') {
25 if (server.pm2) {
26 // With PM2, discovery is not needed.
27 new RateLimiterClusterMasterPM2(pm2);
28 } else {
29 // When a new master is demoted, the rate limiters it has become the pivot points of the real, synced
30 // rate limiter instances. Just trust this value.
31 server.discover.join('rate_limiter:limiters', (rateLimiters: { [key: string]: RateLimiterAbstract }) => {
32 this.rateLimiters = Object.fromEntries(
33 Object.entries(rateLimiters).map(([key, rateLimiterObject]: [string, any]) => {
34 return [
35 key,
36 this.createNewRateLimiter(key.split(':')[0], rateLimiterObject._points),
37 ];
38 })
39 );
40 });
41
42 // All nodes need to know when other nodes consumed from the rate limiter.
43 server.discover.join('rate_limiter:consume', ({ app, eventKey, points, maxPoints }: ConsumptionMessage) => {
44 super.consume(app, eventKey, points, maxPoints);
45 });
46
47 server.discover.on('added', () => {
48 if (server.nodes.get('self').isMaster) {
49 // When a new node is added, just send the rate limiters this master instance has.
50 // This value is the true value of the rate limiters.
51 this.sendRateLimiters();
52 }
53 });
54 }
55 }
56 }
57
58 /**
59 * Consume points for a given key, then

Callers

nothing calls this directly

Calls 5

sendRateLimitersMethod · 0.95
createNewRateLimiterMethod · 0.65
getMethod · 0.65
joinMethod · 0.45
consumeMethod · 0.45

Tested by

no test coverage detected