Really close http connections

This commit is contained in:
Alexandre Archambault 2017-06-06 18:00:04 +02:00
parent 3ac230f0be
commit 4cdd95b37e
4 changed files with 23 additions and 30 deletions

View File

@ -31,6 +31,16 @@ trait AuthenticatedURLConnection extends URLConnection {
object Cache { object Cache {
private[coursier] def closeConn(conn: URLConnection): Unit = {
Try(conn.getInputStream).toOption.filter(_ != null).foreach(_.close())
conn match {
case conn0: HttpURLConnection =>
Try(conn0.getErrorStream).toOption.filter(_ != null).foreach(_.close())
conn0.disconnect()
case _ =>
}
}
// java.nio.charset.StandardCharsets.UTF_8 not available in Java 6 // java.nio.charset.StandardCharsets.UTF_8 not available in Java 6
private val UTF_8 = Charset.forName("UTF-8") private val UTF_8 = Charset.forName("UTF-8")
@ -398,12 +408,7 @@ object Cache {
} catch { } catch {
case NonFatal(e) => case NonFatal(e) =>
if (conn != null) if (conn != null)
conn match { closeConn(conn)
case conn0: HttpURLConnection =>
conn0.getInputStream.close()
conn0.disconnect()
case _ =>
}
throw e throw e
} }
} }
@ -448,11 +453,7 @@ object Cache {
} }
} finally { } finally {
if (conn != null) if (conn != null)
conn match { closeConn(conn)
case conn0: HttpURLConnection =>
conn0.disconnect()
case _ =>
}
} }
} }
@ -535,11 +536,7 @@ object Cache {
} }
} finally { } finally {
if (conn != null) if (conn != null)
conn match { closeConn(conn)
case conn0: HttpURLConnection =>
conn0.disconnect()
case _ =>
}
} }
} }
} }
@ -673,8 +670,7 @@ object Cache {
ackRange.startsWith(s"bytes $alreadyDownloaded-") || { ackRange.startsWith(s"bytes $alreadyDownloaded-") || {
// unrecognized Content-Range header -> start a new connection with no resume // unrecognized Content-Range header -> start a new connection with no resume
conn0.getInputStream.close() closeConn(conn)
conn0.disconnect()
conn = urlConnection(url, artifact.authentication) conn = urlConnection(url, artifact.authentication)
false false
} }
@ -719,11 +715,7 @@ object Cache {
} }
} finally { } finally {
if (conn != null) if (conn != null)
conn match { closeConn(conn)
case conn0: HttpURLConnection =>
conn0.disconnect()
case _ =>
}
} }
} }

View File

@ -45,7 +45,7 @@ object Scaladex {
coursier.Platform.readFullySync(conn.getInputStream) coursier.Platform.readFullySync(conn.getInputStream)
} finally { } finally {
if (conn != null) if (conn != null)
conn.disconnect() coursier.Cache.closeConn(conn)
} }
new String(b, StandardCharsets.UTF_8).right[String] new String(b, StandardCharsets.UTF_8).right[String]

View File

@ -44,8 +44,11 @@ object HttpUtil {
} finally { } finally {
if (is != null) if (is != null)
is.close() is.close()
if (httpConn != null) if (httpConn != null) {
scala.util.Try(httpConn.getInputStream).filter(_ != null).foreach(_.close())
scala.util.Try(httpConn.getErrorStream).filter(_ != null).foreach(_.close())
httpConn.disconnect() httpConn.disconnect()
}
} }
} }

View File

@ -38,7 +38,7 @@ object FallbackDependenciesRepository {
None None
} finally { } finally {
if (conn != null) if (conn != null)
conn.disconnect() coursier.Cache.closeConn(conn)
} }
case _ => case _ =>
None None
@ -55,10 +55,8 @@ object FallbackDependenciesRepository {
case _: IOException => case _: IOException =>
false false
} finally { } finally {
conn match { if (conn != null)
case conn0: HttpURLConnection => conn0.disconnect() coursier.Cache.closeConn(conn)
case _ =>
}
} }
} }
} }