diff --git a/src/api.js b/src/api.js index e30fd33..78e2f2e 100644 --- a/src/api.js +++ b/src/api.js @@ -94,7 +94,7 @@ export default class MusicBrainz { timeout: this.timeout } - debug(path, info.currentAttempt > 1 ? `(attempt #${info.currentAttempt})` : '') + debug(`Sending request. url=${path} attempt=${info.currentAttempt}`) request(options, (err, response, body) => { if (err) { diff --git a/src/loaders.js b/src/loaders.js index 01c04cc..20277c9 100644 --- a/src/loaders.js +++ b/src/loaders.js @@ -12,7 +12,7 @@ export default function createLoaders (client) { max: parseInt(process.env.GRAPHBRAINZ_CACHE_SIZE || 8192, 10), maxAge: parseInt(process.env.GRAPHBRAINZ_CACHE_TTL || ONE_DAY, 10), dispose (key) { - debug(`Removed '${key}' from cache.`) + debug(`Removed from cache. key=${key}`) } }) // Make the cache Map-like. diff --git a/src/rate-limit.js b/src/rate-limit.js index 14bfd9e..92c31ac 100644 --- a/src/rate-limit.js +++ b/src/rate-limit.js @@ -1,3 +1,5 @@ +const debug = require('debug')('graphbrainz:rate-limit') + export default class RateLimit { constructor ({ limit = 1, @@ -16,9 +18,17 @@ export default class RateLimit { this.timer = null this.pendingFlush = false this.paused = false + this.prevTaskID = null + } + + nextTaskID (prevTaskID = this.prevTaskID) { + const id = (prevTaskID || 0) + 1 + this.prevTaskID = id + return id } pause () { + clearTimeout(this.timer) this.paused = true } @@ -35,7 +45,9 @@ export default class RateLimit { priority = Math.max(0, priority) return new Promise((resolve, reject) => { const queue = this.queues[priority] = this.queues[priority] || [] - queue.push({ fn, args, resolve, reject }) + const id = this.nextTaskID() + debug(`Enqueuing task. id=${id} priority=${priority}`) + queue.push({ fn, args, resolve, reject, id }) if (!this.pendingFlush) { this.pendingFlush = true process.nextTick(() => { @@ -70,7 +82,7 @@ export default class RateLimit { if (this.numPending < this.concurrency && this.periodCapacity > 0) { const task = this.dequeue() if (task) { - const { resolve, reject, fn, args } = task + const { resolve, reject, fn, args, id } = task if (this.timer == null) { const now = Date.now() let timeout = this.period @@ -99,6 +111,7 @@ export default class RateLimit { reject(err) this.flush() } + debug(`Running task. id=${id}`) fn(...args).then(onResolve, onReject) this.flush() }