Skip to content

Commit cca327a

Browse files
committed
Added keep-alive for clients.
1 parent 4846d9d commit cca327a

File tree

5 files changed

+35
-2
lines changed

5 files changed

+35
-2
lines changed

CHANGES.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# Changes Logs
22

3+
## v0.1.7
4+
5+
- Added keep-alive for clients.
6+
37
## v0.1.6
48

59
- Auto switch to the selected database.

package-lock.json

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@litert/redis",
3-
"version": "0.1.6",
3+
"version": "0.1.7",
44
"description": "A redis protocol implement for Node.js.",
55
"main": "./dist/index.js",
66
"scripts": {

sources/lib/RedisClient.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ implements Abstract.RedisClient {
3535

3636
private _listeners!: Dict<Array<Abstract.SubscriptionCallback | Readable>>;
3737

38+
private _keepAlive: any;
39+
3840
public constructor(
3941
connection: Net.Socket,
4042
host: string,
@@ -45,6 +47,14 @@ implements Abstract.RedisClient {
4547

4648
super(connection, host, port, createDecoder, createEncoder);
4749

50+
this._keepAlive = setInterval(() => {
51+
52+
if (this._status === Abstract.ClientStatus.NORMAL) {
53+
54+
this.executeNow("PING").catch((e) => this.emit("error", e));
55+
}
56+
}, 5000);
57+
4858
this._database = 0;
4959
}
5060

@@ -1313,6 +1323,8 @@ implements Abstract.RedisClient {
13131323
delete this._subscriber;
13141324
}
13151325

1326+
clearInterval(this._keepAlive);
1327+
13161328
return super.close();
13171329
}
13181330

sources/lib/SubscriberClient.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ implements Abstract.ProtocolClient {
3434

3535
protected _password: string;
3636

37+
private _keepAlive: any;
38+
3739
public constructor(
3840
connection: Net.Socket,
3941
host: string,
@@ -44,6 +46,14 @@ implements Abstract.ProtocolClient {
4446
) {
4547
super(connection, host, port, createDecoder, createEncoder);
4648

49+
this._keepAlive = setInterval(() => {
50+
51+
if (this._status === Abstract.ClientStatus.NORMAL) {
52+
53+
this.executeNow("PING").catch((e) => this.emit("error", e));
54+
}
55+
}, 5000);
56+
4757
this._subjects = [];
4858
this._patterns = [];
4959
this._password = password;
@@ -316,6 +326,13 @@ implements Abstract.ProtocolClient {
316326

317327
return ret;
318328
}
329+
330+
public async close(): Promise<void> {
331+
332+
clearInterval(this._keepAlive);
333+
334+
return super.close();
335+
}
319336
}
320337

321338
export default SubscriberClient;

0 commit comments

Comments
 (0)