5.0.10 • Published 4 years ago

reliable-get v5.0.10

Weekly downloads
15
License
MIT
Repository
github
Last release
4 years ago

Reliable HTTP get wrapper (with cache and serve stale on error), best wrapped around things you dont trust very much.

Build Status Coverage Status

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) {
  // Wire up to your favourite logger
});
rg.on('stat', function(type, key, value) {
 // Wire up to your favourite stats library (e.g. statsd)
});
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
  }
}
PropertyDescriptionExample / DefaultRequired
cache.engineCache to use, redis/memcached/memorycache/nocachenocacheNo
cache.engine.urlURL to redislocalhost:6379No
cache.compressUse snappy compressionfalseNo
cache.namespacePrefix for redis keys''No
cache.hostsArray of host:port combinations for memcached[]No
cache.autodiscoverUse Elasticache Auto DiscoveryfalseNo

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);
});
PropertyDescriptionExample / DefaultRequired
urlService to gethttp://my-service.tes.co.ukYes
timeoutTimeout for service5000No
cacheKeyKey to store cached value againstmy-service_tes_co_ukNo
tagsList of tags (surrogate keys)[]No
cacheTTLTTL of cached value in ms1 minute (60000)No
explicitNoCacheDo not cache under any circumstancesfalseNo
headersHeaders to send with requestNo
tracerUnique value to pass with requestNo
typeType of request, used for statsd and loggingNo
statsdKeyKey that statsd events will be posted toNo
eventHandlerObject (see below) for logging and statsNo

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
};

The options object is fully passed down to the request.

Response fields

The library will decorate with response with some useful keys that you may need to use, there are following:

  • stale - is added to response when the request to origin failed and a stale cached version is returned instead
  • cached - is true if there was a cache hit, otherwise false
  • realTiming - show the time it took for the response to be returned

Tags

Some store (memorycache/redis) supports assigning one or more tags to a certain resource. They are used only if the resource is cached, to purge all cache entries with the same tag.

Configuration

Cache configuration

The cache object accept any config value accepted by redis. It also takes:

  • config.cache.engine: nocache, memorycache, memcached, 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
  • config.cache.hosts: Memcached cluster nodes addresses as <host>:<port> combinartions in an array
  • config.cache.autodiscover: enable AWS Elasticache Auto Discovery of Memcached cache cluster nodes

Request Configuration

The "config.requestOpts" contains the default configuration passed to "request".

5.0.10

4 years ago

5.0.9

5 years ago

5.0.8

5 years ago

5.0.7

6 years ago

5.0.6

6 years ago

5.0.4

6 years ago

5.0.3

6 years ago

5.0.2

6 years ago

5.0.1

6 years ago

5.0.0

6 years ago

4.0.0

7 years ago

3.2.2

7 years ago

3.2.1

7 years ago

3.2.0

7 years ago

3.1.0

7 years ago

3.0.1

7 years ago

3.0.0

7 years ago

2.0.1

7 years ago

2.0.9

7 years ago

1.0.9

7 years ago

1.0.8

7 years ago

1.0.7

7 years ago

1.0.5

7 years ago

1.0.4

7 years ago

1.0.3

7 years ago

1.0.2

7 years ago

1.0.1

8 years ago

0.1.31

8 years ago

0.1.30

8 years ago

0.1.29

8 years ago

0.1.28

8 years ago

0.1.26

8 years ago

0.1.25

8 years ago

0.1.24

9 years ago

0.1.23

9 years ago

0.1.22

9 years ago

0.1.21

9 years ago

0.1.19

9 years ago

0.1.18

9 years ago

0.1.17

9 years ago

0.1.16

9 years ago

0.1.15

9 years ago

0.1.14

9 years ago

0.1.13

9 years ago

0.1.12

9 years ago

0.1.11

9 years ago

0.1.10

9 years ago

0.1.9

9 years ago

0.1.8

9 years ago

0.1.7

9 years ago

0.1.6

9 years ago

0.1.5

9 years ago

0.1.4

9 years ago

0.1.3

9 years ago

0.1.2

9 years ago

0.1.1

9 years ago

0.1.0

9 years ago