close

open override fun close()

Initiates the shutdown process for the HttpClient. This is a non-blocking call, which means it returns immediately and begins the client closure in the background.

Usage

val client = HttpClient()
client.close()
client.coroutineContext.job.join() // Waits for complete termination if necessary

Important Notes

  • Non-blocking: close() only starts the closing process and does not wait for it to complete.

  • Coroutine Context: To wait for all client resources to be freed, use client.coroutineContext.job.join() or client.coroutineContext.cancel() to terminate ongoing tasks.

  • Manual Engine Management: If a custom engine was manually created, it must be closed explicitly after calling client.close() to release all resources.

Example with custom engine management:

val engine = HttpClientEngine() // Custom engine instance
val client = HttpClient(engine)

client.close()
engine.close() // Ensure manually created engine is also closed