Skip to content

Commit d08012a

Browse files
committed
Move trie use into store
1 parent 3c27f81 commit d08012a

File tree

2 files changed

+16
-9
lines changed

2 files changed

+16
-9
lines changed

lib/configproxy.js

+3-7
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,7 @@ var http = require('http'),
1717
util = require('util'),
1818
URL = require('url'),
1919
querystring = require('querystring'),
20-
store = require('./store.js'),
21-
trie = require('./trie.js');
20+
store = require('./store.js');
2221

2322
function bound (that, method) {
2423
// bind a method, to ensure `this=that` when it is called
@@ -103,7 +102,6 @@ function ConfigurableProxy (options) {
103102
this.options = options || {};
104103

105104
this._routes = store.MemoryStore();
106-
this.trie = new trie.URLTrie();
107105
this.auth_token = this.options.auth_token;
108106
this.includePrefix = options.includePrefix === undefined ? true : options.includePrefix;
109107
this.host_routing = this.options.host_routing;
@@ -189,20 +187,18 @@ util.inherits(ConfigurableProxy, EventEmitter);
189187

190188
ConfigurableProxy.prototype.add_route = function (path, data) {
191189
// add a route to the routing table
192-
path = trie.trim_prefix(path);
190+
path = this._routes.cleanPath(path);
193191
if (this.host_routing && path !== '/') {
194192
data.host = path.split('/')[1];
195193
}
196194
this._routes.add(path, data);
197-
this.trie.add(path, data);
198195
this.update_last_activity(path);
199196
};
200197

201198
ConfigurableProxy.prototype.remove_route = function (path) {
202199
// remove a route from the routing table
203200
if (this._routes.hasRoute(path)) {
204201
this._routes.remove(path);
205-
this.trie.remove(path);
206202
}
207203
};
208204

@@ -274,7 +270,7 @@ ConfigurableProxy.prototype.target_for_req = function (req) {
274270
var timer = this.statsd.createTimer('find_target_for_req');
275271
// return proxy target for a given url path
276272
var base_path = (this.host_routing) ? '/' + parse_host(req) : '';
277-
var route = this.trie.get(trie.string_to_path(base_path + decodeURIComponent(req.url)));
273+
var route = this._routes.getTarget(base_path + decodeURIComponent(req.url));
278274
timer.stop();
279275
if (route) {
280276
return {

lib/store.js

+13-2
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,23 @@
1+
var trie = require("./trie.js");
2+
13
function MemoryStore () {
24
var routes = {};
5+
var urls = new trie.URLTrie()
36

47
return {
58
get: function (path) { return routes[path]; },
9+
getTarget: function(path) { return urls.get(path); },
610
getAll: function () { return routes; },
7-
add: function (path, data) { routes[path] = data; },
11+
cleanPath: function(path) { return trie.trim_prefix(path); },
12+
add: function (path, data) {
13+
routes[path] = data;
14+
urls.add(path, data);
15+
},
816
update: function(path, data) { Object.assign(routes[path], data) },
9-
remove: function (path) { delete routes[path]; },
17+
remove: function (path) {
18+
delete routes[path];
19+
urls.remove(path);
20+
},
1021
hasRoute: function (path) { return routes.hasOwnProperty(path); }
1122
};
1223
}

0 commit comments

Comments
 (0)