@@ -14,6 +14,7 @@ import com.jd.jdcache.service.DelegateManager
14
14
import com.jd.jdcache.service.base.*
15
15
import com.jd.jdcache.util.*
16
16
import com.jd.jdcache.util.CoroutineHelper.launchCoroutine
17
+ import com.jd.jdcache.util.CoroutineHelper.runOnIo
17
18
import com.jd.jdcache.util.JDCacheLog.d
18
19
import com.jd.jdcache.util.JDCacheLog.e
19
20
import com.jd.jdcache.util.UrlHelper.convertHeader
@@ -203,30 +204,54 @@ open class PreloadHtmlMatcher : JDCacheResourceMatcher() {
203
204
204
205
protected open fun geDownloadLocalResp () : JDCacheLocalResp ? {
205
206
return waitingChannel?.let {
206
- runBlocking {
207
- try {
208
- log { d(name, " Waiting for receiving pre-download html file." ) }
209
- // 等待下载完成
210
- withTimeout(2000L ) {
211
- waitingChannel?.receive()
207
+ if (! it.isClosedForReceive) {
208
+ runBlocking {
209
+ try {
210
+ log { d(name, " Waiting for receiving pre-download html file." ) }
211
+ // 等待下载完成
212
+ withTimeout(2000L ) {
213
+ it.receive()
214
+ }
215
+ } catch (e: TimeoutCancellationException ) {
216
+ log { d(name, " Timeout in receiving pre-download html file." ) }
217
+ null
218
+ } catch (e: Exception ) {
219
+ log { e(name, " Error in receiving pre-download html file, e = $e " ) }
220
+ null
212
221
}
213
- } catch (e: TimeoutCancellationException ) {
214
- log { d(name, " Timeout in receiving pre-download html file." ) }
215
- null
216
- } catch (e: Exception ) {
217
- log { e(name, " Error in receiving pre-download html file, e = $e " ) }
218
- null
219
222
}
223
+ } else {
224
+ null
220
225
}
221
226
}
222
227
}
223
228
224
229
override fun onDestroy () {
225
230
super .onDestroy()
226
- waitingChannel?.cancel()
227
- downloadTask?.cancel()
231
+ waitingChannel?.let {
232
+ it.cancel()
233
+ waitingChannel = null
234
+ }
235
+ downloadTask?.let {
236
+ it.cancel()
237
+ downloadTask = null
238
+ }
228
239
229
- localResp?.fileStream?.close()
240
+ val fileStream = localResp?.fileStream
241
+ fileStream?.let {
242
+ if (it !is PreReadInputStream || ! it.isClosed()) {
243
+ launchCoroutine {
244
+ runOnIo {
245
+ try {
246
+ @Suppress(" BlockingMethodInNonBlockingContext" )
247
+ it.close()
248
+ } catch (e: Throwable ) {
249
+ log { e(name, e) }
250
+ }
251
+ }
252
+ }
253
+ }
254
+ }
230
255
localResp?.filename?.let { fileRepo?.deleteFile(it) }
231
256
}
232
257
}
0 commit comments