有时易造成 Socket 连接打开过多
当用此 Web Server 接收 Hub Server 发送过来的各种请求时,遇到了一个大问题:
随着时间推移,处于 ESTABLISHED 状态的 Socket 连接越来越多,慢慢抵达500多个,
TCP X.X.X.X:8080 72.14.192.68:55693 ESTABLISHED
TCP X.X.X.X:8080 74.125.126.80:59064 ESTABLISHED最终导致服务爆出异常“too many file descriptors in select”,当此异常发生时,已无法挽救,只能重启服务
解决方法
from twisted.protocols.policies import TimeoutMixin
class TimeoutTester(protocol.Protocol, policies.TimeoutMixin):
conn_timeout = 3 data_timeout = 300 def connectionMade(self): self.setTimeout(self.conn_timeout) def dataReceived(self, data): self.setTimeout(self.data_timeout) def connectionLost(self, reason=None): self.setTimeout(None)