-
Notifications
You must be signed in to change notification settings - Fork 5
/
redis.js
122 lines (93 loc) · 3.19 KB
/
redis.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
RPS._serverId = Random.id();
const Redis = Npm.require('redis');
const url = Npm.require('url');
RPS._status = {};
RPS._clients = {};
function parseRedisEnvUrl () {
if (process.env.RPS_REDIS_URL) {
const parsedUrl = url.parse(process.env.RPS_REDIS_URL);
if (parsedUrl.protocol === 'redis:' && parsedUrl.hostname && parsedUrl.port) {
const connObj = {
host: parsedUrl.hostname,
port: parseInt(parsedUrl.port)
};
if (parsedUrl.auth) {
connObj.auth = parsedUrl.auth.split(':')[1];
}
return connObj;
} else {
throw new Error(
'RPS_REDIS_URL must contain following url format\n\tredis://redis:<password>@<hostname>:<port>'
);
}
} else {
return null;
}
}
const redisConfig = parseRedisEnvUrl() || {};
let needToResubscribe;
RPS._createRedisClient = function createRedisClient (key, revive) {
const logLabel = 'RPS: [' + key + '] ';
RPS._status[key] = {errors: [], messages: 0, reconnects: 0};
console.info(logLabel + 'connecting to Redis...', redisConfig);
const client = RPS._clients[key] = Redis.createClient(
redisConfig.port,
redisConfig.host,
{
retry_strategy: function (options) {
return Math.min(options.attempt * 100, 3000);
},
password: redisConfig.auth
});
client.on('error', function (err) {
console.error(logLabel + err.toString());
RPS._status[key].errors.push(err.toString());
});
client.on('connect', function () {
console.info(logLabel + 'connected to Redis!');
if (needToResubscribe) {
resubscribe();
needToResubscribe = false;
}
RPS._status[key].connected = true;
});
client.on('reconnecting', function () {
console.info(logLabel + 'reconnecting to Redis...');
RPS._status[key].connected = false;
RPS._status[key].reconnects++;
});
client.on('end', function () {
console.error(logLabel + 'end of the Redis? No...');
RPS._status[key].connected = false;
});
client.on('message', function (channel, messageString) {
RPS._status[key].messages++;
let message;
try {
message = JSON.parse(messageString);
} catch (err) {
console.error(logLabel + 'failed `JSON.parse`; channel: ' + channel + ', messageString: ' + messageString, err.toString());
RPS._status[key].errors.push(err.toString());
}
if (message && message._serverId !== RPS._serverId) {
RPS._messenger.onMessage(channel, message, true);
}
});
}
RPS._createRedisClient('pub');
RPS._createRedisClient('sub');
RPS._sub = function (channel) {
RPS._clients.sub.subscribe(channel);
};
RPS._unsub = function (channel) {
RPS._clients.sub.unsubscribe(channel);
};
RPS._pub = function (channel, message) {
RPS._status.pub.messages++;
RPS._clients.pub.publish(channel, message);
};
function resubscribe () {
_.each(RPS._messenger.channels, function (observerKeys, channel) {
RPS._sub(channel);
});
}