mirror of
https://github.com/kalarina-bit/Sparkledns.git
synced 2026-11-22 15:46:09 +00:00
Derive all OkHttp clients from one shared base client
The DoH transport keeps its own connection pool and no longer creates the dispatcher's thread pool only to shut it down.
This commit is contained in:
@@ -379,8 +379,7 @@ private class BlocklistStore(context: Context) {
|
||||
|
||||
companion object {
|
||||
val client: OkHttpClient by lazy {
|
||||
OkHttpClient.Builder()
|
||||
.connectTimeout(15, TimeUnit.SECONDS)
|
||||
Http.client.newBuilder()
|
||||
.readTimeout(60, TimeUnit.SECONDS)
|
||||
.build()
|
||||
}
|
||||
|
||||
@@ -10,10 +10,10 @@ import android.net.http.UrlResponseInfo
|
||||
import android.os.Build
|
||||
import android.util.Log
|
||||
import androidx.annotation.RequiresApi
|
||||
import okhttp3.ConnectionPool
|
||||
import okhttp3.Dns
|
||||
import okhttp3.HttpUrl.Companion.toHttpUrl
|
||||
import okhttp3.MediaType.Companion.toMediaType
|
||||
import okhttp3.OkHttpClient
|
||||
import okhttp3.Request
|
||||
import okhttp3.RequestBody.Companion.toRequestBody
|
||||
import java.io.ByteArrayOutputStream
|
||||
@@ -91,7 +91,11 @@ private class OkHttpTransport(private val provider: DohProvider) : Transport {
|
||||
|
||||
private val url by lazy { provider.url.toHttpUrl() }
|
||||
|
||||
private val client = OkHttpClient.Builder()
|
||||
// Its own connection pool, so stopping a session drops only DoH connections
|
||||
private val pool = ConnectionPool()
|
||||
|
||||
private val client = Http.client.newBuilder()
|
||||
.connectionPool(pool)
|
||||
.dns(BootstrapDns(provider.bootstrap))
|
||||
.connectTimeout(5, TimeUnit.SECONDS)
|
||||
.readTimeout(5, TimeUnit.SECONDS)
|
||||
@@ -118,8 +122,7 @@ private class OkHttpTransport(private val provider: DohProvider) : Transport {
|
||||
}
|
||||
|
||||
override fun shutdown() {
|
||||
client.dispatcher.executorService.shutdown()
|
||||
client.connectionPool.evictAll()
|
||||
pool.evictAll()
|
||||
}
|
||||
|
||||
private companion object {
|
||||
|
||||
@@ -0,0 +1,14 @@
|
||||
package cc.skysparkle.sparkledns
|
||||
|
||||
import okhttp3.OkHttpClient
|
||||
import java.util.concurrent.TimeUnit
|
||||
|
||||
// Base for every OkHttp client in the app. Clients made with newBuilder() share its dispatcher
|
||||
// and thread pools, and its connection pool unless they bring their own.
|
||||
internal object Http {
|
||||
val client: OkHttpClient by lazy {
|
||||
OkHttpClient.Builder()
|
||||
.connectTimeout(15, TimeUnit.SECONDS)
|
||||
.build()
|
||||
}
|
||||
}
|
||||
@@ -4,7 +4,6 @@ import android.content.Context
|
||||
import kotlinx.coroutines.flow.MutableStateFlow
|
||||
import kotlinx.coroutines.flow.StateFlow
|
||||
import okhttp3.HttpUrl.Companion.toHttpUrlOrNull
|
||||
import okhttp3.OkHttpClient
|
||||
import okhttp3.Request
|
||||
import java.io.File
|
||||
import java.io.IOException
|
||||
@@ -74,8 +73,7 @@ object ServerList {
|
||||
@Volatile private var loaded = false
|
||||
|
||||
private val client by lazy {
|
||||
OkHttpClient.Builder()
|
||||
.connectTimeout(15, TimeUnit.SECONDS)
|
||||
Http.client.newBuilder()
|
||||
.readTimeout(30, TimeUnit.SECONDS)
|
||||
.build()
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user