Skip to content

Commit

Permalink
Merge pull request #668 from codeforequity-at/develop
Browse files Browse the repository at this point in the history
Botium Core 1.12.2
  • Loading branch information
Botium authored Jan 28, 2022
2 parents bf8be1b + f089046 commit 2bfa4a4
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 15 deletions.
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "botium-core",
"version": "1.12.1",
"version": "1.12.2",
"description": "The Selenium for Chatbots",
"main": "index.js",
"module": "dist/botium-es.js",
Expand Down
1 change: 1 addition & 0 deletions src/Capabilities.js
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,7 @@ module.exports = {
// API Calls Rate Limiting
RATELIMIT_USERSAYS_MAXCONCURRENT: 'RATELIMIT_USERSAYS_MAXCONCURRENT',
RATELIMIT_USERSAYS_MINTIME: 'RATELIMIT_USERSAYS_MINTIME',
RATELIMIT_BOTTLENECK_FN: 'RATELIMIT_BOTTLENECK_FN',
SECURITY_ALLOW_UNSAFE: 'SECURITY_ALLOW_UNSAFE',
PRECOMPILERS: 'PRECOMPILERS'
}
30 changes: 20 additions & 10 deletions src/containers/BaseContainer.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ module.exports = class BaseContainer {
this.tempDirectory = tempDirectory
this.cleanupTasks = []
this.queues = {}
this.userSaysLimiter = null
this.bottleneck = null
}

Validate () {
Expand All @@ -32,18 +32,28 @@ module.exports = class BaseContainer {
this.onStopHook = getHook(this.caps, this.caps[Capabilities.CUSTOMHOOK_ONSTOP])
this.onCleanHook = getHook(this.caps, this.caps[Capabilities.CUSTOMHOOK_ONCLEAN])

return Promise.resolve()
}

Build () {
if (this.caps[Capabilities.RATELIMIT_USERSAYS_MAXCONCURRENT] || this.caps[Capabilities.RATELIMIT_USERSAYS_MINTIME]) {
if (this.caps[Capabilities.RATELIMIT_BOTTLENECK_FN]) {
if (_.isFunction(this.caps[Capabilities.RATELIMIT_BOTTLENECK_FN])) {
this.bottleneck = this.caps[Capabilities.RATELIMIT_BOTTLENECK_FN]
debug('Validate: Applying userSays rate limits from capability')
} else {
const limiter = new Bottleneck(this.caps[Capabilities.RATELIMIT_BOTTLENECK_FN])
this.bottleneck = (fn) => limiter.schedule(fn)
debug(`Validate: Applying userSays rate limits ${util.inspect(this.caps[Capabilities.RATELIMIT_BOTTLENECK_FN])}`)
}
} else if (this.caps[Capabilities.RATELIMIT_USERSAYS_MAXCONCURRENT] || this.caps[Capabilities.RATELIMIT_USERSAYS_MINTIME]) {
const opts = {}
if (this.caps[Capabilities.RATELIMIT_USERSAYS_MAXCONCURRENT]) opts.maxConcurrent = this.caps[Capabilities.RATELIMIT_USERSAYS_MAXCONCURRENT]
if (this.caps[Capabilities.RATELIMIT_USERSAYS_MINTIME]) opts.minTime = this.caps[Capabilities.RATELIMIT_USERSAYS_MINTIME]
this.userSaysLimiter = new Bottleneck(opts)
debug(`Build: Applying userSays rate limits ${util.inspect(opts)}`)
const limiter = new Bottleneck(opts)
this.bottleneck = (fn) => limiter.schedule(fn)
debug(`Validate: Applying userSays rate limits ${util.inspect(opts)}`)
}

return Promise.resolve()
}

Build () {
return new Promise((resolve, reject) => {
this._RunCustomHook('onBuild', this.onBuildHook)
.then(() => resolve(this))
Expand All @@ -69,8 +79,8 @@ module.exports = class BaseContainer {
const run = () => this._RunCustomHook('onUserSays', this.onUserSaysHook, { meMsg })
.then(() => this.UserSaysImpl(meMsg))

if (this.userSaysLimiter) {
return this.userSaysLimiter.schedule(run)
if (this.bottleneck) {
return this.bottleneck(run)
} else {
return run()
}
Expand Down
1 change: 1 addition & 0 deletions src/containers/PluginConnectorContainer.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ module.exports = class PluginConnectorContainer extends BaseContainer {
{
container: this,
queueBotSays: (msg) => this._QueueBotSays(msg),
bottleneck: this.bottleneck,
eventEmitter: this.eventEmitter,
caps: this.caps,
sources: this.sources,
Expand Down
7 changes: 4 additions & 3 deletions src/containers/plugins/SimpleRestContainer.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,10 @@ const { BotiumError } = require('../../scripting/BotiumError')
Mustache.escape = s => s

module.exports = class SimpleRestContainer {
constructor ({ queueBotSays, caps }) {
constructor ({ queueBotSays, caps, bottleneck }) {
this.queueBotSays = queueBotSays
this.caps = Object.assign({}, Defaults, caps)
this.bottleneck = bottleneck || ((fn) => fn())
this.processInbound = false
this.redisTopic = this.caps[Capabilities.SIMPLEREST_REDIS_TOPIC] || 'SIMPLEREST_INBOUND_SUBSCRIPTION'

Expand Down Expand Up @@ -528,11 +529,11 @@ module.exports = class SimpleRestContainer {
throw new Error(`Failed to ping bot after ${retries} retries`)
}
tries++
const { err, response, body } = await new Promise((resolve) => {
const { err, response, body } = await this.bottleneck(() => new Promise((resolve) => {
request(pingConfig, (err, response, body) => {
resolve({ err, response, body })
})
})
}))
if (err) {
debug(`_waitForUrlResponse error on url check ${pingConfig.uri}: ${err}`)
await timeout(pingConfig.timeout)
Expand Down

0 comments on commit 2bfa4a4

Please sign in to comment.