Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit ffdd097

Browse files
committedJan 22, 2025·
OkHttp options WIP
1 parent e4a6925 commit ffdd097

File tree

8 files changed

+263
-51
lines changed

8 files changed

+263
-51
lines changed
 

‎.github/workflows/run-tests.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ name: Tests
22

33
on:
44
push:
5+
branches-ignore:
6+
- master
57
workflow_dispatch:
68

79
jobs:

‎.github/workflows/run-validations.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ name: Validations
22

33
on:
44
push:
5+
branches-ignore:
6+
- master
57
workflow_dispatch:
68
concurrency:
79
group: ${{ github.workflow }}-${{ github.ref }}

‎gradle/libs.versions.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ ktlint = "12.1.0"
1212
dokka = "1.9.20"
1313
kotlinx_datetime = "0.6.1"
1414
kotlinx_coroutines = "1.9.0"
15-
pubnub_js = "8.4.1"
15+
pubnub_js = "8.6.0"
1616
pubnub_swift = "8.3.0"
1717

1818
[libraries]

‎kotlin-js-store/yarn.lock

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -659,10 +659,26 @@ proxy-from-env@^1.1.0:
659659
resolved "https://registry.yarnpkg.com/proxy-from-env/-/proxy-from-env-1.1.0.tgz#e102f16ca355424865755d2c9e8ea4f24d58c3e2"
660660
integrity sha512-D+zkORCbA9f1tdWRK0RaCR3GPv50cMxcrz4X8k5LTSUD1Dkw47mKJEZQNunItRTkWwgtaUSo1RVFRIG9ZXiFYg==
661661

662-
pubnub@8.4.0:
663-
version "8.4.0"
664-
resolved "https://registry.yarnpkg.com/pubnub/-/pubnub-8.4.0.tgz#8a5fdd3af9783abb1de44ea4145107e296c8394d"
665-
integrity sha512-HvkFhn4XcfR1wdJv4paX94I0TT4UBHW/vIuYnS+6XuoSx0AunJMCk5wbKnSesmdbzYUZa8Ag3H1mJQZKuyRy8Q==
662+
pubnub@8.4.1:
663+
version "8.4.1"
664+
resolved "https://registry.yarnpkg.com/pubnub/-/pubnub-8.4.1.tgz#5f6f19e84d3187dc8aee0a458bd6b05e90d43e6a"
665+
integrity sha512-mPlwVoHJDWPasZx52UfSMiPX5TATm5A+ficSogyqDqTQ4w5EQnwxH+PJdsWc0mPnlT051jM1vIISMeM0fQ30CQ==
666+
dependencies:
667+
agentkeepalive "^3.5.2"
668+
buffer "^6.0.3"
669+
cbor-js "^0.1.0"
670+
cbor-sync "^1.0.4"
671+
form-data "^4.0.0"
672+
lil-uuid "^0.1.1"
673+
node-fetch "^2.7.0"
674+
proxy-agent "^6.3.0"
675+
react-native-url-polyfill "^2.0.0"
676+
text-encoding "^0.7.0"
677+
678+
pubnub@8.6.0:
679+
version "8.6.0"
680+
resolved "https://registry.yarnpkg.com/pubnub/-/pubnub-8.6.0.tgz#75524e7ed3653090652d160ce83ac089362a0379"
681+
integrity sha512-LBCglooxiLkNT3ArUOvSJnLKK6/QdeshWY60IWlSQ+SkXlzEjt74wAnX5XriEXKsmza2yw9mFGG6+B5SlczRzA==
666682
dependencies:
667683
agentkeepalive "^3.5.2"
668684
buffer "^6.0.3"

