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

Commit

Permalink
Merge pull request #722 from aleitner/du
Browse files Browse the repository at this point in the history
periodically check space used with du
  • Loading branch information
braydonf authored Sep 6, 2017
2 parents 4149f11 + 93ac868 commit 5adf51f
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 5 deletions.
48 changes: 45 additions & 3 deletions lib/network/farmer.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ const merge = require('merge');
const constants = require('../constants');
const utils = require('../utils');
const {execFile} = require('child_process');
const du = require('du');

/**
* Creates and a new farmer interface
Expand Down Expand Up @@ -83,6 +84,9 @@ function FarmerInterface(options) {
this._connectBridgesInterval = null;

this.spaceAvailable = true;
if (options.spaceAvailable !== undefined) {
this.spaceAvailable = options.spaceAvailable;
}

// Connecting the dots. Note: We should organize this better
// so that it's not necesarry to set these after they
Expand All @@ -95,6 +99,7 @@ function FarmerInterface(options) {
inherits(FarmerInterface, Network);

FarmerInterface.CONNECT_BRIDGE_INTERVAL = 10000;
FarmerInterface.CHECK_SPACE_USED_INTERVAL = 86400000;

FarmerInterface.prototype._deprecatedConfigV1 = function(options) {
if (options.renterWhitelist) {
Expand Down Expand Up @@ -294,20 +299,52 @@ FarmerInterface.prototype.connectBridges = function() {
);
};

/**
* Mark if there is space available for accepting offers
*/
FarmerInterface.prototype.runSpaceCheck = function() {
this._runSpaceCheck();
this._spaceCheckInterval = setInterval(
this._runSpaceCheck.bind(this),
FarmerInterface.CHECK_SPACE_USED_INTERVAL
);
}

FarmerInterface.prototype._runSpaceCheck = function() {
du(this._options.storagePath, {
filter: function(f) {
return f.indexOf('contracts.db') !== -1 ||
f.indexOf('sharddata.kfs') !== -1;
}
}, (err, size) => {
if (err) {
return this._logger.error(err);
}

if (size >= this.storageManager._options.maxCapacity) {
this.noSpaceLeft(true);
} else {
this.noSpaceLeft(false);
}
});
}

/**
* This will change the state of the farmer so that it will stop receiving
* messages to store data.
*/
FarmerInterface.prototype.noSpaceLeft = function() {
FarmerInterface.prototype.noSpaceLeft = function(noSpace) {
if (this.spaceAvailable === !noSpace) {
return;
}

this.spaceAvailable = false;
this.spaceAvailable = !noSpace;
let keys = this.bridges.keys();

for (let i = 0; i < keys.length; i++) {
// Set to false so that the contact will be updated
this.bridges.get(keys[i]).connected = false;
}

};

FarmerInterface.prototype._connectBridge = function(bridge, callback) {
Expand Down Expand Up @@ -591,6 +628,11 @@ FarmerInterface.prototype._negotiateContract = function(contract, contact) {
FarmerInterface.prototype._shouldSendOffer = function(contract, callback) {
var self = this;

if (!this.spaceAvailable) {
self._logger.debug('No space available to accept offer');
return callback(false);
}

this._negotiator.call(this, contract, function(shouldNegotiate) {
/* eslint max-statements: [2, 16] */
self._logger.debug('negotiator returned: %s', shouldNegotiate);
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "storj-lib",
"version": "7.0.1",
"version": "7.0.2",
"description": "implementation of the storj protocol for node.js and the browser",
"main": "index.js",
"directories": {
Expand Down Expand Up @@ -72,6 +72,7 @@
"bitcore-mnemonic": "=1.1.1",
"concat-stream": "^1.6.0",
"diglet": "^1.0.6",
"du": "^0.1.0",
"fs-blob-store": "^5.2.1",
"hdkey": "^0.7.1",
"ip": "littleskunk/node-ip#b1e7ec0cbff9f7841b1584e50339a774363437c0",
Expand Down
2 changes: 1 addition & 1 deletion test/network/farmer.unit.js
Original file line number Diff line number Diff line change
Expand Up @@ -528,7 +528,7 @@ describe('FarmerInterface', function() {
});

expect(farmer.spaceAvailable).to.equal(true);
farmer.noSpaceLeft();
farmer.noSpaceLeft(true);
expect(farmer.spaceAvailable).to.equal(false);
expect(farmer.bridges.get(extendedKey1).connected).to.equal(false);
expect(farmer.bridges.get(extendedKey2).connected).to.equal(false);
Expand Down

0 comments on commit 5adf51f

Please sign in to comment.