botium-core 1.12.1 → 1.12.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/botium-cjs.js +35 -13
- package/dist/botium-cjs.js.map +1 -1
- package/dist/botium-es.js +35 -13
- package/dist/botium-es.js.map +1 -1
- package/package.json +1 -1
- package/src/Capabilities.js +1 -0
- package/src/containers/BaseContainer.js +20 -10
- package/src/containers/PluginConnectorContainer.js +1 -0
- package/src/containers/plugins/SimpleRestContainer.js +4 -3
package/package.json
CHANGED
package/src/Capabilities.js
CHANGED
|
@@ -143,6 +143,7 @@ module.exports = {
|
|
|
143
143
|
// API Calls Rate Limiting
|
|
144
144
|
RATELIMIT_USERSAYS_MAXCONCURRENT: 'RATELIMIT_USERSAYS_MAXCONCURRENT',
|
|
145
145
|
RATELIMIT_USERSAYS_MINTIME: 'RATELIMIT_USERSAYS_MINTIME',
|
|
146
|
+
RATELIMIT_BOTTLENECK_FN: 'RATELIMIT_BOTTLENECK_FN',
|
|
146
147
|
SECURITY_ALLOW_UNSAFE: 'SECURITY_ALLOW_UNSAFE',
|
|
147
148
|
PRECOMPILERS: 'PRECOMPILERS'
|
|
148
149
|
}
|
|
@@ -21,7 +21,7 @@ module.exports = class BaseContainer {
|
|
|
21
21
|
this.tempDirectory = tempDirectory
|
|
22
22
|
this.cleanupTasks = []
|
|
23
23
|
this.queues = {}
|
|
24
|
-
this.
|
|
24
|
+
this.bottleneck = null
|
|
25
25
|
}
|
|
26
26
|
|
|
27
27
|
Validate () {
|
|
@@ -32,18 +32,28 @@ module.exports = class BaseContainer {
|
|
|
32
32
|
this.onStopHook = getHook(this.caps, this.caps[Capabilities.CUSTOMHOOK_ONSTOP])
|
|
33
33
|
this.onCleanHook = getHook(this.caps, this.caps[Capabilities.CUSTOMHOOK_ONCLEAN])
|
|
34
34
|
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
35
|
+
if (this.caps[Capabilities.RATELIMIT_BOTTLENECK_FN]) {
|
|
36
|
+
if (_.isFunction(this.caps[Capabilities.RATELIMIT_BOTTLENECK_FN])) {
|
|
37
|
+
this.bottleneck = this.caps[Capabilities.RATELIMIT_BOTTLENECK_FN]
|
|
38
|
+
debug('Validate: Applying userSays rate limits from capability')
|
|
39
|
+
} else {
|
|
40
|
+
const limiter = new Bottleneck(this.caps[Capabilities.RATELIMIT_BOTTLENECK_FN])
|
|
41
|
+
this.bottleneck = (fn) => limiter.schedule(fn)
|
|
42
|
+
debug(`Validate: Applying userSays rate limits ${util.inspect(this.caps[Capabilities.RATELIMIT_BOTTLENECK_FN])}`)
|
|
43
|
+
}
|
|
44
|
+
} else if (this.caps[Capabilities.RATELIMIT_USERSAYS_MAXCONCURRENT] || this.caps[Capabilities.RATELIMIT_USERSAYS_MINTIME]) {
|
|
40
45
|
const opts = {}
|
|
41
46
|
if (this.caps[Capabilities.RATELIMIT_USERSAYS_MAXCONCURRENT]) opts.maxConcurrent = this.caps[Capabilities.RATELIMIT_USERSAYS_MAXCONCURRENT]
|
|
42
47
|
if (this.caps[Capabilities.RATELIMIT_USERSAYS_MINTIME]) opts.minTime = this.caps[Capabilities.RATELIMIT_USERSAYS_MINTIME]
|
|
43
|
-
|
|
44
|
-
|
|
48
|
+
const limiter = new Bottleneck(opts)
|
|
49
|
+
this.bottleneck = (fn) => limiter.schedule(fn)
|
|
50
|
+
debug(`Validate: Applying userSays rate limits ${util.inspect(opts)}`)
|
|
45
51
|
}
|
|
46
52
|
|
|
53
|
+
return Promise.resolve()
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
Build () {
|
|
47
57
|
return new Promise((resolve, reject) => {
|
|
48
58
|
this._RunCustomHook('onBuild', this.onBuildHook)
|
|
49
59
|
.then(() => resolve(this))
|
|
@@ -69,8 +79,8 @@ module.exports = class BaseContainer {
|
|
|
69
79
|
const run = () => this._RunCustomHook('onUserSays', this.onUserSaysHook, { meMsg })
|
|
70
80
|
.then(() => this.UserSaysImpl(meMsg))
|
|
71
81
|
|
|
72
|
-
if (this.
|
|
73
|
-
return this.
|
|
82
|
+
if (this.bottleneck) {
|
|
83
|
+
return this.bottleneck(run)
|
|
74
84
|
} else {
|
|
75
85
|
return run()
|
|
76
86
|
}
|
|
@@ -22,9 +22,10 @@ const { BotiumError } = require('../../scripting/BotiumError')
|
|
|
22
22
|
Mustache.escape = s => s
|
|
23
23
|
|
|
24
24
|
module.exports = class SimpleRestContainer {
|
|
25
|
-
constructor ({ queueBotSays, caps }) {
|
|
25
|
+
constructor ({ queueBotSays, caps, bottleneck }) {
|
|
26
26
|
this.queueBotSays = queueBotSays
|
|
27
27
|
this.caps = Object.assign({}, Defaults, caps)
|
|
28
|
+
this.bottleneck = bottleneck || ((fn) => fn())
|
|
28
29
|
this.processInbound = false
|
|
29
30
|
this.redisTopic = this.caps[Capabilities.SIMPLEREST_REDIS_TOPIC] || 'SIMPLEREST_INBOUND_SUBSCRIPTION'
|
|
30
31
|
|
|
@@ -528,11 +529,11 @@ module.exports = class SimpleRestContainer {
|
|
|
528
529
|
throw new Error(`Failed to ping bot after ${retries} retries`)
|
|
529
530
|
}
|
|
530
531
|
tries++
|
|
531
|
-
const { err, response, body } = await new Promise((resolve) => {
|
|
532
|
+
const { err, response, body } = await this.bottleneck(() => new Promise((resolve) => {
|
|
532
533
|
request(pingConfig, (err, response, body) => {
|
|
533
534
|
resolve({ err, response, body })
|
|
534
535
|
})
|
|
535
|
-
})
|
|
536
|
+
}))
|
|
536
537
|
if (err) {
|
|
537
538
|
debug(`_waitForUrlResponse error on url check ${pingConfig.uri}: ${err}`)
|
|
538
539
|
await timeout(pingConfig.timeout)
|