From 81c382e5ad1c0e02fc113f1b125ac60667a412e2 Mon Sep 17 00:00:00 2001 From: zhongchao Date: Fri, 25 Feb 2022 14:24:57 +0800 Subject: [PATCH] changed the httpdns logic --- .../com/mogo/cloud/httpdns/HttpDnsHelper.kt | 43 +++++++------------ .../mogo/cloud/httpdns/MogoHttpDnsClient.kt | 4 +- 2 files changed, 17 insertions(+), 30 deletions(-) diff --git a/foudations/mogo-httpdns/src/main/java/com/mogo/cloud/httpdns/HttpDnsHelper.kt b/foudations/mogo-httpdns/src/main/java/com/mogo/cloud/httpdns/HttpDnsHelper.kt index b73df90..b8c0cb2 100644 --- a/foudations/mogo-httpdns/src/main/java/com/mogo/cloud/httpdns/HttpDnsHelper.kt +++ b/foudations/mogo-httpdns/src/main/java/com/mogo/cloud/httpdns/HttpDnsHelper.kt @@ -3,7 +3,9 @@ package com.mogo.cloud.httpdns import android.os.Handler import android.os.HandlerThread import android.os.Message +import android.text.TextUtils import android.util.ArrayMap +import com.mogo.cloud.httpdns.bean.HttpDnsSimpleLocation import com.mogo.cloud.httpdns.listener.IMogoHttpDns import com.mogo.cloud.httpdns.listener.OnAddressChangedListener import com.mogo.cloud.httpdns.util.ApiManager @@ -39,6 +41,7 @@ internal class HttpDnsHelper(private val builder: MogoHttpDnsConfig) : Handler.C private val apiManager: ApiManager private var isInit = false private var defaultUrl: String? = null + private var httpDnsCache: HttpDnsSimpleLocation? = null init { L.isDebug = builder.showDebugLog() @@ -50,7 +53,7 @@ internal class HttpDnsHelper(private val builder: MogoHttpDnsConfig) : Handler.C workThread.start() handler = Handler(workThread.looper, this) apiManager = ApiManager(builder.getSn()!!, builder.getEnv(), builder.getAppKey()) - handler.sendEmptyMessageDelayed(MSG_REQUEST_IP_PORT, builder.getLoopCheckDelay()) + handler.sendEmptyMessage(MSG_REQUEST_IP_PORT) netWorkUtil.registerStatusCallback(builder.getContext()!!) { if (it && !localConnectStateCache) { // 网络状态可用,且本地记录的连接状态为false,开始请求 @@ -60,6 +63,7 @@ internal class HttpDnsHelper(private val builder: MogoHttpDnsConfig) : Handler.C } localConnectStateCache = it } + httpDnsCache = builder.getCurrentLocation()?.getCurrentLocation() } private fun getHttpDnsAddressFromNet() { @@ -76,11 +80,6 @@ internal class HttpDnsHelper(private val builder: MogoHttpDnsConfig) : Handler.C } } - @Volatile - private var isRequest = false - - private val requestCache = ArrayList() - private val requestLock = Any() override fun addressChangedListener(addressChangedListener: OnAddressChangedListener) { @@ -100,43 +99,24 @@ internal class HttpDnsHelper(private val builder: MogoHttpDnsConfig) : Handler.C override fun getHttpDnsAddress(type: Int, _host: String): String? { val host = _host.toLowerCase(Locale.getDefault()) if (isInit) { - requestCache.add("$type-$host") synchronized(requestLock) { - L.d( - TAG, - "getHttpDnsAddress: $type-$host \n thread: ${Thread.currentThread().name} isRequest: $isRequest" - ) - if (!isRequest) { - isRequest = true - L.d( - TAG, - "prepare to get http dns from net thread: ${Thread.currentThread().name}" - ) - getHttpDnsAddressFromNet() - } + getHttpDnsAddressFromNet() } addressMap?.let { val builder = StringBuilder() for (key in it.keys) { builder.append(key).append(" : ").append(it[key]).append("\n") } - L.d(TAG, "getHttpDnsAddress over $type-$host ${builder.toString()}") - requestCache.remove("$type-$host") - L.d(TAG, "requestCache.size: ${requestCache.size}") - if (requestCache.isEmpty()) { - isRequest = false - } + L.d(TAG, "getHttpDnsAddress over $type-$host $builder") return if (it["$type-$host"] == null) { defaultUrl ?: host } else { it["$type-$host"] } } - L.d(TAG, "getHttpDnsAddress over addressMap is null") } else { L.d(TAG, "not init over") throw IllegalStateException("Http dns not init") - } return defaultUrl ?: host } @@ -144,7 +124,14 @@ internal class HttpDnsHelper(private val builder: MogoHttpDnsConfig) : Handler.C override fun getHttpDnsCachedAddress(type: Int, _host: String): String? { val host = _host.toLowerCase(Locale.getDefault()) L.d(TAG, "getHttpDnsCachedAddress: $type-$host") - return addressMap?.get("$type-$host") + httpDnsCache?.let { + val currentLoc = builder.getCurrentLocation()?.getCurrentLocation() + if (currentLoc != null && !TextUtils.equals(it.cityCode, currentLoc.cityCode)) { + getHttpDnsAddress(type, _host) + httpDnsCache = currentLoc + } + } + return addressMap?.get("$type-$host") ?: defaultUrl } override fun getAllAddress(): Map? { diff --git a/foudations/mogo-httpdns/src/main/java/com/mogo/cloud/httpdns/MogoHttpDnsClient.kt b/foudations/mogo-httpdns/src/main/java/com/mogo/cloud/httpdns/MogoHttpDnsClient.kt index cde63ec..60b14d4 100644 --- a/foudations/mogo-httpdns/src/main/java/com/mogo/cloud/httpdns/MogoHttpDnsClient.kt +++ b/foudations/mogo-httpdns/src/main/java/com/mogo/cloud/httpdns/MogoHttpDnsClient.kt @@ -8,7 +8,7 @@ object MogoHttpDnsClient : IMogoHttpDns { private var httpDnsHelper: HttpDnsHelper? = null fun init(config: MogoHttpDnsConfig) { - // httpdns init + // httpDns init if (httpDnsHelper == null) { httpDnsHelper = HttpDnsHelper(config) } @@ -18,7 +18,7 @@ object MogoHttpDnsClient : IMogoHttpDns { * 先从本地缓存中根据type和host获取ip:port,如果本地缓存中没有,再通过网络获取 */ fun getHttpDnsAddressUseCacheIfNecessary(type: Int, _host: String): String? { - return getHttpDnsCachedAddress(type, _host) ?: return getHttpDnsAddress(type, _host) + return getHttpDnsCachedAddress(type, _host) } override fun addressChangedListener(addressChangedListener: OnAddressChangedListener) {