Too many TIME_WAIT connections, receiving "Unable to assign the requested address",

I have a small web application that opens a TCP socket connection, issues a command, reads the response, and then closes the connection for each request to a specific REST endpoint.

I started downloading endpoint testing using Apache JMeter and notice that after starting for some time I start to see errors like "Unable to assign the requested address", the opening code for this connection:

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
 }
}

When I run netstat, I also see many of the following TIME_WAIT connection statuses, which implies that I am running out of ports in ephemeral space.

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-, . ? , .

- , ? - Linux Ubuntu.

+4
2

, . , . , , TIME_WAIT, . TIME_WAIT , .

, , ( ), . , . , , , , , .

, root-, sysctl , :

  • net.ipv4.ip_local_port_range - . , .
  • net.ipv4.tcp_tw_recycle - TIME_WAIT.
  • net.ipv4.tcp_tw_reuse - TIME_WAIT. .

. man- ip(7) tcp(7) .

+6

.

0

All Articles