Skip to content

Commit

Permalink
fixed #57
Browse files Browse the repository at this point in the history
  • Loading branch information
manast committed Jun 24, 2016
1 parent 210ace5 commit 8e72b6b
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 17 deletions.
25 changes: 12 additions & 13 deletions lib/proxy.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ var
bunyan = require('bunyan'),
cluster = require('cluster'),
hash = require('object-hash'),
routeCache = {}
;
LRUCache = require("lru-cache"),
routeCache = LRUCache({ max: 5000 });

function ReverseProxy(opts){
if(!(this instanceof ReverseProxy)){
Expand Down Expand Up @@ -388,12 +388,13 @@ ReverseProxy.prototype._defaultResolver.priority = 0;
*/
ReverseProxy.prototype.resolve = function(host, url){
var route;

host = host.toLowerCase();
for(var i=0; i < this.resolvers.length; i++){
route = this.resolvers[i].call(this, host, url);
if(route && (route = ReverseProxy.buildRoute(route))){
//ensure resolved route has path that prefixes URL
//no need to check for native routes.

// ensure resolved route has path that prefixes URL
// no need to check for native routes.
if(!route.isResolved || route.path === '/' || startsWith(url, route.path)){
return route;
}
Expand All @@ -412,12 +413,12 @@ ReverseProxy.buildRoute = function (route) {
}

var cacheKey = _.isString(route) ? route : hash(route);
if(routeCache.hasOwnProperty(cacheKey)){
routeCache[cacheKey]._hits++;
return routeCache[cacheKey];
var entry = routeCache.get(cacheKey);
if(entry){
return entry;
}

var routeObject = {rr: 0, isResolved: true, _hits: 1};
var routeObject = {rr: 0, isResolved: true};
if(_.isString(route)){
routeObject.urls = [ReverseProxy.buildTarget(route)];
routeObject.path = '/';
Expand All @@ -432,10 +433,8 @@ ReverseProxy.buildRoute = function (route) {

routeObject.path = route.path || '/';
}

//cache routes for easy referencing. It's not likely to change.
return routeCache[cacheKey] = routeObject;

routeCache.set(cacheKey, routeObject);
return routeObject;
};

ReverseProxy.prototype._getTarget = function(src, req){
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
"dolphin": "^0.0.7",
"http-proxy": "1.12.1",
"lodash": "^4.13.1",
"lru-cache": "^4.0.1",
"node-etcd": "^4.2.1",
"object-hash": "^1.1.2",
"valid-url": "^1.0.9"
Expand Down
4 changes: 1 addition & 3 deletions test/test_custom_resolver.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ describe("Custom Resolver", function(){

});


it('Should properly convert and cache route to routeObject', function () {

var builder = Redbird.buildRoute;
Expand Down Expand Up @@ -121,7 +121,6 @@ describe("Custom Resolver", function(){

var result2 = builder(testString);
expect(result2).to.be.eq(result);
expect(result2._hits).to.be.above(1);

//case with object

Expand All @@ -138,7 +137,6 @@ describe("Custom Resolver", function(){
//test object caching.
var testObjectResult_2 = builder(testObject_1);
expect(testObjectResult_1).to.be.eq(testObjectResult_2);
expect(testObjectResult_2._hits).to.be.above(1);

var testObject_2= {url: ['http://127.0.0.1', 'http://123.1.1.1']}
var testResult2 = builder(testObject_2);
Expand Down
18 changes: 17 additions & 1 deletion test/test_register.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ describe("Route registration", function(){

expect(redbird.routing).to.have.property("example.com")

expect(redbird.resolve('example.com')).to.be.an("object");

var host = redbird.routing["example.com"];
expect(host).to.be.an("array");
expect(host[0]).to.have.property('path')
Expand All @@ -34,7 +36,21 @@ describe("Route registration", function(){
expect(redbird.resolve('example.com')).to.be.an("undefined")

redbird.close();
})
});

it("should resolve domains as case insensitive", function(){
var redbird = Redbird(opts);

expect(redbird.routing).to.be.an("object");

redbird.register('example.com', '192.168.1.2:8080');

expect(redbird.resolve('Example.com')).to.be.an("object");

redbird.close();
});


it("should register multiple routes", function(){
var redbird = Redbird(opts);

Expand Down

0 comments on commit 8e72b6b

Please sign in to comment.