-
-
Notifications
You must be signed in to change notification settings - Fork 16
/
MsgRateLimiter.js
151 lines (126 loc) · 4.2 KB
/
MsgRateLimiter.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
'use strict'
const Bottleneck = require('bottleneck/es5')
function MsgRateLimiter(logger) {
this.profileConfigs = {
DEFAULT: {
maxConcurrent: 1,
minTime: 1000, //1 sec
highWater: 0,
strategy: 'BLOCK',
penalty: 30 * 1000, //30 sec
reservoir: 60,
reservoirIncreaseInterval: 60 * 1000, //60 sec
reservoirIncreaseAmount: 1,
reservoirIncreaseMaximum: 60,
},
}
this.profileMapping = {}
this.rateLimiters = {}
this.logger = logger
/**
* returns the most specific rate limiter profile for a given classication
*
* the following search cascade is applied:
* 1. <causeType>_<template> i.e. PHYSICAL_INTERACTION_DIMMER_SWITCH
* 2. <causeType>_DEFAULT i.e. PHYSICAL_INTERACTION_DEFAULT
* 3. DEFAULT i.e. DEFAULT
*/
this.getProfileForClassification = function ({ causeType, template }) {
if (
this.profileMapping[`${causeType}_${template}`] &&
this.profileConfigs[this.profileMapping[`${causeType}_${template}`]]
) {
return this.profileMapping[`${causeType}_${template}`]
} else if (
this.profileMapping[`${causeType}_DEFAULT`] &&
this.profileConfigs[this.profileMapping[`${causeType}_DEFAULT`]]
) {
return this.profileMapping[`${causeType}_DEFAULT`]
} else {
return 'DEFAULT'
}
}
this.getRateLimiterForClassification = function (classification) {
const { causeType, template, endpointId } = classification
const key = `${endpointId}-${causeType}`
if (!this.rateLimiters[key]) {
const profileName = this.getProfileForClassification({
causeType,
template,
})
const bottleneckConfig = {
maxConcurrent: 1,
minTime: 0,
highWater: 1,
...this.profileConfigs[profileName],
strategy:
Bottleneck.strategy[this.profileConfigs[profileName]['strategy']],
}
//console.log(`new bottleneck for ${key}`, bottleneckConfig)
const logger = this.logger
const rateLimiter = new Bottleneck(bottleneckConfig)
// rateLimiter.on('debug', function (message, data) {
// console.log(message, data)
// })
// rateLimiter.on('queued', function (info) {
// console.log('QUEUED', info.options.id)
// })
// rateLimiter.on('scheduled', function (info) {
// console.log('SCHEDULED', info.options.id)
// })
// rateLimiter.on('executing', function (info) {
// console.log('EXECUTING', info.options.id)
// })
// rateLimiter.on('done', function (info) {
// console.log('DONE', info.options.id)
// })
rateLimiter.on('dropped', function (info) {
logger(
`MsgRateLimiter: dropping ${template}:${endpointId}:${causeType}`,
null,
'warn'
)
//console.log('DROPPED', info.options.id)
})
rateLimiter.on('error', function (error) {
logger('ERROR', error)
})
this.rateLimiters[key] = rateLimiter
}
return this.rateLimiters[key]
}
this.destroyAllRateLimiters = async function () {
const promises = Object.keys(this.rateLimiters).map((key) =>
this.rateLimiters[key].stop()
)
await Promise.all(promises)
this.rateLimiters = {}
}
}
MsgRateLimiter.prototype.execute = function (
classification,
callback,
jobId = '<no-id>'
) {
const { causeType, template, endpointId } = classification
const rateLimiter = this.getRateLimiterForClassification(classification)
// let counts
// counts = rateLimiter.counts()
// console.log('counts BEFORE submit', counts)
rateLimiter.submit({ id: jobId }, callback, () => {})
// counts = rateLimiter.counts()
// console.log('counts AFTER submit', counts)
}
MsgRateLimiter.prototype.overrideConfig = async function (config) {
this.logger(
'MsgRateLimiter.overrideConfig: ' + JSON.stringify(config, null, 2)
)
const { profiles, profileMapping } = config
this.profileConfigs = { ...this.profileConfigs, ...profiles } //merge
this.profileMapping = profileMapping //override
await this.destroyAllRateLimiters()
}
MsgRateLimiter.prototype.destroy = async function () {
await this.destroyAllRateLimiters()
}
module.exports = MsgRateLimiter