1
0
mirror of https://github.com/square/okhttp.git synced 2026-01-14 07:22:20 +03:00

Use different thread names if OkHttp is shaded

Closes: https://github.com/square/okhttp/issues/5632
This commit is contained in:
Jesse Wilson
2020-01-26 18:29:18 -05:00
parent 1b4cc4bb33
commit bd2c06dd4c
9 changed files with 26 additions and 8 deletions

View File

@@ -24,6 +24,7 @@ import java.util.concurrent.ThreadPoolExecutor
import java.util.concurrent.TimeUnit
import okhttp3.RealCall.AsyncCall
import okhttp3.internal.assertThreadDoesntHoldLock
import okhttp3.internal.okHttpName
import okhttp3.internal.threadFactory
/**
@@ -91,7 +92,7 @@ class Dispatcher constructor() {
get() {
if (executorServiceOrNull == null) {
executorServiceOrNull = ThreadPoolExecutor(0, Int.MAX_VALUE, 60, TimeUnit.SECONDS,
SynchronousQueue(), threadFactory("OkHttp Dispatcher", false))
SynchronousQueue(), threadFactory("$okHttpName Dispatcher", false))
}
return executorServiceOrNull!!
}

View File

@@ -29,6 +29,7 @@ import okhttp3.internal.http.BridgeInterceptor
import okhttp3.internal.http.CallServerInterceptor
import okhttp3.internal.http.RealInterceptorChain
import okhttp3.internal.http.RetryAndFollowUpInterceptor
import okhttp3.internal.okHttpName
import okhttp3.internal.platform.Platform
import okhttp3.internal.platform.Platform.Companion.INFO
import okhttp3.internal.threadName
@@ -131,7 +132,7 @@ internal class RealCall private constructor(
}
override fun run() {
threadName("OkHttp ${redactedUrl()}") {
threadName("$okHttpName ${redactedUrl()}") {
var signalledCallback = false
transmitter.timeoutEnter()
try {

View File

@@ -532,6 +532,16 @@ internal fun <E> MutableList<E>.addIfAbsent(element: E) {
@JvmField
internal val assertionsEnabled = OkHttpClient::class.java.desiredAssertionStatus()
/**
* Returns the string "OkHttp" unless the library has been shaded for inclusion in another library,
* or obfuscated with tools like R8 or ProGuard. In such cases it'll return a longer string like
* "com.example.shaded.okhttp3.OkHttp". In large applications it's possible to have multiple OkHttp
* instances; this makes it clear which is which.
*/
@JvmField
internal val okHttpName =
OkHttpClient::class.java.name.removePrefix("okhttp3.").removeSuffix("Client")
@Suppress("NOTHING_TO_INLINE")
internal inline fun Any.assertThreadHoldsLock() {
if (assertionsEnabled && !Thread.holdsLock(this)) {

View File

@@ -30,6 +30,7 @@ import okhttp3.internal.closeQuietly
import okhttp3.internal.concurrent.Task
import okhttp3.internal.concurrent.TaskRunner
import okhttp3.internal.io.FileSystem
import okhttp3.internal.okHttpName
import okhttp3.internal.platform.Platform
import okhttp3.internal.platform.Platform.Companion.WARN
import okio.BufferedSink
@@ -169,7 +170,7 @@ class DiskLruCache internal constructor(
private var nextSequenceNumber: Long = 0
private val cleanupQueue = taskRunner.newQueue()
private val cleanupTask = object : Task("OkHttp Cache") {
private val cleanupTask = object : Task("$okHttpName Cache") {
override fun runOnce(): Long {
synchronized(this@DiskLruCache) {
if (!initialized || closed) {

View File

@@ -18,6 +18,7 @@ package okhttp3.internal.concurrent
import java.util.concurrent.CountDownLatch
import java.util.concurrent.RejectedExecutionException
import okhttp3.internal.assertThreadDoesntHoldLock
import okhttp3.internal.okHttpName
/**
* A set of tasks that are executed in sequential order.
@@ -129,7 +130,7 @@ class TaskQueue internal constructor(
}
}
private class AwaitIdleTask : Task("OkHttp awaitIdle", cancelable = false) {
private class AwaitIdleTask : Task("$okHttpName awaitIdle", cancelable = false) {
val latch = CountDownLatch(1)
override fun runOnce(): Long {

View File

@@ -25,6 +25,7 @@ import okhttp3.internal.assertThreadDoesntHoldLock
import okhttp3.internal.assertThreadHoldsLock
import okhttp3.internal.concurrent.TaskRunner.Companion.INSTANCE
import okhttp3.internal.notify
import okhttp3.internal.okHttpName
import okhttp3.internal.threadFactory
/**
@@ -305,7 +306,7 @@ class TaskRunner(
companion object {
@JvmField
val INSTANCE = TaskRunner(RealBackend(threadFactory("OkHttp TaskRunner", daemon = true)))
val INSTANCE = TaskRunner(RealBackend(threadFactory("$okHttpName TaskRunner", daemon = true)))
val logger: Logger = Logger.getLogger(TaskRunner::class.java.name)
}

View File

@@ -29,6 +29,7 @@ import okhttp3.internal.concurrent.Task
import okhttp3.internal.concurrent.TaskQueue
import okhttp3.internal.concurrent.TaskRunner
import okhttp3.internal.connection.Transmitter.TransmitterReference
import okhttp3.internal.okHttpName
import okhttp3.internal.platform.Platform
class RealConnectionPool(
@@ -41,7 +42,7 @@ class RealConnectionPool(
private val keepAliveDurationNs: Long = timeUnit.toNanos(keepAliveDuration)
private val cleanupQueue: TaskQueue = taskRunner.newQueue()
private val cleanupTask = object : Task("OkHttp ConnectionPool") {
private val cleanupTask = object : Task("$okHttpName ConnectionPool") {
override fun runOnce() = cleanup(System.nanoTime())
}

View File

@@ -29,6 +29,7 @@ import okhttp3.internal.http2.ErrorCode.REFUSED_STREAM
import okhttp3.internal.http2.Settings.Companion.DEFAULT_INITIAL_WINDOW_SIZE
import okhttp3.internal.ignoreIoExceptions
import okhttp3.internal.notifyAll
import okhttp3.internal.okHttpName
import okhttp3.internal.peerName
import okhttp3.internal.platform.Platform
import okhttp3.internal.platform.Platform.Companion.INFO
@@ -574,7 +575,7 @@ class Http2Connection internal constructor(builder: Builder) : Closeable {
) = apply {
this.socket = socket
this.connectionName = when {
client -> "OkHttp $peerName"
client -> "$okHttpName $peerName"
else -> "MockWebServer $peerName"
}
this.source = source

View File

@@ -38,6 +38,7 @@ import okhttp3.internal.closeQuietly
import okhttp3.internal.concurrent.Task
import okhttp3.internal.concurrent.TaskRunner
import okhttp3.internal.connection.Exchange
import okhttp3.internal.okHttpName
import okhttp3.internal.ws.WebSocketProtocol.CLOSE_CLIENT_GOING_AWAY
import okhttp3.internal.ws.WebSocketProtocol.CLOSE_MESSAGE_MAX
import okhttp3.internal.ws.WebSocketProtocol.OPCODE_BINARY
@@ -164,7 +165,7 @@ class RealWebSocket(
// Process all web socket messages.
try {
val name = "OkHttp WebSocket ${request.url.redact()}"
val name = "$okHttpName WebSocket ${request.url.redact()}"
initReaderAndWriter(name, streams)
listener.onOpen(this@RealWebSocket, response)
loopReader()