gpt4 book ai didi

node.js 按域计算带宽使用

转载 作者:搜寻专家 更新时间:2023-11-01 00:34:12 24 4
gpt4 key购买 nike

如何使用 node.js 作为 Web 服务器监控每个域的带宽使用情况?

有人知道我没有遇到过执行此操作的 API 调用吗?

或者其他人在按带宽收费的 Multi-Tenancy 环境中使用的模块或其他方法?

更新:

有谁知道可以放在任何 Web 服务器(node.js、apache 等)之前的轻量级代理/服务器,它可以通过检查域来记录这些带宽统计信息?

最佳答案

在不修改 node.js 核心的情况下,最好的选择似乎是使用 bytesRead 和 bytesWritten 变量在套接字级别跟踪它。

这实际上比某些其他带宽跟踪器计算传输数据时仅测量 http 请求/响应字符串的大小更准确。

您有 2 个选项:1) 记录每个请求的字节数或 2) 记录 TCP 连接关闭时的字节数。

登录请求级别将提供可能有用的实时数据。但是,根据您实现日志记录的方式,它可能会减慢速度。最好将其保存在内存中并经常将其转储到磁盘/数据库。

在请求级别记录:

var http = require('http');
var server = http.createServer(function (req, res) {
// setup a counter for bytes already logged
req.connection.bytesLogged = (req.connection.bytesLogged || 0);

// output the bytes read by the socket
console.log('Socket [' + req.connection.remoteAddress + '] [' + req.headers.host + '] - Bytes Read: ' + req.connection.bytesRead);

// calculate the bytes of the request (includes headers and other tcp overhead - more realistic)
req.bytes = req.connection.bytesRead - req.connection.bytesLogged;
// output the bytes size of the request (note this is calculated after the TCP packets have been collected from the network and parsed by the HTTP parser
console.log('Request [' + req.connection.remoteAddress + '] [' + req.headers.host + '] - Bytes: ' + req.bytes);

// log the bytes to a memory array, DB or disk (implementation not shown)
// [code here]

// add the request bytes to the counter
req.connection.bytesLogged = req.connection.bytesLogged + req.bytes;

// normal http server processing like return document or file
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end('ok');
}).listen(1337, '127.0.0.1');
console.log('Server running at http://127.0.1.1:1337/');

套接字级别的日志:

var http = require('http');
var server = http.createServer(function (req, res) {

// create some variables for data we want to log
//
// due to the way node.js works the remoteAddress and remotePort will not
// be available in the connection close event
// we also need to store the domain/host from the http header because the http
// request also won't exist when the connection close event runs
var remoteAddress = req.connection.remoteAddress;
var remotePort = req.connection.remotePort;
var host = req.headers.host;
var connection = req.connection;

// output bytes read by socket on each request
console.log('HTTP Request [' + remoteAddress + ':' + remotePort + '] [' + host + '] - connection Bytes Read: ' + connection.bytesRead);

// setup handle for connection close event
//
// to avoid the handle being added multiple times we add the handle onto
// the connection object which will persist between http requests
//
// we store the handler so we can check whether it has already been added
// to the connection listeners array - a less robust alternative would be to
// add a flag like connection.closeHandleAdded = true
connection.handle = connection.handle || {};
if (!connection.handle.onconnectionClose) {
connection.handle.onconnectionClose = function() {
onconnectionClose(remoteAddress, remotePort, host, connection.bytesRead);
}
}

// check whether the close handle has already been added to the connection
// if not add it
if(connection.listeners('close').indexOf(connection.handle.onconnectionClose) == -1)
{
// attach handler to connection close event
connection.on('close', connection.handle.onconnectionClose);
// set connection idle timeout to 5 secs for testing purposes (default is 2min)
connection._idleTimeout = 5000;
}

// process http request as required
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end('ok');

}).listen(1337, '127.0.0.1');
console.log('Server running at http://127.0.1.1:1337/');

function onconnectionClose (remoteAddress, remotePort, host, bytesRead) {
console.log('connection Closed [' + remoteAddress + ':' + remotePort + '] [' + host + '] - connection Bytes Read: ' + bytesRead);
}

关于node.js 按域计算带宽使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10100060/

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