Skip to content

Commit

Permalink
feat: add config.client.weakDependent (#30)
Browse files Browse the repository at this point in the history
  • Loading branch information
dead-horse authored Jun 13, 2019
1 parent 2d866f7 commit 5fbd718
Show file tree
Hide file tree
Showing 7 changed files with 82 additions and 2 deletions.
18 changes: 16 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,8 @@ config.redis = {
config.redis = {
client: {
sentinels: [{ // Sentinel instances
port: 26379, // Sentinel port
host: '127.0.0.1', // Sentinel host
port: 26379, // Sentinel port
host: '127.0.0.1', // Sentinel host
}],
name: 'mymaster', // Master name
password: 'auth',
Expand Down Expand Up @@ -133,6 +133,20 @@ config.redis = {
}
```

**weakDependent**

```javascript
config.redis = {
client: {
port: 6379, // Redis port
host: '127.0.0.1', // Redis host
password: 'auth',
db: 0,
weakDependent: true, // this redis instance won't block app start
},
}
```

## Usage

In controller, you can use `app.redis` to get the redis instance, check [ioredis](https://github.com/luin/ioredis#basic-usage) to see how to use.
Expand Down
8 changes: 8 additions & 0 deletions lib/redis.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,14 @@ function createClient(config, app) {

app.beforeStart(async () => {
const index = count++;
if (config.weakDependent) {
app.coreLogger.info(`[egg-redis] instance[${index}] is weak dependent and won't block app start`);
client.once('ready', () => {
app.coreLogger.info(`[egg-redis] instance[${index}] status OK`);
});
return;
}

await awaitFirst(client, [ 'ready', 'error' ]);
app.coreLogger.info(`[egg-redis] instance[${index}] status OK, client ready`);
});
Expand Down
11 changes: 11 additions & 0 deletions test/fixtures/apps/redisapp-weakdependent/app/controller/home.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
'use strict';

module.exports = app => {
return class HomeController extends app.Controller {
* index() {
const { ctx, app } = this;
yield app.redis.set('foo', 'bar');
ctx.body = yield app.redis.get('foo');
}
};
};
5 changes: 5 additions & 0 deletions test/fixtures/apps/redisapp-weakdependent/app/router.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
'use strict';

module.exports = function(app) {
app.get('/', 'home.index');
};
20 changes: 20 additions & 0 deletions test/fixtures/apps/redisapp-weakdependent/config/config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
'use strict';

exports.redis = {
client: {
weakDependent: true,
host: '127.0.0.1',
port: 6379,
password: '',
db: '0',
},
agent:true,
};

exports.logger = {
coreLogger: {
level: 'INFO',
},
};

exports.keys = 'keys';
3 changes: 3 additions & 0 deletions test/fixtures/apps/redisapp-weakdependent/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"name": "redisapp-weakdependent"
}
19 changes: 19 additions & 0 deletions test/redis.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,25 @@ describe('test/redis.test.js', () => {
});
});

describe('weak dependent', () => {
let app;
before(async () => {
app = mm.app({
baseDir: 'apps/redisapp-weakdependent',
});
await app.ready();
});
after(() => app.close());
afterEach(mm.restore);

it('should query', () => {
return request(app.callback())
.get('/')
.expect(200)
.expect('bar');
});
});

describe('single client supportTimeCommand = false', () => {
let app;
before(async () => {
Expand Down

0 comments on commit 5fbd718

Please sign in to comment.