Skip to content

Commit

Permalink
feat: reconnect if command timeout
Browse files Browse the repository at this point in the history
  • Loading branch information
juneil committed Dec 21, 2018
1 parent d12430c commit 4fe8d09
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 30 deletions.
52 changes: 26 additions & 26 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@hapiness/redis",
"version": "1.3.0",
"version": "1.4.0",
"description": "Hapiness module for redis",
"main": "commonjs/index.js",
"types": "index.d.ts",
Expand Down
1 change: 1 addition & 0 deletions src/module/interfaces/redis.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ import { ClientOpts } from 'redis';
export interface RedisConfig extends ClientOpts {
reconnect_interval?: number;
ping_keepalive_interval?: number; // In seconds
command_timeout?: number;
}
14 changes: 12 additions & 2 deletions src/module/managers/redis-client.manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import * as redis_commands from 'redis-commands';

import * as EventEmitter from 'events';

import { Observable } from 'rxjs';
import { Observable, TimeoutError } from 'rxjs';
import { URL } from 'url';

import { RedisConfig } from '../interfaces';
Expand Down Expand Up @@ -148,7 +148,17 @@ export class RedisClientManager {
observer.complete();
}
});
});
}).timeout((this._config.command_timeout || 2) * 1000).retryWhen(e =>
e.flatMap(err => {
if (err instanceof TimeoutError) {
this._client.end(true);
debug(`Got a timeout on ${command}`);
return this.createClient();
} else {
return Observable.throw(err);
}
})
);
}

public get client(): RedisClient {
Expand Down
5 changes: 4 additions & 1 deletion test/mocks/redis.mock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ export class FakeRedisClient extends EventEmitter {
}

get(param: string, cb: redis.Callback<string>) {
cb(null, param);
setTimeout(() => cb(null, param), 250);
// cb(null, param);
return true;
}

Expand All @@ -32,4 +33,6 @@ export class FakeRedisClient extends EventEmitter {
cb(null, null);
return true;
}

end() {}
}
25 changes: 25 additions & 0 deletions test/unit/redis-client.manager.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -168,4 +168,29 @@ export class RedisClientManagerTest {
}
);
}

// @test.only('- ')
// test(done) {
// const fakeInst = new FakeRedisClient();

// const redisStub = mockRedisCreateConnection();
// redisStub.returns(<any>fakeInst);

// Observable
// .of(fakeInst)
// .delay(new Date(Date.now() + 1500))
// .map(_ => fakeInst.emit('ready'))
// .subscribe();

// const manager = new RedisClientManager(
// {
// url: '//toto',
// password: 'pass_redis',
// db: '2'
// }
// );

// manager.createClient().flatMap(_ => manager.sendCommand('get', []))
// .subscribe(_ => { console.log('NEXT'); done() }, e => { console.log(e); done(e) });
// }
}

0 comments on commit 4fe8d09

Please sign in to comment.