Memcache 间歇性错误

Memcache intermittent errors

对于以下间歇性 Memcache 连接失败的可能原因的任何建议,我们将不胜感激:

Memcache::connect(): Server 127.0.0.1 (tcp 11211, udp 0) failed with: 
Only one usage of each socket address (protocol/network address/port) is normally permitted.

据我所知,PHP 脚本仅在构建控制器时尝试打开连接,即没有多次尝试打开连接。错误报告在唯一一个尝试连接的地方。

我也查看了 memcached 统计数据,看起来没有错:

array(36) {
  ["pid"]=> string(4) "5788"
  ["uptime"]=> string(6) "731274"
  ["time"]=> string(10) "1533137924"
  ["version"]=> string(16) "1.4.5_4_gaa7839e
  ["pointer_size"]=> string(2) "64"
  ["curr_connections"]=> string(1) "2"
  ["total_connections"]=> string(8) "31881420"
  ["connection_structures"]=> string(3) "163"
  ["cmd_get"]=> string(8) "26219501"
  ["cmd_set"]=> string(8) "17532714"
  ["cmd_flush"]=> string(4) "1110"
  ["get_hits"]=> string(8) "25834764"
  ["get_misses"]=> string(6) "384737"
  ["delete_misses"]=> string(1) "5"
  ["delete_hits"]=> string(7) "1252043"
  ["incr_misses"]=> string(1) "0"
  ["incr_hits"]=> string(1) "0"
  ["decr_misses"]=> string(1) "0"
  ["decr_hits"]=> string(1) "0"
  ["cas_misses"]=> string(1) "0"
  ["cas_hits"]=> string(1) "0"
  ["cas_badval"]=> string(1) "0"
  ["auth_cmds"]=> string(1) "0"
  ["auth_errors"]=> string(1) "0"
  ["bytes_read"]=> string(11) "12021422144"
  ["bytes_written"]=> string(12) "163830241155"
  ["limit_maxbytes"]=> string(10) "4294967296"
  ["accepting_conns"]=> string(1) "1"
  ["listen_disabled_num"]=> string(1) "0"
  ["threads"]=> string(1) "4"
  ["conn_yields"]=> string(1) "0"
  ["bytes"]=> string(8) "89537575"
  ["curr_items"]=> string(5) "15811"
  ["total_items"]=> string(7) "2871704"
  ["evictions"]=> string(1) "0"
  ["reclaimed"]=> string(6) "570282"
}

系统 运行 在 Windows 服务器上,如果这会影响事情

This article 似乎解决了问题。

It means that you are exhausting all the available network ports on the machine. By default the OS only has around 4000 ports available that are not reserved by the system. What happens is that when any network connection is closed it goes into a TIME_WAIT state for 240 seconds and cannot be reused until this wait state is over. So as an example, if there are 16 connections per second for 4 minutes (16*4*60=3840), you will exhaust all the ports shortly there after. Now if you have HAS and the MTA on the same machine, this will get exhausted a lot sooner because in addition to them communicating with each other, which uses 2 ports (one for the MTA and one for HAS), the MTA uses up a lot of ports sending the mail.

解决方法如下:

You can fix this by modifying the below values.

  1. One of the ways is to increase the dynamic port range. The max by default is 5000. You can set this up to 65534. HKLM\System\CurrentControlSet\Services\Tcpip\Parameters\MaxUserPort is the key to use.

  2. The second thing you can do is, once the connection does get into an TIME_WAIT state, you can reduce the time it is in that state. Default is 4 minutes, but you can set this to 30 seconds. HKLM\System\CurrentControlSet\Services\Tcpip\Parameters\TCPTimedWaitDelay is the key to use.

After these changes are made the system must be restarted.

如果失败,请尝试使用 resmon(鲜为人知的 Windows 资源监视器)检查您的端口使用情况是否存在冲突服务。您可以从 Cortana 搜索框或命令 shell...

访问它

(我想将此归功于此,但这是团队的努力!)

最后,当您使用 Memcache 连接时,使用 connect 方法中的 return 检查您的连接是否成功很有用。如果使用 Memcache::add_server,您可能必须采用不同的方法,因为此方法只会在第一次尝试访问内存缓存时发现连接失败。