在MacOSX上测试Mochiweb遇到的一些问题

本文介绍了在MacOSX平台使用Mochiweb开发Comet应用时遇到的问题,包括IPv6导致的连接错误及解决方法、进程能打开的文件描述符数量限制的调整、以及调节IP端口号范围的方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

[url=http://www.metabrew.com/article/a-million-user-comet-application-with-mochiweb-part-1/]A Million-user Comet Application with Mochiweb, Part 1[/url]
在MacOSX平台上遇到的一些问题:

一. IPv6导致的问题
在MacOS下测试时报错econnrefused,mochi-urls.txt文件的url不能用localhost,得用127.0.0.1才行

在Erlang的邮件列表中找到了解释:
We have seen the same issue with CouchDB. What we found out
is that in our case localhost was not only resolving to 127.0.0.1 (IPv4)
but also ::1 (IPv6) and that http:request() would try to connect to
::1 where no service was listening.

Erlang的http模块缺省是打开IPv6模式的,当测试程序通过http模块连接localhost时,连接的地址是IPv6的::1地址,但是mochi没有在此监听,所以连接出错 {error,econnrefused}

解决方法两种:
1. 将mochi-urls.txt中的地址都改成127.0.0.1
2. 在http:request(…)运行之前调用http:set_options([{ipv6,disabled}]),关闭测试程序的IPv6模式,使用IPv4模式

理论上还有一种方法:让mochi服务器开启IPv6模式监听,这个我不知道这么开,
参考
You should teach Yaws to listen also on IPv6 - “localhost” resolves not
only to IPv4 127.0.0.1, but also to IPv6 ::1.


二. 进程能打开的文件描述符数量的限制
MacOSX下缺省能同时打开的文件描述符最大数是256个,使用 ulimit -a命令查看
$ ulimit -a
core file size (blocks, -c) 0
data seg size (kbytes, -d) 6144
file size (blocks, -f) unlimited
max locked memory (kbytes, -l) unlimited
max memory size (kbytes, -m) unlimited
[b]open files (-n) 256
[/b]pipe size (512 bytes, -p) 1
stack size (kbytes, -s) 8192
cpu time (seconds, -t) unlimited
max user processes (-u) 266
virtual memory (kbytes, -v) unlimited
使用ulimit -n XXXX设置,但最大数量还是不能超过10240

查看内核每进程最大文件数:
$ sysctl kern.maxfiles kern.maxfilesperproc
kern.maxfiles: 12288
kern.maxfilesperproc: 10240

增大每进程最大文件数:
$ sudo sysctl -w kern.maxfilesperproc=20480 kern.maxfiles=22528

然后设置
$ ulimit -n 20480
注意ulimit只在每个shell窗口生命周期内有效,当新开一个shell后,得再次设置
sysctl做的修改没有这个问题

三、调节IP端口号范围,不然最多只能有15K个连接
缺省动态端口号从49152开始,改成2000后最多可以有63K个连接
sysctl -w net.inet.ip.portrange.hifirst=2000 net.inet.ip.portrange.first=2000
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值