Skip to content

Commit

Permalink
Add timeout configuration (#21)
Browse files Browse the repository at this point in the history
* Add timeout configuration to HttpURLConnectionUpload

* NIT: ignore scala-cli directory
  • Loading branch information
MaciejG604 authored Oct 2, 2023
1 parent 0c9373d commit cde8475
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 3 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
out/
.bsp
.idea
.scala-build
29 changes: 26 additions & 3 deletions publish/src/coursier/publish/upload/HttpURLConnectionUpload.scala
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,11 @@ import scala.jdk.CollectionConverters._
import scala.util.Try
import scala.util.control.NonFatal

final case class HttpURLConnectionUpload(urlSuffix: String) extends Upload {
final case class HttpURLConnectionUpload(
urlSuffix: String,
readTimeoutMsOpt: Option[Int],
connectTimeoutMsOpt: Option[Int]
) extends Upload {

def upload(
url: String,
Expand All @@ -29,6 +33,10 @@ final case class HttpURLConnectionUpload(urlSuffix: String) extends Upload {
val conn = url0.openConnection().asInstanceOf[HttpURLConnection]

conn.setRequestMethod("PUT")
readTimeoutMsOpt
.foreach(conn.setReadTimeout)
connectTimeoutMsOpt
.foreach(conn.setConnectTimeout)

for (auth <- authentication; (k, v) <- auth.allHttpHeaders)
conn.setRequestProperty(k, v)
Expand Down Expand Up @@ -138,7 +146,22 @@ final case class HttpURLConnectionUpload(urlSuffix: String) extends Upload {

object HttpURLConnectionUpload {
def create(): Upload =
HttpURLConnectionUpload("")
HttpURLConnectionUpload("", None, None)
def create(urlSuffix: String): Upload =
HttpURLConnectionUpload(urlSuffix)
HttpURLConnectionUpload(urlSuffix, None, None)

/** Create a HttpURLConnectionUpload with a read timeout
* @param readTimeoutMs
* the response read timeout in miliseconds
* @param connetionTimeoutMs
* the connection timeout in miliseconds
* @param urlSuffix
* the suffix to append to the url
*/
def create(
readTimeoutMs: Option[Int],
connectionTimeoutMs: Option[Int],
urlSuffix: String = ""
): Upload =
HttpURLConnectionUpload(urlSuffix, readTimeoutMs, connectionTimeoutMs)
}

0 comments on commit cde8475

Please sign in to comment.