Apache下使用Bandwidth Mod限制总带宽

 Apache下使用Bandwidth Mod限制总带宽

环境:

CentOS release 5.6 + Apache 2.2.22

mod_bw v0.92
http://ivn.cl/files/source/mod_bw-0.92.tgz

mod_bw v0.92 文档
http://legacy.ivn.cl/files/txt/mod_bw-0.92.txt


下载及安装:

#wget http://ivn.cl/files/source/mod_bw-0.92.tgz
#tar zxvf mod_bw-0.92.tgz
#/data/apps/httpd2.2.22/bin/apxs -c -i -a mod_bw.c


配置VirtualHost

<VirtualHost *:80>
DocumentRoot /data/webapps/www.domain.com
ServerName www.domain.com

<Location />
        SetEnvIfNoCase User-Agent "Googlebot" google
        Order allow,deny
        Allow from all
        deny from env=google
</Location>

BandWidthModule On
ForceBandWidthModule On
BandWidth     all 131072
</VirtualHost>


其中 <Location />***</Location> 段是为了拒绝Googlebot的抓取用的。
在没有暴露链接地址的情况下,Google老来抓取文件,原因不明。
当然这个可以robots.txt里面写上,但没这个见效快。

下面是两个概念(注意区分大小写):

bps ( bits per second ,小b) 位/秒 一般用作传输速率
Bps ( bytes per second ,大B) 字节/秒 一般用作显示速度
1Byte = 8*1bit (1字节=8字位)

而我们通常所讲的2M带宽、10M带宽,一般是指10M bit (小b)

官方文档上BandWidth的单位: BandWidth [From] [bytes/s]

所以如果要限制某个虚拟主机的总带宽为1M,则:

BandWidth     all 131072
###(1024*1024) bps/8 = 131072 Bps

 

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在C#中,你可以使用System.Net.NetworkInformation命名空间中的NetworkInterface类来获取网络接口信息,并使用System.Diagnostics命名空间中的Process类来获取当前正在运行的进程。 以下是一个简单的示例代码,它将优先本应用程序使用带宽,同时限制其它应用程序占用带宽: ```csharp using System; using System.Diagnostics; using System.Net.NetworkInformation; using System.Threading; public void LimitBandwidth(int bandwidth) { // 获取当前网络接口 NetworkInterface networkInterface = NetworkInterface.GetAllNetworkInterfaces()[0]; // 获取当前进程的 ID int processId = Process.GetCurrentProcess().Id; // 获取当前进程的优先级 ProcessPriorityClass processPriorityClass = Process.GetCurrentProcess().PriorityClass; // 设置当前进程的优先级为高 Process.GetCurrentProcess().PriorityClass = ProcessPriorityClass.High; // 获取当前进程的 CPU 时间 long currentCpuTime = Process.GetCurrentProcess().TotalProcessorTime.Ticks; // 获取当前进程的网络流量 long currentBytesSent = networkInterface.GetIPv4Statistics().BytesSent; long currentBytesReceived = networkInterface.GetIPv4Statistics().BytesReceived; // 计算每秒可以发送的字节数 int bytesPerSecond = bandwidth * 1024 / 8; while (true) { // 计算当前进程的 CPU 时间和网络流量 long newCpuTime = Process.GetCurrentProcess().TotalProcessorTime.Ticks; long newBytesSent = networkInterface.GetIPv4Statistics().BytesSent; long newBytesReceived = networkInterface.GetIPv4Statistics().BytesReceived; // 计算当前进程的 CPU 使用率和发送速度 double cpuUsage = (double)(newCpuTime - currentCpuTime) / Stopwatch.Frequency / Environment.ProcessorCount; int sendSpeed = (int)((newBytesSent - currentBytesSent) / (newCpuTime - currentCpuTime) * Stopwatch.Frequency); // 如果发送速度超过限制,则暂停当前线程 if (sendSpeed > bytesPerSecond) { int sleepTime = (int)((double)(newBytesSent - currentBytesSent) / bytesPerSecond * 1000 - (newCpuTime - currentCpuTime) / Stopwatch.Frequency * 1000); if (sleepTime > 0) Thread.Sleep(sleepTime); } // 更新 CPU 时间和网络流量 currentCpuTime = newCpuTime; currentBytesSent = newBytesSent; currentBytesReceived = newBytesReceived; // 如果当前进程的 CPU 使用率超过 70%,则降低其优先级 if (cpuUsage > 0.7 && Process.GetCurrentProcess().PriorityClass == ProcessPriorityClass.High) { Process.GetCurrentProcess().PriorityClass = ProcessPriorityClass.Normal; } // 如果当前进程的 CPU 使用率低于 50%,则提高其优先级 if (cpuUsage < 0.5 && Process.GetCurrentProcess().PriorityClass == ProcessPriorityClass.Normal) { Process.GetCurrentProcess().PriorityClass = ProcessPriorityClass.High; } } } ``` 这段代码将每秒发送的字节数限制为指定的带宽,如果发送速度超过限制,则暂停当前线程。同时,它还会在当前进程的 CPU 使用率超过 70% 时降低其优先级,在 CPU 使用率低于 50% 时提高其优先级,以确保本应用程序能够优先使用带宽

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值