Horis 3 years ago
parent
commit
155f6e1e31
1 changed files with 17 additions and 20 deletions
  1. 17 20
      app/src/main/java/io/legado/app/model/analyzeRule/AnalyzeUrl.kt

+ 17 - 20
app/src/main/java/io/legado/app/model/analyzeRule/AnalyzeUrl.kt

@@ -371,6 +371,19 @@ class AnalyzeUrl(
         }
         }
     }
     }
 
 
+    /**
+     * 获取并发记录,若处于并发限制状态下则会等待
+     */
+    private suspend fun getConcurrentRecord(): ConcurrentRecord? {
+        while (true) {
+            try {
+                return fetchStart()
+            } catch (e: ConcurrentException) {
+                delay(e.waitTime.toLong())
+            }
+        }
+    }
+
     /**
     /**
      * 访问网站,返回StrResponse
      * 访问网站,返回StrResponse
      */
      */
@@ -383,15 +396,7 @@ class AnalyzeUrl(
         if (type != null) {
         if (type != null) {
             return StrResponse(url, HexUtil.encodeHexStr(getByteArrayAwait()))
             return StrResponse(url, HexUtil.encodeHexStr(getByteArrayAwait()))
         }
         }
-        var concurrentRecord: ConcurrentRecord?
-        while (true) {
-            try {
-                concurrentRecord = fetchStart()
-                break
-            } catch (e: ConcurrentException) {
-                delay(e.waitTime.toLong())
-            }
-        }
+        val concurrentRecord = getConcurrentRecord()
         try {
         try {
             setCookie()
             setCookie()
             val strResponse: StrResponse
             val strResponse: StrResponse
@@ -455,7 +460,7 @@ class AnalyzeUrl(
             }
             }
             return strResponse
             return strResponse
         } finally {
         } finally {
-            saveCookie()
+            //saveCookie()
             fetchEnd(concurrentRecord)
             fetchEnd(concurrentRecord)
         }
         }
     }
     }
@@ -477,15 +482,7 @@ class AnalyzeUrl(
      */
      */
     @Throws(ConcurrentException::class)
     @Throws(ConcurrentException::class)
     suspend fun getResponseAwait(): Response {
     suspend fun getResponseAwait(): Response {
-        var concurrentRecord: ConcurrentRecord?
-        while (true) {
-            try {
-                concurrentRecord = fetchStart()
-                break
-            } catch (e: ConcurrentException) {
-                delay(e.waitTime.toLong())
-            }
-        }
+        val concurrentRecord = getConcurrentRecord()
         try {
         try {
             setCookie()
             setCookie()
             val response = getProxyClient(proxy).newCallResponse(retry) {
             val response = getProxyClient(proxy).newCallResponse(retry) {
@@ -510,7 +507,7 @@ class AnalyzeUrl(
             }
             }
             return response
             return response
         } finally {
         } finally {
-            saveCookie()
+            //saveCookie()
             fetchEnd(concurrentRecord)
             fetchEnd(concurrentRecord)
         }
         }
     }
     }