Skip to content

KTOR-8455: Introduces the ability to preserve context with HttpClient. #4819

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,10 @@ public interface HttpClientEngine : CoroutineScope, Closeable {
private suspend fun executeWithinCallContext(requestData: HttpRequestData): HttpResponseData {
val callContext = createCallContext(requestData.executionContext)

val context = callContext + KtorCallContextElement(callContext)
val context = config.contextProvider?.let { provider ->
callContext + KtorCallContextElement(callContext) + provider.invoke()
} ?: callContext + KtorCallContextElement(callContext)

return async(context) {
if (closed) {
throw ClientEngineClosedException()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package io.ktor.client.engine

import io.ktor.utils.io.*
import kotlinx.coroutines.*
import kotlin.coroutines.CoroutineContext

/**
* Base configuration for [HttpClientEngine].
Expand All @@ -32,6 +33,13 @@ public open class HttpClientEngineConfig {
*/
public var dispatcher: CoroutineDispatcher? = null

/**
* Allow preservation of additional context after processing operations.
*
* [Report a problem](https://ktor.io/feedback/?fqname=io.ktor.client.engine.HttpClientEngineConfig.context)
*/
public var contextProvider: (() -> CoroutineContext)? = null

/**
* Enables HTTP pipelining advice.
*
Expand Down