我有一个小型的Web应用程序,它打开一个TCP套接字连接,发出一个命令,读取响应,然后关闭每个请求到特定REST端点的连接。太多的TIME_WAIT连接,得到“无法分配请求的地址”
我已经开始负载测试使用Apache JMeter的端点和我注意到,运行一段时间后,我开始看到类似“无法分配请求的地址”错误,该代码打开此连接是:
def lookup(word: String): Option[String] = {
try {
val socket = new Socket(InetAddress.getByName("localhost"), 2222)
val out = new PrintStream(socket.getOutputStream)
val reader = new BufferedReader(new InputStreamReader(socket.getInputStream, "utf8"))
out.println("lookup " + word)
out.flush()
var curr = reader.readLine()
var response = ""
while (!curr.contains("SUCC") && !curr.contains("FAIL")) {
response += curr + "\n"
curr = reader.readLine()
}
socket.close()
curr match {
case code if code.contains(SUCCESS_CODE) => {
Some(response)
}
case _ => None
}
}
catch {
case e: Exception => println("Got an exception "+ e.getMessage); None
}
}
当我运行netstat时,我也看到很多下面的TIME_WAIT连接状态,这意味着我在短暂的空间里用完了端口。
tcp6 0 0 localhost:54646 localhost:2222 TIME_WAIT
tcp6 0 0 localhost:54638 localhost:2222 TIME_WAIT
tcp6 0 0 localhost:54790 localhost:2222 TIME_WAIT
tcp6 0 0 localhost:54882 localhost:2222 TIME_WAIT
我想知道什么是最好的解决方案是这个问题。我目前的想法是创建一个连接池,其中连接到端口2222
上的此服务的连接可以被不同的HTTP请求重用,而不是每次都创建新的请求。这是解决问题并使应用程序扩展更好的明智方式吗?看起来引入了很多开销,并且使得我的应用程序更加复杂。
是否有任何其他解决方案来帮助此应用程序扩展并克服此端口问题,但我没有看到?我的web应用程序在Ubuntu linux VM中运行。
你需要SO_REUSE_ADDR吗?在这里看到答案:http://stackoverflow.com/questions/14388706/socket-options-so-reuseaddr-and-so-reuseport-how-do-they-differ-do-they-mean-t – 2014-09-25 07:20:36
也http: //stackoverflow.com/questions/16014627/tcp-connection-cant-be-established-when-there-is-a-tcp-connection-with-state-t?rq = 1和几百万:)其他相关的问题 - 请参阅RHS列表 - > – 2014-09-25 07:22:11