‎pubnub-gson/pubnub-gson-impl/src/main/java/com/pubnub/internal/java/v2/PNConfigurationImpl.kt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import com.pubnub.api.retry.RetryableEndpointGroup
1111
import okhttp3.Authenticator
1212
import okhttp3.CertificatePinner
1313
import okhttp3.ConnectionSpec
14+
import okhttp3.OkHttpClient
1415
import okhttp3.logging.HttpLoggingInterceptor
1516
import org.slf4j.LoggerFactory
1617
import java.net.Proxy
@@ -71,6 +72,10 @@ class PNConfigurationImpl(
7172
)
7273
),
7374
override val managePresenceListManually: Boolean = false,
75+
override val baseOkHttpClient: OkHttpClient? = null,
76+
override val subscribeOkHttpConfigureAction: ((OkHttpClient.Builder) -> Unit)? = null,
77+
override val nonSubscribeOkHttpConfigureAction: ((OkHttpClient.Builder) -> Unit)? = null,
78+
override val filesOkHttpConfigureAction: ((OkHttpClient.Builder) -> Unit)? = null,
7479
) : PNConfiguration {
7580
companion object {
7681
const val DEFAULT_DEDUPE_SIZE = 100

‎pubnub-kotlin/pubnub-kotlin-core-api/src/jvmMain/kotlin/com/pubnub/api/v2/PNConfiguration.kt

Lines changed: 144 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import com.pubnub.api.retry.RetryConfiguration
88
import okhttp3.Authenticator
99
import okhttp3.CertificatePinner
1010
import okhttp3.ConnectionSpec
11+
import okhttp3.OkHttpClient
1112
import okhttp3.logging.HttpLoggingInterceptor
1213
import java.net.Proxy
1314
import java.net.ProxySelector
@@ -16,6 +17,47 @@ import javax.net.ssl.SSLSocketFactory
1617
import javax.net.ssl.X509ExtendedTrustManager
1718

1819
actual interface PNConfiguration {
20+
/**
21+
* The base [OkHttpClient] instance that will be used to preconfigure PubNub SDK's internal clients.
22+
*
23+
* Please note that the internal clients will share the dispatcher and connection pool with the provided client.
24+
*
25+
* @see [subscribeOkHttpConfigureAction]
26+
* @see [nonSubscribeOkHttpConfigureAction]
27+
* @see [filesOkHttpConfigureAction]
28+
*/
29+
val baseOkHttpClient: OkHttpClient?
30+
31+
/**
32+
* Use this callback to configure any options on the [OkHttpClient] instance that will be used for Subscribe requests.
33+
*
34+
* Setting this option disables reading values from [logVerbosity], [httpLoggingInterceptor], [sslSocketFactory],
35+
* [connectionSpec], [hostnameVerifier], [proxy], [proxySelector], [proxyAuthenticator] and [certificatePinner] for
36+
* the instance of `OkHttpClient` servicing Subscribe requests. Those options should be set using the
37+
* [subscribeOkHttpConfigureAction] instead.
38+
*/
39+
val subscribeOkHttpConfigureAction: ((OkHttpClient.Builder) -> Unit)?
40+
41+
/**
42+
* Use this callback to configure any options on the [OkHttpClient] instance that will be used for requests other than Subscribe or File upload/download.
43+
*
44+
* Setting this option disables reading values from [logVerbosity], [httpLoggingInterceptor], [sslSocketFactory],
45+
* [connectionSpec], [hostnameVerifier], [proxy], [proxySelector], [proxyAuthenticator] and [certificatePinner] for
46+
* the instance of `OkHttpClient` servicing requests other than Subscribe and File upload/download. Those options
47+
* should be set using the [nonSubscribeOkHttpConfigureAction] instead.
48+
*/
49+
val nonSubscribeOkHttpConfigureAction: ((OkHttpClient.Builder) -> Unit)?
50+
51+
/**
52+
* Use this callback to configure any options on the [OkHttpClient] instance that will be used for File upload/download requests.
53+
*
54+
* Setting this option disables reading values from [logVerbosity], [httpLoggingInterceptor], [sslSocketFactory],
55+
* [connectionSpec], [hostnameVerifier], [proxy], [proxySelector], [proxyAuthenticator] and [certificatePinner] for
56+
* the instance of `OkHttpClient` servicing File upload/download requests. Those options
57+
* should be set using the [filesOkHttpConfigureAction] instead.
58+
*/
59+
val filesOkHttpConfigureAction: ((OkHttpClient.Builder) -> Unit)?
60+
1961
/**
2062
* The user ID that the PubNub client will use.
2163
*/
@@ -77,6 +119,9 @@ actual interface PNConfiguration {
77119
/**
78120
* Set to [PNLogVerbosity.BODY] to enable logging of network traffic, otherwise se to [PNLogVerbosity.NONE].
79121
*/
122+
@Deprecated(
123+
message = "Use `subscribeOkHttpConfigureAction`, `nonSubscribeOkHttpConfigureAction`, or `filesOkHttpConfigureAction` to configure the OkHttp client."
124+
)
80125
actual val logVerbosity: PNLogVerbosity
81126

82127
/**
@@ -103,7 +148,7 @@ actual interface PNConfiguration {
103148
val heartbeatInterval: Int
104149

105150
/**
106-
* The subscribe request timeout.
151+
* The subscribe read timeout.
107152
*
108153
* The value is in seconds.
109154
*
@@ -147,6 +192,9 @@ actual interface PNConfiguration {
147192
*
148193
* Defaults to 10.
149194
*/
195+
@Deprecated(
196+
message = "Use `subscribeOkHttpConfigureAction`, `nonSubscribeOkHttpConfigureAction`, or `filesOkHttpConfigureAction` to configure the OkHttp client."
197+
)
150198
val nonSubscribeReadTimeout: Int
151199

152200
/**
@@ -212,48 +260,75 @@ actual interface PNConfiguration {
212260
*
213261
* @see [Proxy]
214262
*/
263+
@Deprecated(
264+
message = "Use `subscribeOkHttpConfigureAction`, `nonSubscribeOkHttpConfigureAction`, or `filesOkHttpConfigureAction` to configure the OkHttp client."
265+
)
215266
val proxy: Proxy?
216267

217268
/**
218269
* @see [ProxySelector]
219270
*/
271+
@Deprecated(
272+
message = "Use `subscribeOkHttpConfigureAction`, `nonSubscribeOkHttpConfigureAction`, or `filesOkHttpConfigureAction` to configure the OkHttp client."
273+
)
220274
val proxySelector: ProxySelector?
221275

222276
/**
223277
* @see [Authenticator]
224278
*/
279+
@Deprecated(
280+
message = "Use `subscribeOkHttpConfigureAction`, `nonSubscribeOkHttpConfigureAction`, or `filesOkHttpConfigureAction` to configure the OkHttp client."
281+
)
225282
val proxyAuthenticator: Authenticator?
226283

227284
/**
228285
* @see [CertificatePinner]
229286
*/
287+
@Deprecated(
288+
message = "Use `subscribeOkHttpConfigureAction`, `nonSubscribeOkHttpConfigureAction`, or `filesOkHttpConfigureAction` to configure the OkHttp client."
289+
)
230290
val certificatePinner: CertificatePinner?
231291

232292
/**
233293
* Sets a custom [HttpLoggingInterceptor] for logging network traffic.
234294
*
235295
* @see [HttpLoggingInterceptor]
236296
*/
297+
@Deprecated(
298+
message = "Use `subscribeOkHttpConfigureAction`, `nonSubscribeOkHttpConfigureAction`, or `filesOkHttpConfigureAction` to configure the OkHttp client."
299+
)
237300
val httpLoggingInterceptor: HttpLoggingInterceptor?
238301

239302
/**
240303
* @see [SSLSocketFactory]
241304
*/
305+
@Deprecated(
306+
message = "Use `subscribeOkHttpConfigureAction`, `nonSubscribeOkHttpConfigureAction`, or `filesOkHttpConfigureAction` to configure the OkHttp client."
307+
)
242308
val sslSocketFactory: SSLSocketFactory?
243309

244310
/**
245311
* @see [X509ExtendedTrustManager]
246312
*/
313+
@Deprecated(
314+
message = "Use `subscribeOkHttpConfigureAction`, `nonSubscribeOkHttpConfigureAction`, or `filesOkHttpConfigureAction` to configure the OkHttp client."
315+
)
247316
val x509ExtendedTrustManager: X509ExtendedTrustManager?
248317

249318
/**
250319
* @see [okhttp3.ConnectionSpec]
251320
*/
321+
@Deprecated(
322+
message = "Use `subscribeOkHttpConfigureAction`, `nonSubscribeOkHttpConfigureAction`, or `filesOkHttpConfigureAction` to configure the OkHttp client."
323+
)
252324
val connectionSpec: ConnectionSpec?
253325

254326
/**
255327
* @see [javax.net.ssl.HostnameVerifier]
256328
*/
329+
@Deprecated(
330+
message = "Use `subscribeOkHttpConfigureAction`, `nonSubscribeOkHttpConfigureAction`, or `filesOkHttpConfigureAction` to configure the OkHttp client."
331+
)
257332
val hostnameVerifier: HostnameVerifier?
258333

259334
/**
@@ -505,6 +580,9 @@ actual interface PNConfiguration {
505580
/**
506581
* @see [okhttp3.Dispatcher.setMaxRequestsPerHost]
507582
*/
583+
@Deprecated(
584+
message = "Use `subscribeOkHttpConfigureAction`, `nonSubscribeOkHttpConfigureAction`, or `filesOkHttpConfigureAction` to configure the OkHttp client."
585+
)
508586
var maximumConnections: Int?
509587

510588
/**
@@ -594,6 +672,41 @@ actual interface PNConfiguration {
594672
*/
595673
var managePresenceListManually: Boolean
596674

675+
/**
676+
* The base [OkHttpClient] instance that will be used to preconfigure PubNub SDK's internal `OKHttpClients`.
677+
*/
678+
var baseOkHttpClient: OkHttpClient?
679+
680+
/**
681+
* Use this callback to configure any options on the [OkHttpClient] instance that will be used for Subscribe requests.
682+
*
683+
* Setting this option disables reading values from [logVerbosity], [httpLoggingInterceptor], [sslSocketFactory],
684+
* [connectionSpec], [hostnameVerifier], [proxy], [proxySelector], [proxyAuthenticator] and [certificatePinner] for
685+
* the instance of `OkHttpClient` servicing Subscribe requests. Those options should be set using the
686+
* [subscribeOkHttpConfigureAction] instead.
687+
*/
688+
var subscribeOkHttpConfigureAction: ((OkHttpClient.Builder) -> Unit)?
689+
690+
/**
691+
* Use this callback to configure any options on the [OkHttpClient] instance that will be used for requests other than Subscribe or File upload/download.
692+
*
693+
* Setting this option disables reading values from [logVerbosity], [httpLoggingInterceptor], [sslSocketFactory],
694+
* [connectionSpec], [hostnameVerifier], [proxy], [proxySelector], [proxyAuthenticator] and [certificatePinner] for
695+
* the instance of `OkHttpClient` servicing requests other than Subscribe and File upload/download. Those options
696+
* should be set using the [nonSubscribeOkHttpConfigureAction] instead.
697+
*/
698+
var nonSubscribeOkHttpConfigureAction: ((OkHttpClient.Builder) -> Unit)?
699+
700+
/**
701+
* Use this callback to configure any options on the [OkHttpClient] instance that will be used for File upload/download requests.
702+
*
703+
* Setting this option disables reading values from [logVerbosity], [httpLoggingInterceptor], [sslSocketFactory],
704+
* [connectionSpec], [hostnameVerifier], [proxy], [proxySelector], [proxyAuthenticator] and [certificatePinner] for
705+
* the instance of `OkHttpClient` servicing File upload/download requests. Those options
706+
* should be set using the [filesOkHttpConfigureAction] instead.
707+
*/
708+
var filesOkHttpConfigureAction: ((OkHttpClient.Builder) -> Unit)?
709+
597710
/**
598711
* Create a [PNConfiguration] object with values from this builder.
599712
*/
@@ -701,6 +814,36 @@ interface PNConfigurationOverride {
701814
*/
702815
var nonSubscribeReadTimeout: Int
703816

817+
/**
818+
* Use this callback to configure any options on the [OkHttpClient] instance that will be used for Subscribe requests.
819+
*
820+
* Setting this option disables reading values from [logVerbosity], [httpLoggingInterceptor], [sslSocketFactory],
821+
* [connectionSpec], [hostnameVerifier], [proxy], [proxySelector], [proxyAuthenticator] and [certificatePinner] for
822+
* the instance of `OkHttpClient` servicing Subscribe requests. Those options should be set using the
823+
* [subscribeOkHttpConfigureAction] instead.
824+
*/
825+
var subscribeOkHttpConfigureAction: ((OkHttpClient.Builder) -> Unit)?
826+
827+
/**
828+
* Use this callback to configure any options on the [OkHttpClient] instance that will be used for requests other than Subscribe or File upload/download.
829+
*
830+
* Setting this option disables reading values from [logVerbosity], [httpLoggingInterceptor], [sslSocketFactory],
831+
* [connectionSpec], [hostnameVerifier], [proxy], [proxySelector], [proxyAuthenticator] and [certificatePinner] for
832+
* the instance of `OkHttpClient` servicing requests other than Subscribe and File upload/download. Those options
833+
* should be set using the [nonSubscribeOkHttpConfigureAction] instead.
834+
*/
835+
var nonSubscribeOkHttpConfigureAction: ((OkHttpClient.Builder) -> Unit)?
836+
837+
/**
838+
* Use this callback to configure any options on the [OkHttpClient] instance that will be used for File upload/download requests.
839+
*
840+
* Setting this option disables reading values from [logVerbosity], [httpLoggingInterceptor], [sslSocketFactory],
841+
* [connectionSpec], [hostnameVerifier], [proxy], [proxySelector], [proxyAuthenticator] and [certificatePinner] for
842+
* the instance of `OkHttpClient` servicing File upload/download requests. Those options
843+
* should be set using the [filesOkHttpConfigureAction] instead.
844+
*/
845+
var filesOkHttpConfigureAction: ((OkHttpClient.Builder) -> Unit)?
846+
704847
/**
705848
* Create a [PNConfiguration] object with values from this builder.
706849
*/

‎pubnub-kotlin/pubnub-kotlin-impl/src/main/kotlin/com/pubnub/internal/managers/RetrofitManager.kt

Lines changed: 71 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import com.pubnub.internal.services.TimeService
2020
import com.pubnub.internal.vendor.AppEngineFactory.Factory
2121
import okhttp3.Call
2222
import okhttp3.OkHttpClient
23+
import okhttp3.Request
2324
import okhttp3.logging.HttpLoggingInterceptor
2425
import org.jetbrains.annotations.TestOnly
2526
import retrofit2.Retrofit
@@ -63,10 +64,22 @@ class RetrofitManager(
6364

6465
init {
6566
if (!configuration.googleAppEngineNetworking) {
66-
transactionClientInstance = createOkHttpClient(configuration.nonSubscribeReadTimeout, parentOkHttpClient = transactionClientInstance)
67-
subscriptionClientInstance = createOkHttpClient(configuration.subscribeTimeout, parentOkHttpClient = subscriptionClientInstance)
68-
noSignatureClientInstance =
69-
createOkHttpClient(configuration.nonSubscribeReadTimeout, withSignature = false, parentOkHttpClient = noSignatureClientInstance)
67+
transactionClientInstance = createOkHttpClient(
68+
configuration.nonSubscribeReadTimeout,
69+
parentOkHttpClient = transactionClientInstance ?: configuration.baseOkHttpClient,
70+
configureOkHttpClient = configuration.nonSubscribeOkHttpConfigureAction
71+
)
72+
subscriptionClientInstance = createOkHttpClient(
73+
configuration.subscribeTimeout,
74+
parentOkHttpClient = subscriptionClientInstance ?: configuration.baseOkHttpClient,
75+
configureOkHttpClient = configuration.subscribeOkHttpConfigureAction
76+
)
77+
noSignatureClientInstance = createOkHttpClient(
78+
configuration.nonSubscribeReadTimeout,
79+
withSignature = false,
80+
parentOkHttpClient = noSignatureClientInstance ?: configuration.baseOkHttpClient,
81+
configureOkHttpClient = configuration.filesOkHttpConfigureAction
82+
)
7083
}
7184

7285
val transactionInstance = createRetrofit(transactionClientInstance)
@@ -97,6 +110,7 @@ class RetrofitManager(
97110
readTimeout: Int,
98111
withSignature: Boolean = true,
99112
parentOkHttpClient: OkHttpClient? = null,
113+
configureOkHttpClient: ((OkHttpClient.Builder) -> Unit)? = null,
100114
): OkHttpClient {
101115
val okHttpBuilder = parentOkHttpClient?.newBuilder() ?: OkHttpClient.Builder()
102116

@@ -105,42 +119,43 @@ class RetrofitManager(
105119
.readTimeout(readTimeout.toLong(), TimeUnit.SECONDS)
106120
.connectTimeout(configuration.connectTimeout.toLong(), TimeUnit.SECONDS)
107121

108-
with(configuration) {
109-
if (logVerbosity == PNLogVerbosity.BODY) {
110-
okHttpBuilder.addInterceptor(
111-
HttpLoggingInterceptor().apply {
112-
level = HttpLoggingInterceptor.Level.BODY
113-
},
114-
)
122+
configureOkHttpClient?.let { configureAction -> configureAction(okHttpBuilder) }
123+
?: with(configuration) {
124+
if (logVerbosity == PNLogVerbosity.BODY) {
125+
okHttpBuilder.addInterceptor(
126+
HttpLoggingInterceptor().apply {
127+
level = HttpLoggingInterceptor.Level.BODY
128+
},
129+
)
130+
}
131+
132+
if (httpLoggingInterceptor != null) {
133+
okHttpBuilder.addInterceptor(httpLoggingInterceptor!!)
134+
}
135+
136+
if (sslSocketFactory != null && x509ExtendedTrustManager != null) {
137+
okHttpBuilder.sslSocketFactory(
138+
configuration.sslSocketFactory!!,
139+
configuration.x509ExtendedTrustManager!!,
140+
)
141+
}
142+
connectionSpec?.let { okHttpBuilder.connectionSpecs(listOf(it)) }
143+
hostnameVerifier?.let { okHttpBuilder.hostnameVerifier(it) }
144+
proxy?.let { okHttpBuilder.proxy(it) }
145+
proxySelector?.let { okHttpBuilder.proxySelector(it) }
146+
proxyAuthenticator?.let { okHttpBuilder.proxyAuthenticator(it) }
147+
certificatePinner?.let { okHttpBuilder.certificatePinner(it) }
115148
}
116149

117-
if (httpLoggingInterceptor != null) {
118-
okHttpBuilder.addInterceptor(httpLoggingInterceptor!!)
119-
}
120-
121-
if (sslSocketFactory != null && x509ExtendedTrustManager != null) {
122-
okHttpBuilder.sslSocketFactory(
123-
configuration.sslSocketFactory!!,
124-
configuration.x509ExtendedTrustManager!!,
125-
)
126-
}
127-
connectionSpec?.let { okHttpBuilder.connectionSpecs(listOf(it)) }
128-
hostnameVerifier?.let { okHttpBuilder.hostnameVerifier(it) }
129-
proxy?.let { okHttpBuilder.proxy(it) }
130-
proxySelector?.let { okHttpBuilder.proxySelector(it) }
131-
proxyAuthenticator?.let { okHttpBuilder.proxyAuthenticator(it) }
132-
certificatePinner?.let { okHttpBuilder.certificatePinner(it) }
133-
}
134-
135150
if (withSignature) {
136151
okHttpBuilder.interceptors().removeAll { it is SignatureInterceptor }
137152
okHttpBuilder.addInterceptor(signatureInterceptor)
138153
}
139154

140155
val okHttpClient = okHttpBuilder.build()
141-
142-
configuration.maximumConnections?.let { okHttpClient.dispatcher.maxRequestsPerHost = it }
143-
156+
if (configureOkHttpClient == null) {
157+
configuration.maximumConnections?.let { okHttpClient.dispatcher.maxRequestsPerHost = it }
158+
}
144159
return okHttpClient
145160
}
146161

@@ -150,33 +165,44 @@ class RetrofitManager(
150165
.baseUrl(pubnub.baseUrl)
151166
.addConverterFactory(pubnub.mapper.converterFactory)
152167

153-
if (configuration.googleAppEngineNetworking) {
154-
retrofitBuilder.callFactory(Factory(configuration))
168+
val taggingCallFactory = if (configuration.googleAppEngineNetworking) {
169+
TaggingCallFactory(Factory(configuration), this)
155170
} else if (callFactory != null) {
156-
retrofitBuilder.callFactory(callFactory)
171+
TaggingCallFactory(callFactory, this)
157172
} else {
158173
throw IllegalStateException("Can't instantiate PubNub")
159174
}
175+
retrofitBuilder.callFactory(taggingCallFactory)
160176
return retrofitBuilder.build()
161177
}
162178

163179
fun destroy(force: Boolean = false) {
164-
closeExecutor(transactionClientInstance, force)
165-
closeExecutor(subscriptionClientInstance, force)
166-
closeExecutor(noSignatureClientInstance, force)
180+
cancelCalls(transactionClientInstance, force)
181+
cancelCalls(subscriptionClientInstance, force)
182+
cancelCalls(noSignatureClientInstance, force)
167183
}
168184

169-
private fun closeExecutor(
185+
private fun cancelCalls(
170186
client: OkHttpClient?,
171187
force: Boolean,
172188
) {
173-
if (client != null) {
174-
client.dispatcher.cancelAll()
175-
if (force) {
176-
client.connectionPool.evictAll()
177-
val executorService = client.dispatcher.executorService
178-
executorService.shutdown()
189+
client?.dispatcher?.queuedCalls()?.forEach { call ->
190+
if (this == call.request().tag(RetrofitManager::class.java)) {
191+
call.cancel()
192+
}
193+
}
194+
if (force) {
195+
client?.dispatcher?.runningCalls()?.forEach { call ->
196+
if (this == call.request().tag(RetrofitManager::class.java)) {
197+
call.cancel()
198+
}
179199
}
180200
}
181201
}
182202
}
203+
204+
private class TaggingCallFactory(private val originalFactory: Call.Factory, private val retrofitManager: RetrofitManager) : Call.Factory {
205+
override fun newCall(request: Request): Call = originalFactory.newCall(
206+
request.newBuilder().tag(RetrofitManager::class.java, retrofitManager).build()
207+
)
208+
}

‎pubnub-kotlin/pubnub-kotlin-impl/src/main/kotlin/com/pubnub/internal/v2/PNConfigurationImpl.kt

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import com.pubnub.api.v2.PNConfigurationOverride
1111
import okhttp3.Authenticator
1212
import okhttp3.CertificatePinner
1313
import okhttp3.ConnectionSpec
14+
import okhttp3.OkHttpClient
1415
import okhttp3.logging.HttpLoggingInterceptor
1516
import org.slf4j.LoggerFactory
1617
import java.net.Proxy
@@ -71,6 +72,10 @@ class PNConfigurationImpl(
7172
)
7273
),
7374
override val managePresenceListManually: Boolean = false,
75+
override val baseOkHttpClient: OkHttpClient? = null,
76+
override val subscribeOkHttpConfigureAction: ((OkHttpClient.Builder) -> Unit)? = null,
77+
override val nonSubscribeOkHttpConfigureAction: ((OkHttpClient.Builder) -> Unit)? = null,
78+
override val filesOkHttpConfigureAction: ((OkHttpClient.Builder) -> Unit)? = null,
7479
) : PNConfiguration, PNConfigurationOverride {
7580
companion object {
7681
const val DEFAULT_DEDUPE_SIZE = 100
@@ -183,6 +188,15 @@ class PNConfigurationImpl(
183188

184189
override var managePresenceListManually: Boolean = defaultConfiguration.managePresenceListManually
185190

191+
override var baseOkHttpClient: OkHttpClient? = defaultConfiguration.baseOkHttpClient
192+
override var subscribeOkHttpConfigureAction: ((OkHttpClient.Builder) -> Unit)? = defaultConfiguration.subscribeOkHttpConfigureAction
193+
override var nonSubscribeOkHttpConfigureAction: (
194+
(
195+
OkHttpClient.Builder
196+
) -> Unit
197+
)? = defaultConfiguration.nonSubscribeOkHttpConfigureAction
198+
override var filesOkHttpConfigureAction: ((OkHttpClient.Builder) -> Unit)? = defaultConfiguration.filesOkHttpConfigureAction
199+
186200
override fun build(): PNConfiguration {
187201
return PNConfigurationImpl(
188202
userId = userId,
@@ -224,6 +238,10 @@ class PNConfigurationImpl(
224238
pnsdkSuffixes = pnsdkSuffixes,
225239
retryConfiguration = retryConfiguration,
226240
managePresenceListManually = managePresenceListManually,
241+
baseOkHttpClient = baseOkHttpClient,
242+
subscribeOkHttpConfigureAction = subscribeOkHttpConfigureAction,
243+
nonSubscribeOkHttpConfigureAction = nonSubscribeOkHttpConfigureAction,
244+
filesOkHttpConfigureAction = filesOkHttpConfigureAction,
227245
)
228246
}
229247
}

0 commit comments

Comments
 (0)
Please sign in to comment.