Reliable HTTP get wrapper (with cache and serve stale on error), best wrapped around things you dont trust very much.
Behaviour
If reliable-get makes a request that times out or errors, its callback will receive both an error object and a previously cached response, if one is present in cache.
You can then decide whether to ignore the error and use the cached response, or not.
Basic usage
var ReliableGet = require('reliable-get');
var config = {
cache:{
engine:'memorycache'
}
};
var rg = new ReliableGet(config);
rg.on('log', function(level, message, data) {
});
rg.on('stat', function(type, key, value) {
});
rg.get({url:'http://www.google.com'}, function(err, response) {
console.log(response.content);
});
Configuration options
When you create an instance of reliable-get you need to specify the cache configuration. This then applies across all requests.
var config = {
cache:{
engine:'redis',
url:'redis://localhost:6379?db=0'
}
};
You can also pass a property requestOpts
to pass options to be used in request. Example:
var config = {
cache: {
engine: 'redis',
url: 'redis://localhost:6379?db=0'
},
requestOpts: {
forever: true,
followRedirect: false
}
}
Property | Description | Example / Default | Required |
---|
cache.engine | Cache to use, redis/memorycache/nocache | nocache | No |
cache.engine.url | URL to redis | localhost:6379 | No |
cache.compress | Use snappy compression | false | No |
cache.namespace | Prefix for redis keys | '' | No |
GET options
When making a get request, you need to provide a basic options object:
rg.get({url:'http://www.google.com'}, function(err, response) {
console.log(response.content);
});
Property | Description | Example / Default | Required |
---|
url | Service to get | http://my-service.tes.co.uk | Yes |
timeout | Timeout for service | 5000 | No |
cacheKey | Key to store cached value against | my-service_tes_co_uk | No |
cacheTTL | TTL of cached value in ms | 1 minute (60000) | No |
explicitNoCache | Do not cache under any circumstances | false | No |
headers | Headers to send with request | | No |
tracer | Unique value to pass with request | | No |
type | Type of request, used for statsd and logging | | No |
statsdKey | Key that statsd events will be posted to | | No |
eventHandler | Object (see below) for logging and stats | | No |
Example from a Compoxure backend request:
var options = {
url: targetUrl,
cacheKey: targetCacheKey,
cacheTTL: targetCacheTTL,
timeout: utils.timeToMillis(backend.timeout || DEFAULT_LOW_TIMEOUT),
headers: backendHeaders,
tracer: req.tracer,
statsdKey: 'backend_' + utils.urlToCacheKey(host)
};
From a compoxure fragment request:
var options = {
url: url,
timeout: timeout,
cacheKey: cacheKey,
cacheTTL: cacheTTL,
explicitNoCache: explicitNoCache,
headers: optionsHeaders,
tracer: req.tracer,
statsdKey: statsdKey
}
Configuration
Cache configuration
The cache object accept any config value accepted by redis. It also takes:
- config.cache.engine: nocache, memorycache, redis (use nocache/memorycache for testing only!)
- config.cache.url: redis dsn, it is translated to the connection parameters
- config.cache.compress: enable snappy compression on cached items
- config.cache.namespace: adds this string as a prefix to any key. Useful to share redis with other services or migrations
Request Configuration
The "config.requestOpts" contains the default configuration passed to "request".