исправление трафика

This commit is contained in:
Alexey Kasyanchuk
2017-08-24 11:07:33 +03:00
parent c11a60ea63
commit 2795be2e42
2 changed files with 31 additions and 14 deletions

View File

@ -10,7 +10,8 @@ const config = require('../config')
const fs = require('fs') const fs = require('fs')
const _debug = require('debug') const _debug = require('debug')
const cpuDebug = _debug('cpu') const cpuDebug = _debug('spider:cpu')
const trafficDebug = _debug('spider:traffic')
const bootstraps = [{ const bootstraps = [{
address: 'router.bittorrent.com', address: 'router.bittorrent.com',
@ -45,7 +46,6 @@ class Spider extends Emiter {
this.client = client this.client = client
this.ignore = false; // ignore all requests this.ignore = false; // ignore all requests
this.initialized = false; this.initialized = false;
this.traffic = 0;
this.trafficSpeed = 0 this.trafficSpeed = 0
this.walkInterval = config.spider.walkInterval; this.walkInterval = config.spider.walkInterval;
@ -79,7 +79,11 @@ class Spider extends Emiter {
walk() { walk() {
if(!this.client || this.client.isIdle()) { 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() const node = this.table.shift()
if (node) { if (node) {
@ -217,21 +221,30 @@ class Spider extends Emiter {
}, 3000) }, 3000)
this.join() this.join()
this.walk() this.walk()
setInterval(() => {
fs.readFile('/sys/class/net/enp2s0/statistics/rx_bytes', (err, data) => {
if(!err)
return
if(this.traffic === 0) if(config.trafficMax > 0)
this.traffic = data {
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 trafficDebug('traffic rx', this.trafficSpeed / 1024, 'kbps/s')
})
}, 1000) traffic = newTraffic
})
}, 1000 * config.trafficUpdateTime)
}
}
} }
} }

View File

@ -33,6 +33,10 @@ let config = {
cleanupDiscLimit: 7 * 1024 * 1024 * 1024, cleanupDiscLimit: 7 * 1024 * 1024 * 1024,
spaceQuota: false, spaceQuota: false,
spaceDiskLimit: 7 * 1024 * 1024 * 1024, spaceDiskLimit: 7 * 1024 * 1024 * 1024,
trafficInterface: 'enp2s0',
trafficMax: 0,
trafficUpdateTime: 3, //secs
} }
const fs = require('fs'); const fs = require('fs');