Pregunta

Note: expressed in Scala. Using a BufferedReader to process a gzipped HTTP stream and iterating through each line to read the incoming data. Problem is that if there is ever a reset connection due to a network I/O issue (provider does weird things sometimes) then I can see the connection staying open for up to 15 seconds before it times out, something I'd like to get down to 1 second. For some reason our office provider resets connections every 11 hours.

Here's how I'm handling the connection:

val connection = getConnection(URL, USER, PASSWORD)
      val inputStream = connection.getInputStream()
      val reader = new BufferedReader(new InputStreamReader(new StreamingGZIPInputStream(inputStream), GNIP_CHARSET))
      var line = reader.readLine()
      while(line != null){
          parseSupervisor ! ParseThis(line)
          line = reader.readLine()
      }
      throw new ParseStreamCollapseException

and here is getConnection defined:

private def getConnection(urlString: String, user: String, password: String): HttpURLConnection = {
    val url = new URL(urlString)

    val connection = url.openConnection().asInstanceOf[HttpURLConnection]
    connection.setReadTimeout(1000 * KEEPALIVE_TIMEOUT)
    connection.setConnectTimeout(1000 * 1)

    connection.setRequestProperty("Authorization", createAuthHeader(user, password));
    connection.setRequestProperty("Accept-Encoding", "gzip")
    connection
  }

To summarize: reading HTTP stream line-by-line via java.io.BufferedReader. Keep-alive on stream is 16 seconds, but to prevent further data loss I'd like to narrow it down to hopefully 1-2 seconds (basically check if stream is currently blank or if is network I/O). Some device in the middle is terminating the connection every 11 hours, and it would be nice to have a meaningful workaround to minimize data loss. The HttpURLConnection does not receive a "termination signal" on the connection.

Thanks!

¿Fue útil?

Solución

Unfortunately, unless the network device that's killing the connection is closing it cleanly, you're not going to get any sort of notification that the connection is dead. The reason for this is that there is no way to tell the difference between a remote host that is just taking a long time to respond and a broken connection. Either way the socket is silent.

Again, assuming that the connection is just being severed, your only option to detect the broken connection more quickly is to decrease your timeout.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top