{badmatch,{error,emfile}}

错误日志信息:同时打开文件数超过了服务器的限制。

2017-08-23 15:31:14.447 [error] <0.6981.1>@simulator_enc:get_private_key:31 read private key file "src/keys/private_key.pem" error! Msg = {badmatch,{error,emfile}}

2017-08-23 15:31:14.447 [error] <0.7002.1>@simulator_enc:get_private_key:31 read private key file "src/keys/private_key.pem" error! Msg = {badmatch,{error,emfile}}

2017-08-23 15:31:14.447 [error] <0.7006.1>@simulator_enc:get_private_key:31 read private key file "src/keys/private_key.pem" error! Msg = {badmatch,{error,emfile}}

2017-08-23 15:31:14.447 [error] <0.7011.1>@simulator_enc:get_private_key:31 read private key file "src/keys/private_key.pem" error! Msg = {badmatch,{error,emfile}}

2017-08-23 15:31:14.447 [error] <0.7012.1>@simulator_enc:get_private_key:31 read private key file "src/keys/private_key.pem" error! Msg = {badmatch,{error,emfile}}

2017-08-23 15:31:14.447 [error] <0.7013.1>@simulator_enc:get_private_key:31 read private key file "src/keys/private_key.pem" error! Msg = {badmatch,{error,emfile}}

问题分析:上面报错的原因时由于,同时打开的文件数据,超过了服务器的限制

第一种解决方案:配置服务器,加大上限

Server configuration

This chapter describes how to configure your Linux server for running Zotonic.

File descriptors

By default, Linux limits the number of file descriptors (i.e. the maximum number of open files) at 1024. Running Zotonic at scale will require many more than that, particularly because Zotonic usesWebSocketsextensively; remember that every open port is an open file.

The limit applies on several levels:

the process, in our case the Erlang VM (BEAM) that runs Zotonic

the Zotonic user

system-wide.

To check the current process usage, find BEAM’s PID:

# pidof beam.smp10006(forinstance)

Then count its open files:

# lsof -a -p 10006 | wc -l

And compare it to the process limit:

# cat /proc/10006/limits | grep 'Max open files'

You can raise the process limit by addingulimit-nto your Zotonic init script:

ulimit-n50000

Or change the limit in your system definition. For instance, when using systemd:

[Service]LimitNOFILE=50000

Finally, make sure to checkyour system-wide limits, too.

Network connections

If you have stateful connection tracking enabled, high-traffic Zotonic sites mayoverflow your conntrack table.

Compare the current number of connections with the limit:

# sysctl net.netfilter.nf_conntrack_max# sysctl net.netfilter.nf_conntrack_count

When you increase the maximum number of connections in the connection tracking table it is important to increase the size of the table which stores the connections. The rule of the thumb for this is:nf_conntrack_buckets=nf_conntrack_max/8.

If you need to raise the limit, edit/etc/sysctl.conf:

/etc/sysctl.conf

net.netfilter.nf_conntrack_max = some_number

net.netfilter.nf_conntrack_buckets = some_other_number

Moreover, to reduce the number of open connections, you can decrease their time-out values so they get closed sooner. By default, inactive connections can stay in the conntrack table for 5(!) days. To change this:

/etc/sysctl.conf

net.netfilter.nf_conntrack_tcp_timeout_established = 600

If you have a proxy in front of Zotonic (e.g. HAProxy or Varnish), you need to change the limits on the proxy, too.

See also

SeeExometer metricson how to monitor your Zotonic system.

For general Erlang troubleshooting, Fred Hebert’s free ebookStuff goes bad: Erlang in angeris a very good resource.

第二种解决方案:减小读取文件的次数,具体解决方法:在项目启动时,将私钥文件加载到一个gen_server的State中,每次用的时候去stade中获取。

推荐阅读更多精彩内容