Skip to content

Commit

Permalink
feat: add statusChanged getter (#51)
Browse files Browse the repository at this point in the history
detect status changed or not
  • Loading branch information
fengmk2 authored and dead-horse committed Jun 20, 2017
1 parent 6235a97 commit 427ab21
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 3 deletions.
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() {
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

0 comments on commit 427ab21

Please sign in to comment.