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 {
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
private val UTF_8 = Charset.forName("UTF-8")
@ -398,12 +408,7 @@ object Cache {
} catch {
case NonFatal(e) =>
if (conn != null)
conn match {
case conn0: HttpURLConnection =>
conn0.getInputStream.close()
conn0.disconnect()
case _ =>
}
closeConn(conn)
throw e
}
}
@ -448,11 +453,7 @@ object Cache {
}
} finally {
if (conn != null)
conn match {
case conn0: HttpURLConnection =>
conn0.disconnect()
case _ =>
}
closeConn(conn)
}
}
@ -535,11 +536,7 @@ object Cache {
}
} finally {
if (conn != null)
conn match {
case conn0: HttpURLConnection =>
conn0.disconnect()
case _ =>
}
closeConn(conn)
}
}
}
@ -673,8 +670,7 @@ object Cache {
ackRange.startsWith(s"bytes $alreadyDownloaded-") || {
// unrecognized Content-Range header -> start a new connection with no resume
conn0.getInputStream.close()
conn0.disconnect()
closeConn(conn)
conn = urlConnection(url, artifact.authentication)
false
}
@ -719,11 +715,7 @@ object Cache {
}
} finally {
if (conn != null)
conn match {
case conn0: HttpURLConnection =>
conn0.disconnect()
case _ =>
}
closeConn(conn)
}
}

View File

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

View File

@ -44,8 +44,11 @@ object HttpUtil {
} finally {
if (is != null)
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()
}
}
}

View File

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