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

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 _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()
if(config.trafficMax > 0)
{
const path = `/sys/class/net/${config.trafficInterface}/statistics/rx_bytes`
if(fs.existsSync(path))
{
let traffic = 0
setInterval(() => {
fs.readFile('/sys/class/net/enp2s0/statistics/rx_bytes', (err, data) => {
if(!err)
fs.readFile(path, (err, newTraffic) => {
if(err)
return
if(this.traffic === 0)
this.traffic = data
if(traffic === 0)
traffic = newTraffic
this.trafficSpeed = data - this.traffic
this.trafficSpeed = (newTraffic - traffic) / config.trafficUpdateTime
console.log(this.trafficSpeed / 1024, 'kbps/s')
trafficDebug('traffic rx', this.trafficSpeed / 1024, 'kbps/s')
this.traffic = data
traffic = newTraffic
})
}, 1000)
}, 1000 * config.trafficUpdateTime)
}
}
}
}

View File

@ -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');