feat(config): some network limitations

This commit is contained in:
Alexey Kasyanchuk
2018-03-01 04:22:21 +03:00
parent bdaaa020cc
commit 9a95903cd5
2 changed files with 7 additions and 27 deletions

View File

@ -5,14 +5,9 @@ const Emiter = require('events')
const bencode = require('bencode') const bencode = require('bencode')
const {Table, Node} = require('./table') const {Table, Node} = require('./table')
const Token = require('./token') const Token = require('./token')
const cpuUsage = require('./cpu-usage')
const config = require('../config') const config = require('../config')
const fs = require('fs') const fs = require('fs')
const _debug = require('debug')
const cpuDebug = _debug('spider:cpu')
const trafficDebug = _debug('spider:traffic')
const bootstraps = [{ const bootstraps = [{
address: 'router.bittorrent.com', address: 'router.bittorrent.com',
port: 6881 port: 6881
@ -45,21 +40,16 @@ 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.trafficSpeed = 0
this.walkInterval = config.spider.walkInterval; this.walkInterval = config.spider.walkInterval;
this.cpuLimit = config.spider.cpuLimit;
this.cpuInterval = config.spider.cpuInterval;
this.foundSpeed = 0; this.foundSpeed = 0;
this.foundCounter = 0; this.foundCounter = 0;
setInterval(() => { setInterval(() => {
this.foundSpeed = this.foundCounter; this.foundSpeed = this.foundCounter;
this.foundCounter = 0; this.foundCounter = 0;
console.log('found speed', this.foundSpeed)
}, 1000) }, 1000)
this.announceHashes = [] this.announceHashes = []
this.searchHashes = []
} }
send(message, address) { send(message, address) {
@ -124,9 +114,7 @@ class Spider extends Emiter {
if(!this.ignore) if(!this.ignore)
{ {
const node = this.table.shift() const node = this.table.shift()
//if (node) { if (node && (config.spider.nodesUsage === 0 || parseInt(Math.random() * this.table.nodes.length / config.spider.nodesUsage) === 0)) {
if (node && parseInt(Math.random() * this.table.nodes.length / 100) === 0) {
//console.log('walk', this.table.nodes.length)
this.findNode(Node.neighbor(node.id, this.table.id), {address: node.address, port: node.port}) this.findNode(Node.neighbor(node.id, this.table.id), {address: node.address, port: node.port})
} }
} }
@ -171,7 +159,7 @@ class Spider extends Emiter {
} }
onFindNodeRequest(message, address) { onFindNodeRequest(message, address) {
if(this.foundSpeed > 500) if(config.spider.packagesLimit !== 0 && this.foundSpeed > config.spider.packagesLimit)
{ {
return return
} }
@ -199,7 +187,7 @@ class Spider extends Emiter {
} }
onGetPeersRequest(message, address) { onGetPeersRequest(message, address) {
if(this.foundSpeed > 500) if(config.spider.packagesLimit !== 0 && this.foundSpeed > config.spider.packagesLimit)
{ {
return return
} }
@ -246,15 +234,12 @@ class Spider extends Emiter {
}; };
this.emit('ensureHash', infohash.toString('hex').toUpperCase(), addressPair) this.emit('ensureHash', infohash.toString('hex').toUpperCase(), addressPair)
if(this.client && !this.ignore) { if(this.client && !this.ignore) {
cpuDebug('cpu usage:' + cpuUsage()) this.client.add(addressPair, infohash);
if(this.cpuLimit <= 0 || cpuUsage() <= this.cpuLimit + this.cpuInterval) {
this.client.add(addressPair, infohash);
}
} }
} }
onPingRequest(message, address) { onPingRequest(message, address) {
if(this.foundSpeed > 500) if(config.spider.packagesLimit !== 0 && this.foundSpeed > config.spider.packagesLimit)
{ {
return return
} }

View File

@ -21,8 +21,8 @@ let config = {
spider: { spider: {
walkInterval: 5, walkInterval: 5,
cpuLimit: 0, nodesUsage: 100,
cpuInterval: 10, packagesLimit: 500
}, },
downloader: { downloader: {
@ -35,11 +35,6 @@ let config = {
spaceQuota: false, spaceQuota: false,
spaceDiskLimit: 7 * 1024 * 1024 * 1024, spaceDiskLimit: 7 * 1024 * 1024 * 1024,
trafficInterface: 'enp2s0',
trafficMax: 0,
trafficUpdateTime: 3, //secs
trafficIgnoreDHT: false,
dbPath: '', dbPath: '',
client: { client: {