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

Commit

Permalink
Merge e6350a3 into d3dd8ae
Browse files Browse the repository at this point in the history
  • Loading branch information
braydonf committed Jan 12, 2017
2 parents d3dd8ae + e6350a3 commit 857a366
Show file tree
Hide file tree
Showing 3 changed files with 168 additions and 1 deletion.
33 changes: 33 additions & 0 deletions lib/models/contact.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ var ContactSchema = new mongoose.Schema({
type: Date,
required: false
},
timeoutRate: {
type: Number,
required: false
},
responseTime: {
type: Number,
required: false
Expand All @@ -45,6 +49,8 @@ var ContactSchema = new mongoose.Schema({

ContactSchema.plugin(SchemaOptions);

ContactSchema.index({lastSeen: 1});

ContactSchema.set('toObject', {
virtuals: true,
transform: function(doc, ret) {
Expand Down Expand Up @@ -81,6 +87,33 @@ ContactSchema.statics.record = function(contactInfo, callback) {
}, done);
};

/**
* Will update the lastTimeout and calculate the timeoutRate based
* on a 24 hour window of activity.
*/
ContactSchema.methods.recordTimeoutFailure = function() {
const now = Date.now();
const window = 86400000; // 24 hours
const lastTimeoutRate = this.timeoutRate || 0;

if (this.lastTimeout && this.lastTimeout > this.lastSeen) {
const offlineTime = now - this.lastTimeout.getTime();
const timeoutRate = offlineTime / window;

this.timeoutRate = Math.min(lastTimeoutRate + timeoutRate, 1);

} else if (this.lastTimeout && this.lastTimeout < this.lastSeen) {
const onlineTime = this.lastSeen.getTime() - this.lastTimeout.getTime();
const successRate = onlineTime / window;

this.timeoutRate = Math.max(lastTimeoutRate - successRate, 0);
}

this.lastTimeout = now;

return this;
};

/**
* Update the exponential moving average response time
* for calls to this contact.
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
"mongoose-int32": "^0.1.0",
"mongoose-types": "^1.0.3",
"ms": "^0.7.1",
"storj-lib": "^6.0.0",
"storj-lib": "^6.0.12",
"storj-service-error-types": "^1.0.0",
"stripe": "^4.11.0"
},
Expand Down
134 changes: 134 additions & 0 deletions test/contact.unit.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
'use strict';

const sinon = require('sinon');
const async = require('async');
const expect = require('chai').expect;
const mongoose = require('mongoose');
const storj = require('storj-lib');

/*jshint maxstatements: 100 */

require('mongoose-types').loadTypes(mongoose);

const ContactSchema = require('../lib/models/contact');
Expand Down Expand Up @@ -57,6 +60,137 @@ describe('Storage/models/Contact', function() {

});

describe('#recordTimeoutFailure', function() {
const sandbox = sinon.sandbox.create();
afterEach(() => sandbox.restore());

it('will set last timeout', function() {
const contact = new Contact({});
contact.recordTimeoutFailure();

expect(contact.lastTimeout).to.be.above(Date.now() - 5000);
expect(contact.lastTimeout).to.be.below(Date.now() + 5000);

});

it('will set timeout rate on repeat timeout failures', function() {
const clock = sandbox.useFakeTimers();
const contact = new Contact({
lastSeen: Date.now()
});

clock.tick(600000); // 10 min
contact.recordTimeoutFailure();

clock.tick(600000); // 10 min
contact.recordTimeoutFailure();

expect(contact.timeoutRate.toFixed(4)).to.equal('0.0069');
});

it('0.5 after 12 hours of failure', function() {
const clock = sandbox.useFakeTimers();
const contact = new Contact({
lastSeen: Date.now()
});

clock.tick(600000); // 10 min
contact.recordTimeoutFailure();

// 12 repeated failures, each over an hour
for (var i = 0; i < 12; i++) {
clock.tick(3600000); // 1 hour
contact.recordTimeoutFailure();
}

expect(contact.timeoutRate.toFixed(4)).to.equal('0.5000');
});

it('1 after 24 hours of failure', function() {
const clock = sandbox.useFakeTimers();
const contact = new Contact({
lastSeen: Date.now()
});

clock.tick(600000); // 10 min
contact.recordTimeoutFailure();

clock.tick(600000); // 10 min
contact.recordTimeoutFailure();

// 24 repeated failures, each over an hour
for (var i = 0; i < 24; i++) {
clock.tick(3600000); // 1 hour
contact.recordTimeoutFailure();
}

expect(contact.timeoutRate.toFixed(4)).to.equal('1.0000');
});

it('0.45 after 12 hours of failure (w/ sparce success)', function() {
const clock = sandbox.useFakeTimers();
const contact = new Contact({
lastSeen: Date.now()
});

// 10 minutes passed by before there was the first timeout failure
clock.tick(600000);
contact.recordTimeoutFailure();

// And then 6 repeated failures
for (let i = 0; i < 6; i++) {
clock.tick(3600000); // 1 hour
contact.recordTimeoutFailure();
}

// 10 minutes passed and there was a successful query
clock.tick(600000);
contact.lastSeen = Date.now();

// And then again, followed by 6 repeated failures
for (let i = 0; i < 6; i++) {
clock.tick(3600000);
contact.recordTimeoutFailure();
}

expect(contact.timeoutRate.toFixed(2)).to.equal('0.45');
});

it('will reset after 24 hours', function() {
const clock = sandbox.useFakeTimers();
const contact = new Contact({
lastSeen: Date.now()
});

// 10 minutes passed by before there was the first timeout failure
clock.tick(600000);
contact.recordTimeoutFailure();

// And then 6 repeated failures
for (let i = 0; i < 6; i++) {
clock.tick(3600000); // 1 hour
contact.recordTimeoutFailure();
}

expect(contact.timeoutRate.toFixed(2)).to.equal('0.25');

// 24 hours passed with successful queries
for (let i = 0; i < 24; i++) {
clock.tick(3600000); // 1 hour
contact.lastSeen = Date.now();
}

// And then again, followed by 2 repeated failures
for (let i = 0; i < 2; i++) {
clock.tick(3600000);
contact.recordTimeoutFailure();
}

expect(contact.timeoutRate.toFixed(2)).to.equal('0.04');
});

});

describe('#recordResponseTime', function() {

it('will throw if number is not finite (NaN)', function() {
Expand Down

0 comments on commit 857a366

Please sign in to comment.