-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrobin.js
104 lines (84 loc) · 3.45 KB
/
robin.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
var httpProxy = require('http-proxy/lib/node-http-proxy'),
Cookies = require('cookies'),
util = require("util"),
EventEmitter = require('events').EventEmitter;
Robin.prototype.maximumWeight = 1000;
Robin.prototype.defaultPort = 80;
function Robin(conf) {
this.conf = conf;
EventEmitter.call(this);
}
// Setting the prototype of Robin to a new object created from the parent class EventEmitter.
util.inherits(Robin, EventEmitter);
Robin.prototype.getProxyPort = function () {
return this.conf.proxy_port || this.defaultPort; // "proxy_port" is optional in config.json.
}
Robin.prototype.getExpiryTime = function () {
var currentTimeInMillis = new Date().getTime();
var expires = parseInt(this.conf.expires);
var expiryTime = new Date(currentTimeInMillis + expires);
return expiryTime;
}
Robin.prototype.proxyRequests = function (req, res, proxy) {
var cookies = new Cookies(req, res);
var receivedCookieValue = cookies.get(this.conf.cookie_name);
if (typeof receivedCookieValue == 'undefined') { // No cookie in the request. Initial request.
this.proxyRequestFirstTime(req, res, proxy);
} else { //cookie found in the request
this.proxySubsequentRequests(req, res, proxy, receivedCookieValue);
}
}
Robin.prototype.proxyRequestFirstTime = function (req, res, proxy) {
var cookies = new Cookies(req, res);
var maxWeight = this.conf.max_weight || this.maximumWeight; // "max_weight" is optional in config.json.
var deploymentLabel = this.getRandomDeploymentLabel(maxWeight);
var target = this.conf.deployments[deploymentLabel];
var cookieName = this.conf.cookie_name;
var expiryTime = this.getExpiryTime();
res.oldWriteHead = res.writeHead;
res.writeHead = function(statusCode, headers) {
cookies.set(cookieName, deploymentLabel, {expires: expiryTime}, {domain: req.headers.host});
res.oldWriteHead(statusCode, headers);
}
proxy.proxyRequest(req, res, target);
this.emitEventsForRequests(req, res, target, deploymentLabel);
}
Robin.prototype.proxySubsequentRequests = function (req, res, proxy, deploymentLabel) {
var target;
if (typeof this.conf.deployments[deploymentLabel] != 'undefined') { //valid cookie in the request
target = this.conf.deployments[deploymentLabel];
} else { // no valid cookie found in the request
target = this.conf.default_deployment;
}
proxy.proxyRequest(req, res, target);
this.emitEventsForRequests(req, res, target, deploymentLabel);
}
Robin.prototype.getRandomDeploymentLabel = function (maxWeight) {
var randomNumber = this.generateRandomNumber(maxWeight);
var depWeight;
for (var label in this.conf.deployments) {
if (this.conf.deployments.hasOwnProperty(label)) {
depWeight = this.conf.deployments[label].weight;
if (randomNumber < depWeight) {
return label;
} else {
randomNumber = randomNumber - depWeight;
}
}
}
return label;
}
Robin.prototype.generateRandomNumber = function (maxWeight) {
var randomNumber = Math.ceil(Math.random() * maxWeight);
return randomNumber;
}
Robin.prototype.emitEventsForRequests = function (req, res, target, deploymentLabel) {
var emittedObject = {
request: req,
response: res,
target: target,
label: deploymentLabel
}
this.emit('proxiedRequest', emittedObject);
}
module.exports = Robin;