Skip to content

Commit

Permalink
Merge branch 'v1.8/dev'
Browse files Browse the repository at this point in the history
  • Loading branch information
Mingyueyixi committed Feb 3, 2023
2 parents c39ac62 + 56cb7fe commit 8922246
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 9 deletions.
11 changes: 6 additions & 5 deletions app/src/main/java/com/lu/wxmask/ui/JsonMenuManager.kt
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ class JsonMenuManager {

fun updateMenuListFromRemoteIfNeed(ctx: Context) {
// 2 hour
if (System.currentTimeMillis() - lastUpdateSuccessMills > 1000 * 60 * 60 * 2) {
if (!isRemoteUpdating && System.currentTimeMillis() - lastUpdateSuccessMills > 1000 * 60 * 60 * 2) {
updateMenuListFromRemote(ctx)
}
}
Expand All @@ -122,9 +122,10 @@ class JsonMenuManager {
val context = ctx.applicationContext

val rawJsonMenuUrl = "https://raw.githubusercontent.com/Mingyueyixi/MaskWechat/main/$releatePath"
LogUtil.i("request $rawJsonMenuUrl")
isRemoteUpdating = true
HttpConnectUtil.get(rawJsonMenuUrl, HttpConnectUtil.noCacheHttpHeader) {
HttpConnectUtil.getWithRetry(rawJsonMenuUrl, HttpConnectUtil.noCacheHttpHeader, 3, { retryCount, res ->
LogUtil.i("onFetch retry:$retryCount", rawJsonMenuUrl)
}, {
if (it.error == null && it.code == 200 && it.body.isNotEmpty()) {
writeRemoteToLocal(context, it.body)
isRemoteUpdating = false
Expand All @@ -133,7 +134,7 @@ class JsonMenuManager {
//val githubRawPath = "https://github.com/$releatePath"
//@main分支 或者@v1.6, commit id之类的,直接在写/main有时候不行
//不指定版本,则取最后一个https://www.jsdelivr.com/?docs=gh
val cdnRawPath = "https://cdn.jsdelivr.net/gh/Mingyueyixi/MaskWechat/$releatePath"
val cdnRawPath = "https://cdn.jsdelivr.net/gh/Mingyueyixi/MaskWechat@main/$releatePath"
LogUtil.i("request $cdnRawPath")
HttpConnectUtil.get(cdnRawPath, HttpConnectUtil.noCacheHttpHeader) { cdnRes ->
if (cdnRes.error == null && cdnRes.code == 200 && cdnRes.body.isNotEmpty()) {
Expand All @@ -146,7 +147,7 @@ class JsonMenuManager {

}

}
})
}

private fun writeRemoteToLocal(context: Context, body: ByteArray) {
Expand Down
31 changes: 27 additions & 4 deletions app/src/main/java/com/lu/wxmask/util/http/HttpConnectUtil.kt
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
package com.lu.wxmask.util.http

import com.lu.magic.util.thread.AppExecutor
import java.io.InputStream
import java.io.OutputStream
import java.net.HttpURLConnection
import java.net.URL
import java.nio.charset.Charset
import java.util.concurrent.LinkedBlockingQueue
import java.util.concurrent.ThreadPoolExecutor
import java.util.concurrent.TimeUnit
Expand All @@ -29,8 +27,8 @@ class HttpConnectUtil {
//虽然http协议允许重复的 header,但实际没什么用,所以这里不许
var header: Map<String, String> = mutableMapOf(),
var body: ByteArray? = null,
var connectTimeOut: Int = 5000,
var readTimeOut: Int = 5000
var connectTimeOut: Int = 10000,
var readTimeOut: Int = 10000
) {
override fun toString(): String {
return "Request(url='$url', method='$method', header=$header, body=${body?.toString(Charsets.UTF_8)}, connectTimeOut=$connectTimeOut, readTimeOut=$readTimeOut)"
Expand Down Expand Up @@ -100,6 +98,31 @@ class HttpConnectUtil {
}
}

@JvmStatic
fun getWithRetry(
url: String,
header: Map<String, String>,
retryCount: Int,
onEachFetch: (retryCount: Int, res: Response) -> Unit,
onFinalCallback: (resp: Response) -> Unit
) {
httpExecutor.submit {
val fetcher = Fetcher(Request(url, "GET", header))
var res = fetcher.fetch()
onEachFetch(0, res)
if (res.error != null) {
for (i in 0 until retryCount) {
res = fetcher.fetch()
onEachFetch(i + 1, res)
if (res.error == null) {
break
}
}
}
onFinalCallback.invoke(res)
}
}

@JvmStatic
fun postJson(url: String, json: String, callback: (resp: Response) -> Unit) {
httpExecutor.submit {
Expand Down

0 comments on commit 8922246

Please sign in to comment.