Skip to content
This repository has been archived by the owner on Oct 30, 2018. It is now read-only.

Implement SIP9 #136

Merged
merged 25 commits into from
May 3, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
16 changes: 15 additions & 1 deletion lib/models/contact.js
Original file line number Diff line number Diff line change
Expand Up @@ -142,9 +142,20 @@ ContactSchema.methods.recordPoints = function(points) {
return this;
};

/**
* Will reset the timeout rate after 72 hours without a timeout.
*/
ContactSchema.methods.resetTimeoutRate = function() {
const now = Date.now();
const window = 259200000; // 72 hours
if (this.lastTimeout < now - window) {

Choose a reason for hiding this comment

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

Date.now() is expensive. How about this:
if (this.timeoutRate && this.lastTimeout < Date.now() - window)

That should call Date.now() only if the farmer has a timeoutRate that could be reset.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks, updated.

this.timeoutRate = 0;
}
};

/**
* Will update the lastTimeout and calculate the timeoutRate based
* on a 24 hour window of activity.
* on a 72 hour window of activity.
*/
ContactSchema.methods.recordTimeoutFailure = function() {
const now = Date.now();
Expand Down Expand Up @@ -191,6 +202,9 @@ ContactSchema.methods.recordResponseTime = function(responseTime) {

this.responseTime = newResponseTime;

// Make sure to reset timeout rate if possible
this.resetTimeoutRate();

return this;
};

Expand Down
33 changes: 33 additions & 0 deletions test/contact.unit.js
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,31 @@ describe('Storage/models/Contact', function() {
});
});

describe('#resetTimeoutRate', function() {
it('will reset the timeoutRate after 72 hour window', function() {
const past = new Date(new Date().getTime() - 259200000 - 1);
const contact = new Contact({
lastSeen: Date.now(),
lastTimeout: past,
timeoutRate: 1
});
expect(contact.timeoutRate).to.equal(1);
contact.resetTimeoutRate();
expect(contact.timeoutRate).to.equal(0);
});
it('will not reset the timeoutRate before 72 hour window', function() {
const past = new Date(new Date().getTime() - 259200000 + 1);
const contact = new Contact({
lastSeen: Date.now(),
lastTimeout: past,
timeoutRate: 1
});
expect(contact.timeoutRate).to.equal(1);
contact.resetTimeoutRate();
expect(contact.timeoutRate).to.equal(1);
});
});

describe('#recordTimeoutFailure', function() {
const sandbox = sinon.sandbox.create();
afterEach(() => sandbox.restore());
Expand Down Expand Up @@ -355,6 +380,14 @@ describe('Storage/models/Contact', function() {
expect(contact.responseTime).to.be.above(10000);
});

it('will reset timeout rate', function() {
const contact = new Contact({});
contact.resetTimeoutRate = sinon.stub();
contact.recordResponseTime(15000);
expect(contact.responseTime).to.be.above(10000);
expect(contact.resetTimeoutRate.callCount).to.equal(1);
});

it('will improve response times with a fast response', function() {
const contact = new Contact({});
contact.recordResponseTime(100);
Expand Down