From 2795be2e42d13bf49045611c259a772a9a10ce88 Mon Sep 17 00:00:00 2001 From: Alexey Kasyanchuk Date: Thu, 24 Aug 2017 11:07:33 +0300 Subject: [PATCH] =?UTF-8?q?=D0=B8=D1=81=D0=BF=D1=80=D0=B0=D0=B2=D0=BB?= =?UTF-8?q?=D0=B5=D0=BD=D0=B8=D0=B5=20=D1=82=D1=80=D0=B0=D1=84=D0=B8=D0=BA?= =?UTF-8?q?=D0=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- bt/spider.js | 41 +++++++++++++++++++++++++++-------------- config.js | 4 ++++ 2 files changed, 31 insertions(+), 14 deletions(-) diff --git a/bt/spider.js b/bt/spider.js index 93c3f48..0a76dfb 100644 --- a/bt/spider.js +++ b/bt/spider.js @@ -10,7 +10,8 @@ const config = require('../config') const fs = require('fs') const _debug = require('debug') -const cpuDebug = _debug('cpu') +const cpuDebug = _debug('spider:cpu') +const trafficDebug = _debug('spider:traffic') const bootstraps = [{ address: 'router.bittorrent.com', @@ -45,7 +46,6 @@ class Spider extends Emiter { this.client = client this.ignore = false; // ignore all requests this.initialized = false; - this.traffic = 0; this.trafficSpeed = 0 this.walkInterval = config.spider.walkInterval; @@ -79,7 +79,11 @@ class Spider extends Emiter { walk() { if(!this.client || this.client.isIdle()) { - if(!this.ignore && (this.cpuLimit <= 0 || cpuUsage() < this.cpuLimit + this.cpuInterval)) + if( + !this.ignore + && (this.cpuLimit <= 0 || cpuUsage() < this.cpuLimit + this.cpuInterval) + && (config.trafficMax <= 0 || this.trafficSpeed == 0 || this.trafficSpeed < config.trafficMax) + ) { const node = this.table.shift() if (node) { @@ -217,21 +221,30 @@ class Spider extends Emiter { }, 3000) this.join() this.walk() - setInterval(() => { - fs.readFile('/sys/class/net/enp2s0/statistics/rx_bytes', (err, data) => { - if(!err) - return - if(this.traffic === 0) - this.traffic = data + if(config.trafficMax > 0) + { + const path = `/sys/class/net/${config.trafficInterface}/statistics/rx_bytes` + if(fs.existsSync(path)) + { + let traffic = 0 + setInterval(() => { + fs.readFile(path, (err, newTraffic) => { + if(err) + return - this.trafficSpeed = data - this.traffic + if(traffic === 0) + traffic = newTraffic - console.log(this.trafficSpeed / 1024, 'kbps/s') + this.trafficSpeed = (newTraffic - traffic) / config.trafficUpdateTime - this.traffic = data - }) - }, 1000) + trafficDebug('traffic rx', this.trafficSpeed / 1024, 'kbps/s') + + traffic = newTraffic + }) + }, 1000 * config.trafficUpdateTime) + } + } } } diff --git a/config.js b/config.js index 52fac50..2274209 100644 --- a/config.js +++ b/config.js @@ -33,6 +33,10 @@ let config = { cleanupDiscLimit: 7 * 1024 * 1024 * 1024, spaceQuota: false, spaceDiskLimit: 7 * 1024 * 1024 * 1024, + + trafficInterface: 'enp2s0', + trafficMax: 0, + trafficUpdateTime: 3, //secs } const fs = require('fs');