Update log messages

This commit is contained in:
Brian Beck 2016-12-02 23:59:19 -08:00
parent d7aac5220e
commit e5072de21a
3 changed files with 17 additions and 4 deletions

View file

@ -94,7 +94,7 @@ export default class MusicBrainz {
timeout: this.timeout timeout: this.timeout
} }
debug(path, info.currentAttempt > 1 ? `(attempt #${info.currentAttempt})` : '') debug(`Sending request. url=${path} attempt=${info.currentAttempt}`)
request(options, (err, response, body) => { request(options, (err, response, body) => {
if (err) { if (err) {

View file

@ -12,7 +12,7 @@ export default function createLoaders (client) {
max: parseInt(process.env.GRAPHBRAINZ_CACHE_SIZE || 8192, 10), max: parseInt(process.env.GRAPHBRAINZ_CACHE_SIZE || 8192, 10),
maxAge: parseInt(process.env.GRAPHBRAINZ_CACHE_TTL || ONE_DAY, 10), maxAge: parseInt(process.env.GRAPHBRAINZ_CACHE_TTL || ONE_DAY, 10),
dispose (key) { dispose (key) {
debug(`Removed '${key}' from cache.`) debug(`Removed from cache. key=${key}`)
} }
}) })
// Make the cache Map-like. // Make the cache Map-like.

View file

@ -1,3 +1,5 @@
const debug = require('debug')('graphbrainz:rate-limit')
export default class RateLimit { export default class RateLimit {
constructor ({ constructor ({
limit = 1, limit = 1,
@ -16,9 +18,17 @@ export default class RateLimit {
this.timer = null this.timer = null
this.pendingFlush = false this.pendingFlush = false
this.paused = false this.paused = false
this.prevTaskID = null
}
nextTaskID (prevTaskID = this.prevTaskID) {
const id = (prevTaskID || 0) + 1
this.prevTaskID = id
return id
} }
pause () { pause () {
clearTimeout(this.timer)
this.paused = true this.paused = true
} }
@ -35,7 +45,9 @@ export default class RateLimit {
priority = Math.max(0, priority) priority = Math.max(0, priority)
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
const queue = this.queues[priority] = this.queues[priority] || [] 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) { if (!this.pendingFlush) {
this.pendingFlush = true this.pendingFlush = true
process.nextTick(() => { process.nextTick(() => {
@ -70,7 +82,7 @@ export default class RateLimit {
if (this.numPending < this.concurrency && this.periodCapacity > 0) { if (this.numPending < this.concurrency && this.periodCapacity > 0) {
const task = this.dequeue() const task = this.dequeue()
if (task) { if (task) {
const { resolve, reject, fn, args } = task const { resolve, reject, fn, args, id } = task
if (this.timer == null) { if (this.timer == null) {
const now = Date.now() const now = Date.now()
let timeout = this.period let timeout = this.period
@ -99,6 +111,7 @@ export default class RateLimit {
reject(err) reject(err)
this.flush() this.flush()
} }
debug(`Running task. id=${id}`)
fn(...args).then(onResolve, onReject) fn(...args).then(onResolve, onReject)
this.flush() this.flush()
} }