Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add statusChanged getter #51

Merged
merged 1 commit into from Jun 20, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
10 changes: 8 additions & 2 deletions README.md
Expand Up @@ -98,11 +98,17 @@ req.on('error', e => {
req.end();

setTimeout(() => {
console.log('agent status: %j', keepaliveAgent.getCurrentStatus());
if (keepaliveAgent.statusChanged) {
console.log('[%s] agent status changed: %j', Date(), keepaliveAgent.getCurrentStatus());
}
}, 2000);

```

### `getter agent.statusChanged`

counters have change or not after last checkpoint.

### `agent.getCurrentStatus()`

`agent.getCurrentStatus()` will return a object to show the status of this agent:
Expand Down Expand Up @@ -218,8 +224,8 @@ Socket created:
```
(The MIT License)

Copyright(c) 2012 - 2015 fengmk2 <fengmk2@gmail.com>
Copyright(c) node-modules and other contributors.
Copyright(c) 2012 - 2015 fengmk2 <fengmk2@gmail.com>

Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
Expand Down
30 changes: 30 additions & 0 deletions lib/agent.js
Expand Up @@ -39,12 +39,24 @@ class Agent extends OriginalAgent {
super(options);

this.createSocketCount = 0;
this.createSocketCountLastCheck = 0;

this.createSocketErrorCount = 0;
this.createSocketErrorCountLastCheck = 0;

this.closeSocketCount = 0;
this.closeSocketCountLastCheck = 0;

// socket error event count
this.errorSocketCount = 0;
this.errorSocketCountLastCheck = 0;

this.requestCount = 0;
this.requestCountLastCheck = 0;

this.timeoutSocketCount = 0;
this.timeoutSocketCountLastCheck = 0;

this.on('free', s => {
this.requestCount++;
// last enter free queue timestamp
Expand Down Expand Up @@ -77,6 +89,24 @@ class Agent extends OriginalAgent {
});
}

get statusChanged() {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

先判断是否 status 变化了,避免大量 inspect 操作

const changed = this.createSocketCount !== this.createSocketCountLastCheck ||
this.createSocketErrorCount !== this.createSocketErrorCountLastCheck ||
this.closeSocketCount !== this.closeSocketCountLastCheck ||
this.errorSocketCount !== this.errorSocketCountLastCheck ||
this.timeoutSocketCount !== this.timeoutSocketCountLastCheck ||
this.requestCount !== this.requestCountLastCheck;
if (changed) {
this.createSocketCountLastCheck = this.createSocketCount;
this.createSocketErrorCountLastCheck = this.createSocketErrorCount;
this.closeSocketCountLastCheck = this.closeSocketCount;
this.errorSocketCountLastCheck = this.errorSocketCount;
this.timeoutSocketCountLastCheck = this.timeoutSocketCount;
this.requestCountLastCheck = this.requestCount;
}
return changed;
}

getCurrentStatus() {
return {
createSocketCount: this.createSocketCount,
Expand Down
50 changes: 49 additions & 1 deletion test/agent.test.js
Expand Up @@ -996,12 +996,60 @@ describe('test/agent.test.js', () => {
it('should get current agent status', () => {
const status = agentkeepalive.getCurrentStatus();
assert.deepEqual(Object.keys(status), [
'createSocketCount', 'createSocketErrorCount', 'closeSocketCount', 'errorSocketCount', 'timeoutSocketCount',
'createSocketCount', 'createSocketErrorCount', 'closeSocketCount',
'errorSocketCount', 'timeoutSocketCount',
'requestCount', 'freeSockets', 'sockets', 'requests',
]);
});
});

describe('getter statusChanged', () => {
it('should get statusChanged', () => {
const agentkeepalive = new Agent({
keepAliveTimeout: 1000,
maxSockets: 5,
maxFreeSockets: 5,
});
assert(agentkeepalive.statusChanged === false);
assert(agentkeepalive.statusChanged === false);
agentkeepalive.createSocketCount++;
assert(agentkeepalive.createSocketCount !== agentkeepalive.createSocketCountLastCheck);
assert(agentkeepalive.statusChanged === true);
assert(agentkeepalive.createSocketCount === agentkeepalive.createSocketCountLastCheck);
assert(agentkeepalive.statusChanged === false);

agentkeepalive.createSocketErrorCount++;
assert(agentkeepalive.createSocketErrorCount !== agentkeepalive.createSocketErrorCountLastCheck);
assert(agentkeepalive.statusChanged === true);
assert(agentkeepalive.createSocketErrorCount === agentkeepalive.createSocketErrorCountLastCheck);
assert(agentkeepalive.statusChanged === false);

agentkeepalive.closeSocketCount++;
assert(agentkeepalive.closeSocketCount !== agentkeepalive.closeSocketCountLastCheck);
assert(agentkeepalive.statusChanged === true);
assert(agentkeepalive.closeSocketCount === agentkeepalive.closeSocketCountLastCheck);
assert(agentkeepalive.statusChanged === false);

agentkeepalive.errorSocketCount++;
assert(agentkeepalive.errorSocketCount !== agentkeepalive.errorSocketCountLastCheck);
assert(agentkeepalive.statusChanged === true);
assert(agentkeepalive.errorSocketCount === agentkeepalive.errorSocketCountLastCheck);
assert(agentkeepalive.statusChanged === false);

agentkeepalive.timeoutSocketCount++;
assert(agentkeepalive.timeoutSocketCount !== agentkeepalive.timeoutSocketCountLastCheck);
assert(agentkeepalive.statusChanged === true);
assert(agentkeepalive.timeoutSocketCount === agentkeepalive.timeoutSocketCountLastCheck);
assert(agentkeepalive.statusChanged === false);

agentkeepalive.requestCount++;
assert(agentkeepalive.requestCount !== agentkeepalive.requestCountLastCheck);
assert(agentkeepalive.statusChanged === true);
assert(agentkeepalive.requestCount === agentkeepalive.requestCountLastCheck);
assert(agentkeepalive.statusChanged === false);
});
});

describe('mock idle socket error', () => {
it('should idle socket emit error event', done => {
const agent = new Agent();
Expand Down