Skip to content

Commit

Permalink
use request-etag for basic caching
Browse files Browse the repository at this point in the history
  • Loading branch information
eplusminus committed Oct 18, 2017
1 parent e9e2ab6 commit ecdbf85
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 26 deletions.
42 changes: 42 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@
"node-sha1": "0.0.1",
"original": "~0.0.8",
"redis": "^2.6.0-2",
"request": "2.83.0",
"request": "^2.83.0",
"request-etag": "^2.0.3",
"tunnel": "https://github.com/launchdarkly/node-tunnel/tarball/d860e57650cce1ea655d00854c81babe6b47e02c",
"winston": "^2.2.0"
},
Expand Down
43 changes: 18 additions & 25 deletions requestor.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
var request = require('request');
var ETagRequest = require('request-etag');
/**
* Creates a new Requestor object, which handles remote requests to fetch feature flags for LaunchDarkly.
* This is never called synchronously when requesting a feature flag for a user (e.g. via the toggle) call.
Expand All @@ -13,23 +13,10 @@ var request = require('request');
function Requestor(sdk_key, config) {
var requestor = {};

// TODO: implement minimal caching via ETags - request doesn't do this for us

// requestify.cacheTransporter({
// cache: {},
// get: function(url, fn) {
// fn(null, this.cache[url]);
// },

// set: function(url, response, fn) {
// this.cache[url] = response;
// fn();
// },
// purge: function(url, fn) {
// delete this.cache[url];
// fn();
// }
// });
var cacheConfig = {
max: 100
};
var requestWithETagCaching = new ETagRequest(cacheConfig);

function make_request(resource) {
var request_params = {
Expand All @@ -44,20 +31,26 @@ function Requestor(sdk_key, config) {
}

return function(cb, err_cb) {
request(request_params)
.on('response', cb)
.on('error', err_cb);
requestWithETagCaching(request_params, function(err, resp, body) {
// Note that when request-etag gives us a cached response, the body will only be in the "body"
// callback parameter -- not in resp.getBody(). For a fresh response, it'll be in both.
if (err) {
err_cb(err);
} else {
cb(resp, body);
}
});
};
}

requestor.request_flag = function(key, cb) {
var req = make_request('/sdk/latest-flags/' + key);
req(
function(response) {
function(response, body) {
if (response.code !== 200) {
cb(new Error('Unexpected status code: ' + response.code), null);
} else {
cb(null, response.getBody());
cb(null, body);
}
},
function(err) {
Expand All @@ -69,11 +62,11 @@ function Requestor(sdk_key, config) {
requestor.request_all_flags = function(cb) {
var req = make_request('/sdk/latest-flags');
req(
function(response) {
function(response, body) {
if (response.code !== 200) {
cb(new Error('Unexpected status code: ' + response.code), null);
} else {
cb(null, response.getBody());
cb(null, body);
}
},
function(err) {
Expand Down

0 comments on commit ecdbf85

Please sign in to comment.