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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "botium-core",
3
- "version": "1.12.1",
3
+ "version": "1.12.2",
4
4
  "description": "The Selenium for Chatbots",
5
5
  "main": "index.js",
6
6
  "module": "dist/botium-es.js",
@@ -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.userSaysLimiter = null
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
- return Promise.resolve()
36
- }
37
-
38
- Build () {
39
- if (this.caps[Capabilities.RATELIMIT_USERSAYS_MAXCONCURRENT] || this.caps[Capabilities.RATELIMIT_USERSAYS_MINTIME]) {
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
- this.userSaysLimiter = new Bottleneck(opts)
44
- debug(`Build: Applying userSays rate limits ${util.inspect(opts)}`)
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.userSaysLimiter) {
73
- return this.userSaysLimiter.schedule(run)
82
+ if (this.bottleneck) {
83
+ return this.bottleneck(run)
74
84
  } else {
75
85
  return run()
76
86
  }
@@ -17,6 +17,7 @@ module.exports = class PluginConnectorContainer extends BaseContainer {
17
17
  {
18
18
  container: this,
19
19
  queueBotSays: (msg) => this._QueueBotSays(msg),
20
+ bottleneck: this.bottleneck,
20
21
  eventEmitter: this.eventEmitter,
21
22
  caps: this.caps,
22
23
  sources: this.sources,
@@ -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)