flashheart
Advanced tools
Comparing version 2.8.4 to 2.8.5
@@ -117,3 +117,6 @@ var _ = require('lodash'); | ||
if (maxAge) { | ||
cache.set(createKeyObject(url, opts, doNotVary), cachedObject, maxAge, client._handleCacheError.bind(client)); | ||
// Anonymous function needed here to pass opts through, as cache only calls func(err) | ||
cache.set(createKeyObject(url, opts, doNotVary), cachedObject, maxAge, function(err) { | ||
client._handleCacheError.call(client, err, opts); | ||
}); | ||
} | ||
@@ -123,3 +126,6 @@ | ||
var staleExpiry = staleIfError + maxAge; | ||
cache.set(createStaleKeyObject(url, opts, doNotVary), cachedObject, staleExpiry, client._handleCacheError.bind(client)); | ||
// Anonymous function needed here to pass opts through, as cache only calls func(err) | ||
cache.set(createStaleKeyObject(url, opts, doNotVary), cachedObject, staleExpiry, function(err) { | ||
client._handleCacheError.call(client, err, opts); | ||
}); | ||
} | ||
@@ -148,4 +154,5 @@ } | ||
cache.get(createKeyObject(url, opts, doNotVary), function (err, cached) { | ||
if (err) client._handleCacheError(err); | ||
if (err) client._handleCacheError(err, opts); | ||
@@ -155,3 +162,3 @@ var cacheHit = cached && cached.item && (cached.item.body || cached.item.error); | ||
if (cacheHit) { | ||
if (delegate.stats) delegate.stats.increment(delegate.name + '.cache.hits'); | ||
if (delegate.stats) delegate.stats.increment(delegate._getStatsName(opts) + '.cache.hits'); | ||
@@ -165,3 +172,3 @@ if (cached.item.error) { | ||
if (delegate.stats) delegate.stats.increment(delegate.name + '.cache.misses'); | ||
if (delegate.stats) delegate.stats.increment(delegate._getStatsName(opts) + '.cache.misses'); | ||
@@ -173,5 +180,5 @@ delegate.getWithResponse(url, opts, function (err, body, res) { | ||
return cache.get(createStaleKeyObject(url, opts, doNotVary), function (err, cached) { | ||
if (err) client._handleCacheError(err); | ||
if (err) client._handleCacheError(err, opts); | ||
if (!cached || cached.item.error) return cb(originalErr, body, res); | ||
if (delegate.stats) delegate.stats.increment(delegate.name + '.cache.stale'); | ||
if (delegate.stats) delegate.stats.increment(delegate._getStatsName(opts) + '.cache.stale'); | ||
@@ -187,6 +194,6 @@ cb(err, cached.item.body, cached.item.response, true); | ||
CachingClient.prototype._handleCacheError = function (err) { | ||
CachingClient.prototype._handleCacheError = function (err, opts) { | ||
if (err) { | ||
if (this.delegate.logger) this.delegate.logger.warn('Cache error:', err.message); | ||
if (this.delegate.stats) this.delegate.stats.increment(this.delegate.name + '.cache.errors'); | ||
if (this.delegate.stats) this.delegate.stats.increment(this.delegate._getStatsName(opts) + '.cache.errors'); | ||
} | ||
@@ -193,0 +200,0 @@ }; |
@@ -85,10 +85,2 @@ var _ = require('lodash'); | ||
function getStatsName(clientName, feedName) { | ||
if (feedName) { | ||
return clientName + '.' + feedName; | ||
} | ||
return clientName; | ||
} | ||
function buildResponse(requestRes) { | ||
@@ -119,7 +111,15 @@ return { | ||
Client.prototype._recordStats = function (res, feedName) { | ||
Client.prototype._getStatsName = function (opts) { | ||
if (opts && 'name' in opts) { | ||
return this.name + '.' + opts.name; | ||
} | ||
return this.name; | ||
}; | ||
Client.prototype._recordStats = function (res, opts) { | ||
var statsName; | ||
if (this.stats) { | ||
statsName = getStatsName(this.name, feedName); | ||
statsName = this._getStatsName(opts); | ||
this.stats.increment(statsName + '.requests'); | ||
@@ -145,3 +145,3 @@ this.stats.increment(statsName + '.responses.' + res.statusCode); | ||
if (client.stats) { | ||
statsName = getStatsName(client.name, opts.name); | ||
statsName = client._getStatsName(opts); | ||
client.stats.increment(statsName + '.request_errors'); | ||
@@ -157,3 +157,3 @@ } | ||
client._log(requestRes); | ||
client._recordStats(requestRes, opts.name); | ||
client._recordStats(requestRes, opts); | ||
@@ -201,3 +201,3 @@ if (requestRes.statusCode >= 400) { | ||
if (client.stats) { | ||
statsName = getStatsName(client.name, opts.name); | ||
statsName = client._getStatsName(opts); | ||
client.stats.timing(statsName + '.attempts', currentAttempts); | ||
@@ -204,0 +204,0 @@ } |
{ | ||
"name": "flashheart", | ||
"version": "2.8.4", | ||
"version": "2.8.5", | ||
"description": "A fully-featured REST client built for ease-of-use and resilience", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -306,2 +306,20 @@ var async = require('async'); | ||
it('Uses the request name when incrementing the counter', function (done) { | ||
client.get(url, {name: 'sheeba'}, function (err) { | ||
assert.ifError(err); | ||
sinon.assert.calledWith(stats.increment, 'http.sheeba.requests'); | ||
sinon.assert.calledWith(stats.increment, 'http.sheeba.responses.200'); | ||
done(); | ||
}); | ||
}); | ||
it('Uses the request name when incrementing the counter (when the cache fails)', function (done) { | ||
catbox.set.yields(new Error('Good use of Sheeba!')); | ||
client.get(url, {name: 'sheeba'}, function (err) { | ||
assert.ifError(err); | ||
sinon.assert.calledWith(stats.increment, 'http.sheeba.cache.errors'); | ||
done(); | ||
}); | ||
}); | ||
it('returns the response when writing to the cache fails', function (done) { | ||
@@ -308,0 +326,0 @@ catbox.set.withArgs(expectedKey).returns(new Error('Good use of Sheeba!')); |
@@ -272,2 +272,12 @@ var _ = require('lodash'); | ||
it('calls _getStatsName when incrementing', function (done) { | ||
var spy = sinon.spy(client, '_getStatsName'); | ||
nock.cleanAll(); | ||
client.get(url, function (err) { | ||
assert(err); | ||
sinon.assert.called(spy); | ||
done(); | ||
}); | ||
}); | ||
it('increments a counter for errors with feed name in it', function (done) { | ||
@@ -274,0 +284,0 @@ var client = Client.createClient({ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
284800
1897