gpt4 book ai didi

node.js - Socket.io fs.readFileSync 同时用于多个连接

转载 作者:太空宇宙 更新时间:2023-11-04 01:42:20 26 4
gpt4 key购买 nike

这并不是一个真正的问题,但我想知道我所做的是否正确,因为它有效!

所以,让我们回答这个问题,我同时监视许多接口(interface)(PPPoE 客户端),以了解其从 Linux 读取统计数据的流量。

我正在使用 npm 包:express、socket.io 和 socket.io-stream。

客户:

var sessionsAccel = $('table.accel').DataTable([]);

sessionsAccel.on('preDraw', function() {
$('.interfaceAccel').each(function(i) {
var t = $(this).data();
sockets['socket' + t.id].disconnect();
delete speeds['tx_bytes' + t.id];
delete speeds['rx_bytes' + t.id];
});
})
.on('draw', function() {
$('.interfaceAccel').each(function(i) {
var t = $(this).data();
sockets['socket' + t.id] = io.connect('http://172.16.101.2:3000/status', {
query: 'interface=' + t.interface,
'forceNew': true
});
sockets['socket' + t.id].on("connect", function() {
ss(sockets['socket' + t.id]).on('sendStatus', function(stream, data) {
if (typeof speeds['tx_bytes' + t.id] != 'undefined') {
var speedtx = (data.tx_bytes - speeds['tx_bytes' + t.id]) * 8 / 1000;
var speedrx = (data.rx_bytes - speeds['rx_bytes' + t.id]) * 8 / 1000;
if (speedtx > 1000) {
speedtx = speedtx / 1000;
speedtx = speedtx.toFixed(2);
speedtx_info = speedtx + ' Mbps';
} else {
speedtx = speedtx.toFixed(2);
speedtx_info = speedtx + ' kbps';
}

if (speedrx > 1000) {
speedrx = speedrx / 1000;
speedrx = speedrx.toFixed(2);
speedrx_info = speedrx + ' Mbps';
} else {
speedrx = speedrx.toFixed(2);
speedrx_info = speedrx + ' kbps';
}
$('.tx_' + t.id).html(speedtx_info);
$('.rx_' + t.id).html(speedrx_info);
}
speeds['tx_bytes' + t.id] = data.tx_bytes;
speeds['rx_bytes' + t.id] = data.rx_bytes;
});
});
});
})

服务器:

const app = require('express')();
const http = require('http').createServer(app);

const io = require('socket.io')(http);
const ss = require('socket.io-stream');
const path = require('path');
const fs = require('fs');

function getIntInfo(interface) {
if(fs.existsSync('/sys/class/net/'+ interface +'/statistics/tx_bytes')) {
var tx_bytes = fs.readFileSync('/sys/class/net/'+ interface +'/statistics/tx_bytes').toString();
var rx_bytes = fs.readFileSync('/sys/class/net/'+ interface +'/statistics/rx_bytes').toString();
var tx_packets = fs.readFileSync('/sys/class/net/'+ interface +'/statistics/tx_packets').toString();
var rx_packets = fs.readFileSync('/sys/class/net/'+ interface +'/statistics/rx_packets').toString();
return {tx_bytes : tx_bytes, rx_bytes : rx_bytes, tx_packets: tx_packets, rx_packets: rx_packets};
}else
return false;
}

io.of('/status').on('connection', function(socket) {
var query = socket.handshake.query['interface'];
var timer = setInterval(function() {
var stream = ss.createStream();
var info = getIntInfo(query);
ss(socket).emit('sendStatus', stream, info);
}, 1000);

socket.on('disconnect', function(){
socket.disconnect(true);
//console.info('disconnected user (id=' + socket.id + ').');
});
})

http.listen(3000, function(){
console.log('listening on *:3000');
});

就是这样,Datatable(即接口(interface))中的每一行都打开一个套接字连接并检索统计信息。

我的问题是,这会导致我的服务器因许多 I/O 读取这些文件而变得困惑?

最佳答案

由于您每秒都会为每个连接的客户端执行此操作,因此似乎您应该缓存此数据,以便在数据未更改时不必从磁盘读取或通过线路发送,以节省服务器负载和带宽使用。但是,如何最好地做到这一点的细节取决于您尚未包含的特定应用程序的知识。

您至少可以像这样使用异步 I/O:

const util = require('util');
const fs = require('fs');
const readFile = util.promisify(fs.readFile);

function getIntInfo(interface) {
function readInfo(name) {
return readFile('/sys/class/net/'+ interface +'/statistics/' + name).then(data => data.toString());
}
return Promise.all(
readFile('tx_bytes'),
readFile('rx_bytes'),
readFile('tx_packets'),
readFile('rx_packets')
).then(([tx_bytes, rx_bytes, tx_packets, rx_packets]) => {
return {tx_bytes, rx_bytes, tx_packets, rx_packets};
}).catch(err => {
console.log(err);
return false;
});
}

并且,您必须在客户端断开连接时停止间隔并更改其调用 getIntInfo() 的方式:

io.of('/status').on('connection', function(socket) {
var query = socket.handshake.query['interface'];
var timer = setInterval(function() {
getIntInfo(query).then(info => {
var stream = ss.createStream();
ss(socket).emit('sendStatus', stream, info);
});
}, 1000);

socket.on('disconnect', function(){
// stop the timer for this connection
clearInterval(timer);
});
});

现在我想多了一点,您可以通过仅使用一个间隔计时器来读取数据,然后将一组数据发送到所有连接到 /status 命名空间的监听客户端,从而大大提高可扩展性。您可以将文件读取次数从每个客户端每秒读取一次减少到无论有多少客户端每秒读取一次。

关于node.js - Socket.io fs.readFileSync 同时用于多个连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52488700/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com