A Million-user Comet Application with Mochiweb, Part 1
在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沒有在此監(jiān)聽,所以連接出錯
{error,econnrefused}
解決方法兩種:
1. 將mochi-urls.txt中的地址都改成127.0.0.1
2.
在http:request(…)運行之前調用http:set_options([{ipv6,disabled}]),關閉測試程序的IPv6模式,使用IPv4模式
理論上還有一種方法:讓mochi服務器開啟IPv6模式監(jiān)聽,這個我不知道這么開,
參考
You should teach
Yaws to listen also on IPv6 - “localhost” resolves not
only to IPv4
127.0.0.1, but also to IPv6 ::1.
二. 進程能打開的文件描述符數(shù)量的限制
MacOSX下缺省能同時打開的文件描述符最大數(shù)是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
open files (-n) 256
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設置,但最大數(shù)量還是不能超過10240
查看內核每進程最大文件數(shù):
$
sysctl kern.maxfiles kern.maxfilesperproc
kern.maxfiles: 12288
kern.maxfilesperproc: 10240
增大每進程最大文件數(shù):
$ sudo sysctl -w
kern.maxfilesperproc=20480 kern.maxfiles=22528
然后設置
$ ulimit -n
20480
注意ulimit只在每個shell窗口生命周期內有效,當新開一個shell后,得再次設置
sysctl做的修改沒有這個問題
三、調節(jié)IP端口號范圍,不然最多只能有15K個連接
缺省動態(tài)端口號從49152開始,改成2000后最多可以有63K個連接
sysctl -w net.inet.ip.portrange.hifirst=2000
net.inet.ip.portrange.first=2000