Skip to content

Commit

Permalink
KTOR-7606 Make Closable implement AutoCloseable (#4414)
Browse files Browse the repository at this point in the history
* Use stdlib `use` extension-function
* Update type checks
  • Loading branch information
osipxd committed Oct 31, 2024
1 parent 2925131 commit 90dc63e
Show file tree
Hide file tree
Showing 35 changed files with 70 additions and 112 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
* Copyright 2014-2021 JetBrains s.r.o and contributors. Use of this source code is governed by the Apache 2.0 license.
*/
* Copyright 2014-2024 JetBrains s.r.o and contributors. Use of this source code is governed by the Apache 2.0 license.
*/

package io.ktor.client.engine.cio

Expand All @@ -14,8 +14,6 @@ import io.ktor.http.content.*
import io.ktor.util.date.*
import io.ktor.utils.io.*
import io.ktor.utils.io.CancellationException
import io.ktor.utils.io.core.*
import io.ktor.utils.io.errors.*
import io.ktor.websocket.*
import kotlinx.coroutines.*
import kotlinx.io.IOException
Expand Down
7 changes: 4 additions & 3 deletions ktor-client/ktor-client-cio/jvmAndPosix/test/CIOEngineTest.kt
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
/*
* Copyright 2014-2022 JetBrains s.r.o and contributors. Use of this source code is governed by the Apache 2.0 license.
* Copyright 2014-2024 JetBrains s.r.o and contributors. Use of this source code is governed by the Apache 2.0 license.
*/

package io.ktor.client.engine.cio

import io.ktor.client.*
import io.ktor.client.engine.cio.*
import io.ktor.client.plugins.sse.*
import io.ktor.client.plugins.websocket.*
import io.ktor.client.request.*
Expand All @@ -11,7 +13,6 @@ import io.ktor.http.*
import io.ktor.network.selector.*
import io.ktor.network.sockets.*
import io.ktor.utils.io.*
import io.ktor.utils.io.core.*
import io.ktor.websocket.*
import kotlinx.coroutines.*
import kotlinx.coroutines.flow.*
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
/*
* Copyright 2014-2023 JetBrains s.r.o and contributors. Use of this source code is governed by the Apache 2.0 license.
* Copyright 2014-2024 JetBrains s.r.o and contributors. Use of this source code is governed by the Apache 2.0 license.
*/
import io.ktor.client.engine.cio.*

package io.ktor.client.engine.cio

import io.ktor.network.selector.*
import io.ktor.network.sockets.*
import io.ktor.utils.io.core.*
import kotlinx.coroutines.*
import kotlin.test.*

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
* Copyright 2014-2021 JetBrains s.r.o and contributors. Use of this source code is governed by the Apache 2.0 license.
*/
* Copyright 2014-2024 JetBrains s.r.o and contributors. Use of this source code is governed by the Apache 2.0 license.
*/

package io.ktor.client

Expand Down Expand Up @@ -1361,7 +1361,7 @@ public class HttpClient(
@Suppress("UNCHECKED_CAST")
val plugin = installedFeatures[key as AttributeKey<Any>]

if (plugin is Closeable) {
if (plugin is AutoCloseable) {
plugin.close()
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
/*
* Copyright 2014-2021 JetBrains s.r.o and contributors. Use of this source code is governed by the Apache 2.0 license.
*/
* Copyright 2014-2024 JetBrains s.r.o and contributors. Use of this source code is governed by the Apache 2.0 license.
*/

package io.ktor.client.engine

import io.ktor.util.*
import io.ktor.utils.io.core.*
import kotlinx.atomicfu.*
import kotlinx.coroutines.*
import kotlin.coroutines.*
Expand Down Expand Up @@ -39,19 +38,4 @@ public abstract class HttpClientEngineBase(private val engineName: String) : Htt
public class ClientEngineClosedException(override val cause: Throwable? = null) :
IllegalStateException("Client already closed")

/**
* Closes [CoroutineDispatcher] if it's [CloseableCoroutineDispatcher] or [Closeable].
*/
@OptIn(ExperimentalCoroutinesApi::class)
private fun CoroutineDispatcher.close() {
try {
when (this) {
is CloseableCoroutineDispatcher -> close()
is Closeable -> close()
}
} catch (ignore: Throwable) {
// Some closeable dispatchers like Dispatchers.IO can't be closed.
}
}

internal expect fun ioDispatcher(): CoroutineDispatcher
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2014-2022 JetBrains s.r.o and contributors. Use of this source code is governed by the Apache 2.0 license.
* Copyright 2014-2024 JetBrains s.r.o and contributors. Use of this source code is governed by the Apache 2.0 license.
*/

package io.ktor.client.plugins.cache.storage
Expand All @@ -10,7 +10,6 @@ import io.ktor.util.*
import io.ktor.util.collections.*
import io.ktor.util.date.*
import io.ktor.utils.io.*
import io.ktor.utils.io.core.*
import io.ktor.utils.io.jvm.javaio.*
import kotlinx.coroutines.*
import kotlinx.coroutines.sync.*
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
* Copyright 2014-2021 JetBrains s.r.o and contributors. Use of this source code is governed by the Apache 2.0 license.
*/
* Copyright 2014-2024 JetBrains s.r.o and contributors. Use of this source code is governed by the Apache 2.0 license.
*/

package io.ktor.client.engine.curl.test

Expand All @@ -9,9 +9,7 @@ import io.ktor.client.call.*
import io.ktor.client.engine.curl.*
import io.ktor.client.request.*
import io.ktor.client.statement.*
import io.ktor.utils.io.core.*
import kotlinx.coroutines.*
import kotlin.native.concurrent.*
import kotlin.test.*

class CurlNativeTests {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
* Copyright 2014-2021 JetBrains s.r.o and contributors. Use of this source code is governed by the Apache 2.0 license.
*/
* Copyright 2014-2024 JetBrains s.r.o and contributors. Use of this source code is governed by the Apache 2.0 license.
*/

package io.ktor.client.engine.curl.test

Expand All @@ -11,7 +11,6 @@ import io.ktor.client.engine.curl.*
import io.ktor.client.request.*
import io.ktor.client.statement.*
import io.ktor.http.*
import io.ktor.utils.io.core.*
import kotlinx.coroutines.*
import kotlin.test.*

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2014-2019 JetBrains s.r.o and contributors. Use of this source code is governed by the Apache 2.0 license.
* Copyright 2014-2024 JetBrains s.r.o and contributors. Use of this source code is governed by the Apache 2.0 license.
*/

@file:Suppress("NO_EXPLICIT_RETURN_TYPE_IN_API_MODE_WARNING", "KDocMissingDocumentation")
Expand All @@ -8,7 +8,6 @@ package io.ktor.client.tests.utils

import io.ktor.client.*
import io.ktor.client.engine.*
import io.ktor.utils.io.core.*
import kotlinx.coroutines.*
import kotlinx.coroutines.test.*
import kotlin.time.Duration.Companion.milliseconds
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
/*
* Copyright 2014-2022 JetBrains s.r.o and contributors. Use of this source code is governed by the Apache 2.0 license.
* Copyright 2014-2024 JetBrains s.r.o and contributors. Use of this source code is governed by the Apache 2.0 license.
*/

package io.ktor.client.engine.winhttp.internal

import io.ktor.client.plugins.sse.*
import io.ktor.client.request.*
import io.ktor.http.*
import io.ktor.http.cio.*
import io.ktor.util.date.*
import io.ktor.utils.io.*
import io.ktor.utils.io.core.*
import kotlin.coroutines.*

internal class WinHttpResponseData(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
/*
* Copyright 2014-2022 JetBrains s.r.o and contributors. Use of this source code is governed by the Apache 2.0 license.
*/
* Copyright 2014-2024 JetBrains s.r.o and contributors. Use of this source code is governed by the Apache 2.0 license.
*/

package io.ktor.client.engine.winhttp

import io.ktor.client.*
import io.ktor.client.call.*
import io.ktor.client.engine.*
import io.ktor.client.request.*
import io.ktor.utils.io.core.*
import kotlinx.coroutines.*
import kotlin.test.*

Expand Down
2 changes: 1 addition & 1 deletion ktor-io/api/ktor-io.klib.api
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ abstract interface <#A: kotlin/Any> io.ktor.utils.io.pool/ObjectPool : kotlin/Au
open fun close() // io.ktor.utils.io.pool/ObjectPool.close|close(){}[0]
}

abstract interface io.ktor.utils.io.core/Closeable { // io.ktor.utils.io.core/Closeable|null[0]
abstract interface io.ktor.utils.io.core/Closeable : kotlin/AutoCloseable { // io.ktor.utils.io.core/Closeable|null[0]
abstract fun close() // io.ktor.utils.io.core/Closeable.close|close(){}[0]
}

Expand Down
26 changes: 5 additions & 21 deletions ktor-io/common/src/io/ktor/utils/io/core/Closeable.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,9 @@

package io.ktor.utils.io.core

public expect interface Closeable {
public fun close()
}
import kotlin.use as stdlibUse

public inline fun <T : Closeable?, R> T.use(block: (T) -> R): R {
var closed = false
try {
return block(this)
} catch (cause: Throwable) {
closed = true
try {
this?.close()
} catch (closeException: Throwable) {
cause.addSuppressed(closeException)
}
throw cause
} finally {
if (!closed) {
this?.close()
}
}
}
public expect interface Closeable : AutoCloseable

@Deprecated("Use stdlib implementation instead. Remove import of this function", ReplaceWith("stdlibUse(block)"))
public inline fun <T : Closeable?, R> T.use(block: (T) -> R): R = stdlibUse(block)
4 changes: 2 additions & 2 deletions ktor-io/js/src/io/ktor/utils/io/core/Closeable.js.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@

package io.ktor.utils.io.core

public actual interface Closeable {
public actual fun close()
public actual interface Closeable : AutoCloseable {
override fun close()
}
4 changes: 2 additions & 2 deletions ktor-io/posix/src/io/ktor/utils/io/core/Closeable.posix.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@

package io.ktor.utils.io.core

public actual interface Closeable {
public actual fun close()
public actual interface Closeable : AutoCloseable {
override fun close()
}
4 changes: 2 additions & 2 deletions ktor-io/wasmJs/src/io/ktor/utils/io/core/Closeable.wasmJs.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@

package io.ktor.utils.io.core

public actual interface Closeable {
public actual fun close()
public actual interface Closeable : AutoCloseable {
override fun close()
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
/*
* Copyright 2014-2021 JetBrains s.r.o and contributors. Use of this source code is governed by the Apache 2.0 license.
* Copyright 2014-2024 JetBrains s.r.o and contributors. Use of this source code is governed by the Apache 2.0 license.
*/

package io.ktor.network.sockets.tests

import io.ktor.network.sockets.*
import io.ktor.utils.io.*
import io.ktor.utils.io.CancellationException
import io.ktor.utils.io.core.*
import kotlinx.coroutines.*
import kotlinx.io.*
import kotlin.test.*
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
/*
* Copyright 2014-2021 JetBrains s.r.o and contributors. Use of this source code is governed by the Apache 2.0 license.
* Copyright 2014-2024 JetBrains s.r.o and contributors. Use of this source code is governed by the Apache 2.0 license.
*/

package io.ktor.network.sockets.tests

import io.ktor.network.selector.*
import io.ktor.test.dispatcher.*
import io.ktor.util.*
import io.ktor.utils.io.core.*
import kotlinx.coroutines.*
import kotlin.time.*
import kotlin.time.Duration.Companion.seconds
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2014-2021 JetBrains s.r.o and contributors. Use of this source code is governed by the Apache 2.0 license.
* Copyright 2014-2024 JetBrains s.r.o and contributors. Use of this source code is governed by the Apache 2.0 license.
*/

package io.ktor.network.sockets.tests
Expand All @@ -11,6 +11,7 @@ import kotlinx.coroutines.*
import kotlinx.io.*
import kotlin.random.*
import kotlin.test.*
import kotlin.use

class UDPSocketTest {

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
* Copyright 2014-2021 JetBrains s.r.o and contributors. Use of this source code is governed by the Apache 2.0 license.
*/
* Copyright 2014-2024 JetBrains s.r.o and contributors. Use of this source code is governed by the Apache 2.0 license.
*/

package io.ktor.network.tls

Expand All @@ -21,6 +21,7 @@ import javax.crypto.*
import javax.crypto.spec.*
import javax.security.auth.x500.*
import kotlin.coroutines.*
import kotlin.use

internal class TLSClientHandshake(
rawInput: ByteReadChannel,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2014-2019 JetBrains s.r.o and contributors. Use of this source code is governed by the Apache 2.0 license.
* Copyright 2014-2024 JetBrains s.r.o and contributors. Use of this source code is governed by the Apache 2.0 license.
*/

package io.ktor.network.tls.certificates
Expand All @@ -23,6 +23,7 @@ import javax.security.auth.x500.*
import kotlin.time.*
import kotlin.time.Duration
import kotlin.time.Duration.Companion.days
import kotlin.use

internal val DEFAULT_PRINCIPAL = X500Principal("CN=localhost, OU=Kotlin, O=JetBrains, C=RU")
private val DEFAULT_CA_PRINCIPAL = X500Principal("CN=localhostCA, OU=Kotlin, O=JetBrains, C=RU")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
/*
* Copyright 2014-2019 JetBrains s.r.o and contributors. Use of this source code is governed by the Apache 2.0 license.
* Copyright 2014-2024 JetBrains s.r.o and contributors. Use of this source code is governed by the Apache 2.0 license.
*/

package io.ktor.network.tls.certificates

import io.ktor.network.tls.*
import io.ktor.network.tls.extensions.*
import io.ktor.util.*
import io.ktor.utils.io.core.*
import java.io.*
import java.net.*
import java.security.*
Expand Down
6 changes: 5 additions & 1 deletion ktor-server/ktor-server-cio/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
/*
* Copyright 2014-2024 JetBrains s.r.o and contributors. Use of this source code is governed by the Apache 2.0 license.
*/

description = ""

kotlin.sourceSets {
Expand All @@ -14,11 +18,11 @@ kotlin.sourceSets {
api(project(":ktor-server:ktor-server-core"))
api(project(":ktor-client:ktor-client-cio"))
api(project(":ktor-server:ktor-server-test-suites"))
api(project(":ktor-server:ktor-server-test-base"))
}
}
jvmTest {
dependencies {
api(project(":ktor-server:ktor-server-test-base"))
api(project(":ktor-server:ktor-server-core", configuration = "testOutput"))
api(libs.logback.classic)
}
Expand Down
Loading

0 comments on commit 90dc63e

Please sign in to comment.