-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
165 lines (165 loc) · 6.26 KB
/
index.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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var redis = require("redis");
var RedisConfig = (function () {
function RedisConfig(host, port, db, password) {
this.host = host;
this.port = port;
this.db = db ? db : null;
this.password = password ? password : null;
}
return RedisConfig;
}());
exports.RedisConfig = RedisConfig;
var RedisWeightedPool = (function () {
function RedisWeightedPool(config, client) {
this.DEFAULT_REDIS_HOST = "localhost";
this.DEFAULT_REDIS_PORT = 6379;
this.ENTRY_TYPE = "entries";
this.PEER_TYPE = "peers";
if (client && client instanceof redis.RedisClient) {
this.client = client;
}
else {
var options = {
host: config.host || this.DEFAULT_REDIS_HOST,
port: config.port || this.DEFAULT_REDIS_PORT,
retry_strategy: function (status) {
if (status.error && status.error.code === "ECONNREFUSED") {
return new Error("The server refused the connection");
}
if (status.total_retry_time > 1000 * 60 * 60) {
return new Error("Retry time exhausted");
}
if (status.attempt > 10) {
return undefined;
}
return Math.min(status.attempt * 100, 3000);
},
};
if (config.db) {
options.db = config.db;
}
if (config.password) {
options.password = config.password;
}
this.client = redis.createClient(options);
}
}
RedisWeightedPool.prototype.length = function (channel) {
var _this = this;
return new Promise(function (resolve, reject) {
if (typeof channel !== "string") {
throw new TypeError("Channel parameter must be a string");
}
_this.client.zcard([channel, _this.PEER_TYPE].join(":"), function (err, reply) {
if (err !== null) {
reject(err);
}
resolve(reply);
});
});
};
RedisWeightedPool.prototype.isEmpty = function (channel) {
var _this = this;
return new Promise(function (resolve, reject) {
if (typeof channel !== "string") {
throw new TypeError("Channel parameter must be a string");
}
_this.client.zcard([channel, _this.PEER_TYPE].join(":"), function (err, reply) {
if (err !== null) {
reject(err);
}
resolve(reply === 0);
});
});
};
RedisWeightedPool.prototype.addPeer = function (channel, key, weight) {
var _this = this;
return new Promise(function (resolve, reject) {
var _a;
if (typeof channel !== "string") {
throw new TypeError("Channel parameter must be a string");
}
if (typeof key !== "string") {
throw new TypeError("Key parameter must be a string");
}
if (typeof weight !== "number") {
throw new TypeError("Weight parameter must be a number");
}
var entries = [];
for (var i = 0; i < weight; i++) {
entries.push(key);
}
(_a = _this.client.multi()
.zadd([channel, _this.PEER_TYPE].join(":"), weight, key)
.lrem([channel, _this.ENTRY_TYPE].join(":"), 0, key)).lpush.apply(_a, [[channel, _this.ENTRY_TYPE].join(":")].concat(entries)).exec(function (err, replies) {
if (err !== null) {
reject(err);
}
resolve();
});
});
};
RedisWeightedPool.prototype.removePeer = function (channel, key) {
var _this = this;
return new Promise(function (resolve, reject) {
if (typeof channel !== "string") {
throw new TypeError("Channel parameter must be a string");
}
if (typeof key !== "string") {
throw new TypeError("Key parameter must be a string");
}
_this.client.multi()
.zrem([channel, _this.PEER_TYPE].join(":"), key)
.lrem([channel, _this.ENTRY_TYPE].join(":"), 0, key)
.exec(function (err, replies) {
if (err !== null) {
reject(err);
}
resolve();
});
});
};
RedisWeightedPool.prototype.getNextPeer = function (channel) {
var _this = this;
return new Promise(function (resolve, reject) {
if (typeof channel !== "string") {
throw new TypeError("Channel parameter must be a string");
}
var chosenIndex = 0;
_this.client.llen([channel, _this.ENTRY_TYPE].join(":"), function (lenError, sumWeights) {
if (lenError !== null) {
reject(lenError);
}
chosenIndex = Math.floor(Math.random() * sumWeights);
_this.client.lindex([channel, _this.ENTRY_TYPE].join(":"), chosenIndex, function (indexError, entry) {
if (indexError !== null) {
reject(indexError);
}
resolve(entry);
});
});
});
};
RedisWeightedPool.prototype.reset = function (channel) {
var _this = this;
return new Promise(function (resolve, reject) {
if (typeof channel !== "string") {
throw new TypeError("Channel parameter must be a string");
}
_this.client.multi()
.del([channel, _this.PEER_TYPE].join(":"))
.del([channel, _this.ENTRY_TYPE].join(":"))
.exec(function (err, replies) {
if (err !== null) {
reject(err);
}
resolve();
});
});
};
return RedisWeightedPool;
}());
exports.RedisWeightedPool = RedisWeightedPool;
//# sourceMappingURL=index.js.map