forked from qishibo/AnotherRedisDesktopManager
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathredisClient.js
78 lines (64 loc) · 2.04 KB
/
redisClient.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
import redis from 'redis';
import bluebird from 'bluebird';
import tunnelssh from 'tunnel-ssh';
bluebird.promisifyAll(redis);
export default {
createConnection(host, port, auth, menuIndex = null) {
const options = {
connect_timeout: 2000,
// max_attempts: 3,
retry_strategy: (options) => {return this.retryStragety(options, {host: host, port: port})},
no_ready_check: true,
menu_index: menuIndex,
password: auth,
};
const client = redis.createClient(port, host, options);
return client;
},
createSSHConnection(sshOptions, host, port, auth, menuIndex = null) {
const options = {
retry_strategy: (options) => {return this.retryStragety(options, {host: host, port: port})},
no_ready_check: true,
menu_index: menuIndex,
password: auth,
};
const config = {
username: sshOptions.username,
password: sshOptions.password,
host: sshOptions.host,
port: sshOptions.port,
dstHost: host,
dstPort: port,
localHost: '127.0.0.1',
localPort: null,
};
const sshPromise = new bluebird((resolve, reject) => {
tunnelssh(config, function (error, server) {
const listenAddress = server.address();
console.log('ssh tunnel listening in', listenAddress);
server.on('error', (error) => {
alert('SSH Connection Error: ' + error.message);
reject(error);
});
const client = redis.createClient(listenAddress.port, listenAddress.address, options);
if (error) {
reject(error);
}
else {
resolve(client);
}
});
});
return sshPromise;
},
retryStragety(options, connection) {
console.log('retrying...', options, connection);
const maxRetryTimes = 3;
if (options.attempt >= maxRetryTimes) {
alert(`${connection.host}:${connection.port}\nToo Many Attempts To Reconnect. Please Check The Server Status!`);
return false;
}
// reconnect after
return Math.max(options.attempt * 200, 1000);
},
};