Skip to content

Commit

Permalink
Make connectTimeout configurable
Browse files Browse the repository at this point in the history
  • Loading branch information
slonopotamus committed Sep 27, 2019
1 parent a766b37 commit 09bf573
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 6 deletions.
Expand Up @@ -60,9 +60,19 @@ constructor(objects: ObjectFactory, extension: NexusPublishExtension, repository
@get:Input
val repositoryName: Property<String> = objects.property()

/**
* @see okhttp3.OkHttpClient.Builder.readTimeout
* @see okhttp3.OkHttpClient.Builder.writeTimeout
*/
@get:Internal
val clientTimeout: Property<Duration> = objects.property()

/**
* @see okhttp3.OkHttpClient.Builder.connectTimeout
*/
@get:Internal
val connectTimeout: Property<Duration> = objects.property()

init {
serverUrl.set(repository.nexusUrl)
username.set(repository.username)
Expand All @@ -71,6 +81,7 @@ constructor(objects: ObjectFactory, extension: NexusPublishExtension, repository
stagingProfileId.set(repository.stagingProfileId)
repositoryName.set(repository.name)
clientTimeout.set(extension.clientTimeout)
connectTimeout.set(extension.connectTimeout)
this.onlyIf { extension.useStaging.getOrElse(false) }
}

Expand All @@ -82,7 +93,7 @@ constructor(objects: ObjectFactory, extension: NexusPublishExtension, repository

internal fun createStagingRepo(): URI {
return serverUrlToStagingRepoUrl.computeIfAbsent(serverUrl.get()) { serverUrl ->
val client = NexusClient(serverUrl, username.orNull, password.orNull, clientTimeout.getOrElse(Duration.ZERO))
val client = NexusClient(serverUrl, username.orNull, password.orNull, clientTimeout.orNull, connectTimeout.orNull)
val stagingProfileId = determineStagingProfileId(client)
logger.info("Creating staging repository for stagingProfileId '{}'", stagingProfileId)
val stagingRepositoryId = client.createStagingRepository(stagingProfileId)
Expand Down
Expand Up @@ -40,10 +40,21 @@ open class NexusPublishExtension(project: Project) {
set(project.provider { project.group.toString() })
}

/**
* @see okhttp3.OkHttpClient.Builder.readTimeout
* @see okhttp3.OkHttpClient.Builder.writeTimeout
*/
val clientTimeout: Property<Duration> = project.objects.property<Duration>().apply {
set(Duration.ofMinutes(1))
}

/**
* @see okhttp3.OkHttpClient.Builder.connectTimeout
*/
val connectTimeout: Property<Duration> = project.objects.property<Duration>().apply {
set(Duration.ofMinutes(1))
}

val repositories: NexusRepositoryContainer = DefaultNexusRepositoryContainer(project.container(NexusRepository::class) { name ->
project.objects.newInstance(NexusRepository::class, name, project)
})
Expand Down
Expand Up @@ -32,16 +32,20 @@ import java.io.IOException
import java.io.UncheckedIOException
import java.net.URI
import java.time.Duration
import java.util.concurrent.TimeUnit

class NexusClient(private val baseUrl: URI, username: String?, password: String?, timeout: Duration) {
class NexusClient(private val baseUrl: URI, username: String?, password: String?, timeout: Duration?, connectTimeout: Duration?) {
private val api: NexusApi

init {
val httpClientBuilder = OkHttpClient.Builder()
.connectTimeout(1, TimeUnit.MINUTES)
.readTimeout(timeout.toMillis(), TimeUnit.MILLISECONDS)
.writeTimeout(timeout.toMillis(), TimeUnit.MILLISECONDS)
if (timeout != null) {
httpClientBuilder
.readTimeout(timeout)
.writeTimeout(timeout)
}
if (connectTimeout != null) {
httpClientBuilder.connectTimeout(connectTimeout)
}
if (username != null || password != null) {
val credentials = Credentials.basic(username ?: "", password ?: "")
httpClientBuilder
Expand Down

0 comments on commit 09bf573

Please sign in to comment.