From 71638cdc63c55bf782cb40b5ec20a5854ca8080a Mon Sep 17 00:00:00 2001 From: lemondark <743855454@qq.com> Date: Thu, 29 Jun 2023 11:50:26 +0800 Subject: [PATCH 01/15] =?UTF-8?q?feat:=20http=20dns=E6=94=AF=E6=8C=81keep-?= =?UTF-8?q?alive?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/com/tencent/msdk/dns/base/utils/HttpHelper.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/com/tencent/msdk/dns/base/utils/HttpHelper.java b/src/main/java/com/tencent/msdk/dns/base/utils/HttpHelper.java index 1481713..05653b3 100644 --- a/src/main/java/com/tencent/msdk/dns/base/utils/HttpHelper.java +++ b/src/main/java/com/tencent/msdk/dns/base/utils/HttpHelper.java @@ -31,7 +31,7 @@ public static String getRequest(String urlStr) { String host = url.getHost(); String file = url.getFile(); return GET_METHOD + ' ' + file + ' ' + HTTP_VERSION + CRLF + - "Connection: close" + CRLF + + "Connection: keep-alive" + CRLF + HOST_HEADER + ": " + host + CRLF + CRLF; } catch (MalformedURLException e) { From 597810af26e75d34b9140c43e983e9078100dad5 Mon Sep 17 00:00:00 2001 From: lemondark <743855454@qq.com> Date: Fri, 14 Jul 2023 18:00:31 +0800 Subject: [PATCH 02/15] =?UTF-8?q?fix:=20=E9=94=99=E8=AF=AF=E8=A7=A3?= =?UTF-8?q?=E6=9E=90=E6=97=A5=E5=BF=97=E4=B8=8A=E6=8A=A5=E4=BF=AE=E5=A4=8D?= =?UTF-8?q?=EF=BC=8C=E7=89=B9=E5=88=AB=E6=98=AF=E8=AF=B7=E6=B1=82=E8=BF=94?= =?UTF-8?q?=E5=9B=9E=E6=AD=A3=E5=B8=B8=E5=9F=9F=E5=90=8D=E6=97=A0=E8=A7=A3?= =?UTF-8?q?=E6=9E=90=E8=AE=B0=E5=BD=95=E5=9C=BA=E6=99=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../msdk/dns/core/rest/share/AbsHttpDns.java | 37 +++++++++---------- .../msdk/dns/core/rest/share/AbsRestDns.java | 7 +++- .../tencent/msdk/dns/report/ReportHelper.java | 37 ++++++++++--------- 3 files changed, 43 insertions(+), 38 deletions(-) diff --git a/src/main/java/com/tencent/msdk/dns/core/rest/share/AbsHttpDns.java b/src/main/java/com/tencent/msdk/dns/core/rest/share/AbsHttpDns.java index 5717705..58b8d69 100644 --- a/src/main/java/com/tencent/msdk/dns/core/rest/share/AbsHttpDns.java +++ b/src/main/java/com/tencent/msdk/dns/core/rest/share/AbsHttpDns.java @@ -89,7 +89,7 @@ public LookupResult lookup(LookupParameters lookupParams) { } // 接受返回string String rawRspContent = ""; - String lineTxt = ""; + String lineTxt; try { // 发起请求 HttpURLConnection connection = (HttpURLConnection) new URL(urlStr).openConnection(); @@ -97,10 +97,9 @@ public LookupResult lookup(LookupParameters lookupParams) { connection.setReadTimeout(timeoutMills); // noinspection CharsetObjectCanBeUsed // 读取网络请求结果 - reader = new BufferedReader(new InputStreamReader( - connection.getInputStream(), "UTF-8")); + reader = new BufferedReader(new InputStreamReader(connection.getInputStream(), "UTF-8")); // 结果赋值 -// rawRspContent = reader.readLine(); + // rawRspContent = reader.readLine(); while ((lineTxt = reader.readLine()) != null) { lineTxt += '\n'; rawRspContent += lineTxt; @@ -130,13 +129,16 @@ public LookupResult lookup(LookupParameters lookupParams) { stat.errorCode = ErrorCode.PARSE_RESPONSE_CONTENT_FAILED_ERROR_CODE; return new LookupResult<>(stat.ips, stat); } - if (rsp.ips == Const.EMPTY_IPS) { - DnsLog.d(getTag() + "receive success, but no " + (DnsDescription.Family.INET6 == mFamily ? "INET6" : "INET") + " record"); + stat.clientIp = rsp.clientIp; + stat.ttl = rsp.ttl; + stat.ips = rsp.ips; + if (rsp.ips.length == 0) { + DnsLog.d(getTag() + "receive success, but no record"); stat.isGetEmptyResponse = true; stat.errorCode = ErrorCode.NO_RECORD; return new LookupResult<>(stat.ips, stat); } - // 返回值处理 + // 返回值正常处理 mCacheHelper.put(lookupParams, rsp); stat.errorCode = ErrorCode.SUCCESS; stat.clientIp = rsp.clientIp; @@ -253,8 +255,7 @@ public IToken getToken() { } try { // Selector注册监听 - mSelectionKey = mChannel.register(selector, - SelectionKey.OP_CONNECT | SelectionKey.OP_READ | SelectionKey.OP_WRITE); + mSelectionKey = mChannel.register(selector, SelectionKey.OP_CONNECT | SelectionKey.OP_READ | SelectionKey.OP_WRITE); mSelectionKey.attach(mChannel); } catch (Exception e) { mStat.errorCode = ErrorCode.REGISTER_CHANNEL_FAILED_ERROR_CODE; @@ -358,14 +359,13 @@ protected Response responseInternal() { mReadStringBuilder = new StringBuilder(); } ByteBuffer rspBuf = mReadBuffer; - int rspLen = 0; + int rspLen; int totalLen = 0; StringBuilder sb = mReadStringBuilder; do { try { rspLen = mChannel.read(rspBuf); - DnsLog.d(getTag() + "receive response get len:%d, lastLen:%d", - rspLen, totalLen); + DnsLog.d(getTag() + "receive response get len:%d, lastLen:%d", rspLen, totalLen); if (rspLen > 0) { totalLen += rspLen; // 此时就处理数据 @@ -376,8 +376,7 @@ protected Response responseInternal() { String rspHttpRsp = new String(rawRsp, Charset.forName("UTF-8")); sb.append(rspHttpRsp); if (HttpHelper.checkHttpRspFinished(rspHttpRsp)) { - DnsLog.d(getTag() + "receive response check http rsp finished:%d, so break", - rspLen); + DnsLog.d(getTag() + "receive response check http rsp finished:%d, so break", rspLen); break; } // 重新清空rspBuf,并再次接收 @@ -413,8 +412,7 @@ protected Response responseInternal() { DnsLog.v(getTag() + "receive rspHttpRsp:{\n%s}", rspHttpRsp); String rspBody = HttpHelper.responseBody(rspHttpRsp); String rspContent = decrypt(rspBody, lookupExtra.bizKey); - int rspStatus = HttpHelper.responseStatus(rspHttpRsp); - mStat.statusCode = rspStatus; + mStat.statusCode = HttpHelper.responseStatus(rspHttpRsp); DnsLog.d(getTag() + "receive rawLen:%d, raw:[%s], rsp body content:[%s]", totalLen, rspBody, rspContent); if (TextUtils.isEmpty(rspContent)) { mStat.isGetEmptyResponse = true; @@ -423,11 +421,12 @@ protected Response responseInternal() { } Response resParser = ResponseParser.parseResponse(mFamily, rspContent); // 将hdns有返回但域名自身解析记录配置为空的情况独立出来 - if (resParser.ips == Const.EMPTY_IPS) { - DnsLog.d(getTag() + "receive success, but no " + (DnsDescription.Family.INET6 == mFamily ? "INET6" : "INET") + " record"); + if (resParser.ips.length == 0) { + DnsLog.d(getTag() + "receive success, but no record"); mStat.isGetEmptyResponse = true; mStat.errorCode = ErrorCode.NO_RECORD; - return Response.EMPTY; + } else { + mStat.errorCode = ErrorCode.SUCCESS; } return resParser; diff --git a/src/main/java/com/tencent/msdk/dns/core/rest/share/AbsRestDns.java b/src/main/java/com/tencent/msdk/dns/core/rest/share/AbsRestDns.java index 56b213b..bffa373 100644 --- a/src/main/java/com/tencent/msdk/dns/core/rest/share/AbsRestDns.java +++ b/src/main/java/com/tencent/msdk/dns/core/rest/share/AbsRestDns.java @@ -166,10 +166,13 @@ public final String[] receiveResponse() { return mStat.ips; } rsp = responseInternal(); - if (rsp != Response.EMPTY && rsp != Response.NEED_CONTINUE) { - mStat.errorCode = ErrorCode.SUCCESS; + if (mStat.errorCode == ErrorCode.SUCCESS) { mCacheHelper.put(mLookupContext.asLookupParameters(), rsp); } +// if (rsp != Response.EMPTY && rsp != Response.NEED_CONTINUE && rsp.ips.length > 0) { +// mStat.errorCode = ErrorCode.SUCCESS; +// mCacheHelper.put(mLookupContext.asLookupParameters(), rsp); +// } mStat.clientIp = rsp.clientIp; mStat.ttl = rsp.ttl; mStat.expiredTime = System.currentTimeMillis() + rsp.ttl * 1000; diff --git a/src/main/java/com/tencent/msdk/dns/report/ReportHelper.java b/src/main/java/com/tencent/msdk/dns/report/ReportHelper.java index a0adf51..265addb 100644 --- a/src/main/java/com/tencent/msdk/dns/report/ReportHelper.java +++ b/src/main/java/com/tencent/msdk/dns/report/ReportHelper.java @@ -29,7 +29,7 @@ public final class ReportHelper { private static Runnable sReportAsyncLookupEventTask = new Runnable() { @Override public void run() { - // attta上报统计数据 + // atta上报统计数据 Map cacheStatisticsMap = CacheStatisticsReport.offerAll(); attaReportStatisticsEvent(cacheStatisticsMap); DnsExecutors.MAIN.cancel(sReportAsyncLookupEventTask); @@ -193,27 +193,30 @@ private static void attaReportLookupEvent(String eventName, LookupResult lookupR String dnsIp = backupInfo.getDnsIp(); String reqType = AttaHelper.getReqType(statMerge.curNetStack); - // ErrorCode==2 进行容灾处理 - if (statMerge.restDnsStat.errorCode == 2 || (Const.HTTPS_CHANNEL.equals(sDnsConfig.channel) && (statMerge.restDnsStat.errorCode == 1))) { - // 解析失败,仅当达到最大失败次数满足切换IP时候上报 - if (sDnsConfig.enableReport && backupInfo.getCanReport(backupInfo.getErrorCount() + 1)) { - DnsExecutors.MAIN.execute(AttaHelper.report(carrierCode, statMerge.netType, sDnsConfig.lookupExtra.bizId, sDnsConfig.appId, sDnsConfig.channel, eventName, System.currentTimeMillis(), dnsIp, statMerge.restDnsStat.costTimeMills, statMerge.hostname, reqType, sDnsConfig.timeoutMills, statMerge.restDnsStat.ttl, statMerge.restDnsStat.errorCode, statMerge.restDnsStat.statusCode, statMerge.restDnsStat.cached, 1, CommonUtils.toStringList(statMerge.localDnsStat.ips, ReportConst.IP_SPLITTER), CommonUtils.toStringList(statMerge.restDnsStat.ips, ReportConst.IP_SPLITTER))); - } - // 报错记录+1 - backupInfo.incrementErrorCount(); - DnsLog.d("dnsip连接失败, 当前失败次数:" + backupInfo.getErrorCount()); - } - - // 请求正常时的上报逻辑全量上报 - if (statMerge.restDnsStat.errorCode == 0 && !statMerge.restDnsStat.cached) { - // 请求成功后将ErrorCount置为0 - backupInfo.setErrorCount(0); - if (sDnsConfig.enableReport) { + if (sDnsConfig.enableReport && !statMerge.restDnsStat.cached) { + // 请求正常时的上报逻辑全量上报 + if (statMerge.restDnsStat.errorCode == 0) { + // 请求成功后将ErrorCount置为0 + backupInfo.setErrorCount(0); DnsExecutors.MAIN.execute(AttaHelper.report(carrierCode, statMerge.netType, sDnsConfig.lookupExtra.bizId, sDnsConfig.appId, sDnsConfig.channel, eventName, System.currentTimeMillis(), dnsIp, statMerge.restDnsStat.costTimeMills, statMerge.hostname, reqType, sDnsConfig.timeoutMills, statMerge.restDnsStat.ttl, statMerge.restDnsStat.errorCode, statMerge.restDnsStat.statusCode, statMerge.restDnsStat.cached, 1, CommonUtils.toStringList(statMerge.localDnsStat.ips, ReportConst.IP_SPLITTER), CommonUtils.toStringList(statMerge.restDnsStat.ips, ReportConst.IP_SPLITTER))); + } else { + // ErrorCode==2 进行容灾处理 + if (statMerge.restDnsStat.errorCode == 2 || (Const.HTTPS_CHANNEL.equals(sDnsConfig.channel) && (statMerge.restDnsStat.errorCode == 1))) { + // 解析失败,仅当达到最大失败次数满足切换IP时候上报 + if (backupInfo.getCanReport(backupInfo.getErrorCount() + 1)) { + DnsExecutors.MAIN.execute(AttaHelper.report(carrierCode, statMerge.netType, sDnsConfig.lookupExtra.bizId, sDnsConfig.appId, sDnsConfig.channel, eventName, System.currentTimeMillis(), dnsIp, statMerge.restDnsStat.costTimeMills, statMerge.hostname, reqType, sDnsConfig.timeoutMills, statMerge.restDnsStat.ttl, statMerge.restDnsStat.errorCode, statMerge.restDnsStat.statusCode, statMerge.restDnsStat.cached, 1, CommonUtils.toStringList(statMerge.localDnsStat.ips, ReportConst.IP_SPLITTER), CommonUtils.toStringList(statMerge.restDnsStat.ips, ReportConst.IP_SPLITTER))); + } + // 报错记录+1 + backupInfo.incrementErrorCount(); + DnsLog.d("dnsip连接失败, 当前失败次数:" + backupInfo.getErrorCount()); + } else { + DnsExecutors.MAIN.execute(AttaHelper.report(carrierCode, statMerge.netType, sDnsConfig.lookupExtra.bizId, sDnsConfig.appId, sDnsConfig.channel, eventName, System.currentTimeMillis(), dnsIp, statMerge.restDnsStat.costTimeMills, statMerge.hostname, reqType, sDnsConfig.timeoutMills, statMerge.restDnsStat.ttl, statMerge.restDnsStat.errorCode, statMerge.restDnsStat.statusCode, statMerge.restDnsStat.cached, 1, CommonUtils.toStringList(statMerge.localDnsStat.ips, ReportConst.IP_SPLITTER), CommonUtils.toStringList(statMerge.restDnsStat.ips, ReportConst.IP_SPLITTER))); + } } } } + private static void attaReportStatisticsEvent(Map cacheStatisticsMap) { if (!sDnsConfig.enableReport) { return; From d06c50a38d9768f43481452a6f5c50504b5b60cb Mon Sep 17 00:00:00 2001 From: lemondark <743855454@qq.com> Date: Fri, 14 Jul 2023 18:02:16 +0800 Subject: [PATCH 03/15] =?UTF-8?q?fix:=20=E8=A7=A3=E6=9E=90=E6=97=A5?= =?UTF-8?q?=E5=BF=97=E5=BC=80=E5=85=B3=E4=B8=8B=E7=BA=BF=EF=BC=8C=E4=BF=AE?= =?UTF-8?q?=E5=A4=8D=E6=9C=8D=E5=8A=A1=E8=AF=B7=E6=B1=82=E6=97=A0=E6=B3=95?= =?UTF-8?q?=E8=8E=B7=E5=8F=96=E6=97=B6enableReport=E4=BB=8D=E7=94=9F?= =?UTF-8?q?=E6=95=88=E5=9C=BA=E6=99=AF=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/com/tencent/msdk/dns/DnsConfig.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/com/tencent/msdk/dns/DnsConfig.java b/src/main/java/com/tencent/msdk/dns/DnsConfig.java index 30b969d..ee6b402 100644 --- a/src/main/java/com/tencent/msdk/dns/DnsConfig.java +++ b/src/main/java/com/tencent/msdk/dns/DnsConfig.java @@ -590,7 +590,7 @@ public Builder https() { */ @Deprecated public Builder enableReport(boolean enableReport) { - mEnableReport = enableReport; +// mEnableReport = enableReport; return this; } From f998a4150afdbe8e710e87b9c724b454a58b5160 Mon Sep 17 00:00:00 2001 From: lemondark <743855454@qq.com> Date: Mon, 17 Jul 2023 17:18:19 +0800 Subject: [PATCH 04/15] =?UTF-8?q?fix:=20https=E8=AF=B7=E6=B1=82=E8=B6=85?= =?UTF-8?q?=E6=97=B6=E6=97=B6=E9=94=99=E8=AF=AF=E7=A0=81=E5=8F=8A=E7=BB=93?= =?UTF-8?q?=E6=9E=9C=E5=A4=84=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/tencent/msdk/dns/core/rest/share/AbsHttpDns.java | 9 +++++---- .../java/com/tencent/msdk/dns/report/ReportHelper.java | 2 +- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/src/main/java/com/tencent/msdk/dns/core/rest/share/AbsHttpDns.java b/src/main/java/com/tencent/msdk/dns/core/rest/share/AbsHttpDns.java index 58b8d69..d89bca9 100644 --- a/src/main/java/com/tencent/msdk/dns/core/rest/share/AbsHttpDns.java +++ b/src/main/java/com/tencent/msdk/dns/core/rest/share/AbsHttpDns.java @@ -109,8 +109,12 @@ public LookupResult lookup(LookupParameters lookupParams) { reader.close(); stat.statusCode = connection.getResponseCode(); } catch (Exception e) { - stat.errorCode = ErrorCode.RESPONSE_FAILED_FOR_EXCEPTION_ERROR_CODE; + if (!(e instanceof java.net.SocketTimeoutException)) { + stat.errorCode = ErrorCode.RESPONSE_FAILED_FOR_EXCEPTION_ERROR_CODE; + } stat.errorMsg = e.getMessage(); + stat.isGetEmptyResponse = true; + // return new LookupResult<>(stat.ips, stat); throw e; } @@ -141,10 +145,7 @@ public LookupResult lookup(LookupParameters lookupParams) { // 返回值正常处理 mCacheHelper.put(lookupParams, rsp); stat.errorCode = ErrorCode.SUCCESS; - stat.clientIp = rsp.clientIp; - stat.ttl = rsp.ttl; stat.expiredTime = System.currentTimeMillis() + rsp.ttl * 1000; - stat.ips = rsp.ips; } catch (Exception e) { DnsLog.d(e, getTag() + "lookup failed"); } finally { diff --git a/src/main/java/com/tencent/msdk/dns/report/ReportHelper.java b/src/main/java/com/tencent/msdk/dns/report/ReportHelper.java index 265addb..1e143a8 100644 --- a/src/main/java/com/tencent/msdk/dns/report/ReportHelper.java +++ b/src/main/java/com/tencent/msdk/dns/report/ReportHelper.java @@ -200,7 +200,7 @@ private static void attaReportLookupEvent(String eventName, LookupResult lookupR backupInfo.setErrorCount(0); DnsExecutors.MAIN.execute(AttaHelper.report(carrierCode, statMerge.netType, sDnsConfig.lookupExtra.bizId, sDnsConfig.appId, sDnsConfig.channel, eventName, System.currentTimeMillis(), dnsIp, statMerge.restDnsStat.costTimeMills, statMerge.hostname, reqType, sDnsConfig.timeoutMills, statMerge.restDnsStat.ttl, statMerge.restDnsStat.errorCode, statMerge.restDnsStat.statusCode, statMerge.restDnsStat.cached, 1, CommonUtils.toStringList(statMerge.localDnsStat.ips, ReportConst.IP_SPLITTER), CommonUtils.toStringList(statMerge.restDnsStat.ips, ReportConst.IP_SPLITTER))); } else { - // ErrorCode==2 进行容灾处理 + // ErrorCode==2 (超时)进行容灾处理,https请求存在请求异常超时时间>timeoutMills,此时errCode为1 if (statMerge.restDnsStat.errorCode == 2 || (Const.HTTPS_CHANNEL.equals(sDnsConfig.channel) && (statMerge.restDnsStat.errorCode == 1))) { // 解析失败,仅当达到最大失败次数满足切换IP时候上报 if (backupInfo.getCanReport(backupInfo.getErrorCount() + 1)) { From 2f1ae930355fca3e3ff7d6c4cb1267985b726e2f Mon Sep 17 00:00:00 2001 From: lemondark <743855454@qq.com> Date: Wed, 19 Jul 2023 20:26:41 +0800 Subject: [PATCH 05/15] =?UTF-8?q?feat:=20=E6=89=B9=E9=87=8F=E8=A7=A3?= =?UTF-8?q?=E6=9E=90=E8=AF=BB=E5=8F=96=E4=BB=A5=E5=8D=95=E4=B8=AA=E5=9F=9F?= =?UTF-8?q?=E5=90=8D=E4=B8=BA=E7=BB=B4=E5=BA=A6=E5=A4=84=E7=90=86=EF=BC=8C?= =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E9=83=A8=E5=88=86=E5=91=BD=E4=B8=AD=E7=BC=93?= =?UTF-8?q?=E5=AD=98=E7=8A=B6=E6=80=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/tencent/msdk/dns/core/DnsManager.java | 32 ++++++-- .../java/com/tencent/msdk/dns/core/IDns.java | 2 + .../msdk/dns/core/IStatisticsMerge.java | 5 ++ .../tencent/msdk/dns/core/LookupContext.java | 4 + .../msdk/dns/core/LookupParameters.java | 6 ++ .../tencent/msdk/dns/core/local/LocalDns.java | 4 + .../msdk/dns/core/rest/share/AbsHttpDns.java | 4 +- .../msdk/dns/core/rest/share/AbsRestDns.java | 81 ++++++++++++++++--- .../msdk/dns/core/rest/share/CacheHelper.java | 9 +-- .../msdk/dns/core/stat/AbsStatistics.java | 5 ++ .../msdk/dns/core/stat/StatisticsMerge.java | 9 +++ 11 files changed, 132 insertions(+), 29 deletions(-) diff --git a/src/main/java/com/tencent/msdk/dns/core/DnsManager.java b/src/main/java/com/tencent/msdk/dns/core/DnsManager.java index a550274..410de67 100644 --- a/src/main/java/com/tencent/msdk/dns/core/DnsManager.java +++ b/src/main/java/com/tencent/msdk/dns/core/DnsManager.java @@ -17,6 +17,7 @@ import java.io.IOException; import java.nio.channels.Selector; import java.util.ArrayList; +import java.util.Arrays; import java.util.Collections; import java.util.Iterator; import java.util.List; @@ -132,7 +133,7 @@ LookupResult getResultFromCache(LookupParameters } LookupResult lookupResultFromCache = dns.getResultFromCache(lookupParams); - if (lookupResultFromCache.stat.lookupSuccess()) { + if (lookupResultFromCache.stat.lookupSuccess() || lookupResultFromCache.stat.lookupPartCached()) { lookupContext.sorter().put(dns, lookupResultFromCache.ipSet.ips); lookupContext.statisticsMerge() .merge(dns, lookupResultFromCache.stat); @@ -145,6 +146,7 @@ LookupResult getResultFromCache(LookupParameters lookupResult.ipSet + "; " + lookupResult.stat); return lookupResult; } + return new LookupResult( IpSet.EMPTY, statMerge); } @@ -233,17 +235,19 @@ LookupResult lookup(LookupParameters lookupParams // NOTE: sessions仅会被当前线程访问 List sessions = new ArrayList<>(); lookupContext.sessions(sessions); + LookupResult lookupResultFromCache = new LookupResult( + IpSet.EMPTY, new StatisticsMerge(lookupParams.appContext)); try { // NOTE: 当前对外API上, 不支持AAAA记录的解析, 需要保留LocalDns的解析结果作为AAAA解析结果 // 暂时不忽略LocalDns解析结果(即超时时间内会等待LocalDns解析结果, 无论RestDns是否已经解析成功) if (null != restDnsGroup) { // 先查缓存,有其一即可 - LookupResult lookupResult = getResultFromCache(lookupParams); - DnsLog.d("getResultFromCache: " + lookupResult); - if (lookupResult.stat.lookupSuccess()) { - lookupResultHolder.mLookupResult = lookupResult; + lookupResultFromCache = getResultFromCache(lookupParams); + DnsLog.d("getResultFromCache: " + lookupResultFromCache); + if (lookupResultFromCache.stat.lookupSuccess()) { + lookupResultHolder.mLookupResult = lookupResultFromCache; DnsLog.d("DnsManager lookup getResultFromCache success"); - return lookupResult; + return lookupResultFromCache; } // 打开Selector prepareTasks(restDnsGroup, lookupContext); @@ -291,6 +295,19 @@ LookupResult lookup(LookupParameters lookupParams } } IpSet ipSet = sorter.sort(); + // todo 阻塞 合并 + if(lookupResultFromCache.stat.lookupPartCached()) { + IpSet ipSet1 = lookupResultFromCache.ipSet; + int ipSetLen = ipSet.ips.length; + int ipSetLen1 = ipSet1.ips.length; + String[] str1 = Arrays.copyOf(ipSet.ips, ipSetLen + ipSetLen1); + System.arraycopy(ipSet1, 0, str1, ipSetLen, ipSetLen1); +// ipSet.ips = str1; +// IpSet(str1); + ipSet = new IpSet(str1); + } + + statMerge.statResult(ipSet); LookupResult lookupResult = new LookupResult(ipSet, statMerge); @@ -337,6 +354,9 @@ LookupResult lookup(LookupParameters lookupParams } catch (Exception ignored) { } IpSet ipSet = sorter.sort(); + if (lookupResultFromCache.stat.lookupPartCached()) { + // todo part cached + } statMerge.statResult(ipSet); LookupResult lookupResult = new LookupResult(ipSet, statMerge); diff --git a/src/main/java/com/tencent/msdk/dns/core/IDns.java b/src/main/java/com/tencent/msdk/dns/core/IDns.java index be8bb6b..2303200 100644 --- a/src/main/java/com/tencent/msdk/dns/core/IDns.java +++ b/src/main/java/com/tencent/msdk/dns/core/IDns.java @@ -66,6 +66,8 @@ interface IStatistics { boolean lookupNeedRetry(); boolean lookupFailed(); + + boolean lookupPartCached(); } DnsDescription getDescription(); diff --git a/src/main/java/com/tencent/msdk/dns/core/IStatisticsMerge.java b/src/main/java/com/tencent/msdk/dns/core/IStatisticsMerge.java index 99830db..f6d2a4a 100644 --- a/src/main/java/com/tencent/msdk/dns/core/IStatisticsMerge.java +++ b/src/main/java/com/tencent/msdk/dns/core/IStatisticsMerge.java @@ -40,6 +40,11 @@ public boolean lookupFailed() { return false; } + @Override + public boolean lookupPartCached() { + return false; + } + @Override public String toJsonResult() { return "{\"v4_ips\":\"\",\"v4_ttl\":\"\",\"v4_client_ip\":\"\",\"v6_ips\":\"\",\"v6_ttl\":\"\",\"v6_client_ip\":\"\"}"; diff --git a/src/main/java/com/tencent/msdk/dns/core/LookupContext.java b/src/main/java/com/tencent/msdk/dns/core/LookupContext.java index bbc28eb..02f10bc 100644 --- a/src/main/java/com/tencent/msdk/dns/core/LookupContext.java +++ b/src/main/java/com/tencent/msdk/dns/core/LookupContext.java @@ -66,6 +66,10 @@ public String hostname() { return mLookupParams.hostname; } + public String requestHostname() { + return mLookupParams.requestHostname; + } + public int timeoutMills() { return mLookupParams.timeoutMills; } diff --git a/src/main/java/com/tencent/msdk/dns/core/LookupParameters.java b/src/main/java/com/tencent/msdk/dns/core/LookupParameters.java index 25f4044..a9b14b9 100644 --- a/src/main/java/com/tencent/msdk/dns/core/LookupParameters.java +++ b/src/main/java/com/tencent/msdk/dns/core/LookupParameters.java @@ -33,6 +33,8 @@ public final class LookupParameters { public final int curRetryTime; public final boolean netChangeLookup; + public String requestHostname; + private LookupParameters( Context appContext, @@ -81,6 +83,10 @@ public boolean equals(Object o) { CommonUtils.equals(channel, that.channel); } + public void setRequestHostname(String hostname) { + this.requestHostname = hostname; + } + @Override public int hashCode() { return CommonUtils.hash(appContext, hostname, timeoutMills, dnsIp, lookupExtra, channel, diff --git a/src/main/java/com/tencent/msdk/dns/core/local/LocalDns.java b/src/main/java/com/tencent/msdk/dns/core/local/LocalDns.java index 16cf861..0295926 100644 --- a/src/main/java/com/tencent/msdk/dns/core/local/LocalDns.java +++ b/src/main/java/com/tencent/msdk/dns/core/local/LocalDns.java @@ -129,5 +129,9 @@ public String toString() { '}'; } + @Override + public boolean lookupPartCached() { + return false; + } } } diff --git a/src/main/java/com/tencent/msdk/dns/core/rest/share/AbsHttpDns.java b/src/main/java/com/tencent/msdk/dns/core/rest/share/AbsHttpDns.java index 5717705..7506ed0 100644 --- a/src/main/java/com/tencent/msdk/dns/core/rest/share/AbsHttpDns.java +++ b/src/main/java/com/tencent/msdk/dns/core/rest/share/AbsHttpDns.java @@ -61,7 +61,7 @@ public LookupResult lookup(LookupParameters lookupParams) { throw new IllegalArgumentException("lookupParams".concat(Const.NULL_POINTER_TIPS)); } - String hostname = lookupParams.hostname; + String hostname = lookupParams.requestHostname; int timeoutMills = lookupParams.timeoutMills; String dnsIp = lookupParams.dnsIp; LookupExtra lookupExtra = lookupParams.lookupExtra; @@ -296,7 +296,7 @@ protected int connectInternal() { protected int requestInternal() { // non-block mode String dnsIp = mLookupContext.dnsIp(); - String hostname = mLookupContext.hostname(); + String hostname = mLookupContext.requestHostname(); LookupExtra lookupExtra = mLookupContext.lookupExtra(); String urlStr = getTargetUrl(dnsIp, hostname, lookupExtra); if (TextUtils.isEmpty(urlStr)) { diff --git a/src/main/java/com/tencent/msdk/dns/core/rest/share/AbsRestDns.java b/src/main/java/com/tencent/msdk/dns/core/rest/share/AbsRestDns.java index 56b213b..bac7969 100644 --- a/src/main/java/com/tencent/msdk/dns/core/rest/share/AbsRestDns.java +++ b/src/main/java/com/tencent/msdk/dns/core/rest/share/AbsRestDns.java @@ -44,20 +44,59 @@ protected boolean tryGetResultFromCache( if (lookupParams.enableAsyncLookup) { return false; } - String hostname = lookupParams.hostname; - LookupResult lookupResult = mCacheHelper.get(hostname); - String[] ips; - if (null != lookupResult && !CommonUtils.isEmpty(ips = lookupResult.ipSet.ips)) { - Statistics cachedStat = (Statistics) lookupResult.stat; - stat.errorCode = ErrorCode.SUCCESS; - stat.clientIp = cachedStat.clientIp; - stat.ttl = cachedStat.ttl; - stat.expiredTime = cachedStat.expiredTime; - stat.ips = ips; +// String hostname = lookupParams.hostname; + // todo 解耦批量域名 + final String[] hostnameArr = lookupParams.hostname.split(","); + List tempCachedips = new ArrayList<>(); + String[] ips = new String[0]; + String[] tempIps; + // 对批量域名返回值做处理 + boolean cached = true; + String requestHostname = ""; + if (hostnameArr.length > 1) { + for (String hostname : hostnameArr) { + LookupResult lookupResult = mCacheHelper.get(hostname); + if (null != lookupResult && !CommonUtils.isEmpty(tempIps = lookupResult.ipSet.ips)) { + for (String ip : tempIps) { + tempCachedips.add(hostname + ":" + ip); + } + } else { + cached = false; + requestHostname += hostname + ','; + } + } + requestHostname = requestHostname.length() > 0 ? requestHostname.substring(0, requestHostname.length() - 1) : ""; + if (tempCachedips.size() > 0) { + stat.ips = tempCachedips.toArray(new String[tempCachedips.size()]); + } + } else { + LookupResult lookupResult = mCacheHelper.get(hostnameArr[0]); + if (null != lookupResult && !CommonUtils.isEmpty(tempIps = lookupResult.ipSet.ips)) { + cached = true; + stat.ips = tempIps; + } else { + requestHostname = hostnameArr[0]; + } + } + + if (cached) { stat.cached = true; - DnsLog.d("Lookup for %s, cache hit", hostname); +// Statistics cachedStat = (Statistics) lookupResult.stat; + stat.errorCode = ErrorCode.SUCCESS; + // todo 命中缓存的clientIp和ttl如何处理 +// stat.clientIp = cachedStat.clientIp; +// stat.ttl = cachedStat.ttl; +// stat.expiredTime = cachedStat.expiredTime; +// stat.cached = true; + DnsLog.d("Lookup for %s, cache hit", lookupParams.hostname); return true; } + if (tempCachedips.size() > 0) { + stat.hadPartCachedIps = true; + lookupParams.setRequestHostname(requestHostname); +// stat.cachedIps = tempCachedips.toArray(new String[tempCachedips.size()]); + } + return false; } @@ -143,8 +182,7 @@ public final void request() { try { requestRes = requestInternal(); } finally { - if (requestRes != NonBlockResult.NON_BLOCK_RESULT_NEED_CONTINUE && - State.ENDED != mState) { + if (requestRes != NonBlockResult.NON_BLOCK_RESULT_NEED_CONTINUE && State.ENDED != mState) { mState = State.READABLE; } } @@ -325,6 +363,13 @@ public static class Statistics extends AbsStatistics implements Serializable { NOT_LOOKUP.errorCode = ErrorCode.NOT_LOOKUP; } + /** + * 批量解析部分缓存结果 + */ + public String[] cachedIps = Const.EMPTY_IPS; + + public boolean hadPartCachedIps = false; + /** * 域名解析错误码 */ @@ -384,6 +429,16 @@ public Statistics() { this.expiredTime = System.currentTimeMillis() + ttl * 1000; } + @Override + public boolean lookupPartCached() { + return hadPartCachedIps; + } + + @Override + public boolean lookupSuccess() { + return Const.EMPTY_IPS != ips && !lookupPartCached(); + } + @Override public String toString() { return "Statistics{" + diff --git a/src/main/java/com/tencent/msdk/dns/core/rest/share/CacheHelper.java b/src/main/java/com/tencent/msdk/dns/core/rest/share/CacheHelper.java index b8e39e3..b89d8dd 100644 --- a/src/main/java/com/tencent/msdk/dns/core/rest/share/CacheHelper.java +++ b/src/main/java/com/tencent/msdk/dns/core/rest/share/CacheHelper.java @@ -90,7 +90,7 @@ public void put(LookupParameters lookupParams, Response rsp) { return; } - final String[] hostnameArr = lookupParams.hostname.split(","); + final String[] hostnameArr = lookupParams.requestHostname.split(","); Map> ipsWithHostname = new HashMap<>(); if (hostnameArr.length > 1) { // 对批量域名返回值做处理 @@ -130,13 +130,6 @@ public void onResult(String hostname, String[] sortedIps) { } } - if (hostnameArr.length > 1) { - // todo:批量域名的存储逻辑仍先保留。批量域名解析查询缓存仍以整个hostname为索引。 - AbsRestDns.Statistics stat = new AbsRestDns.Statistics(rsp.ips, rsp.clientIp, rsp.ttl); - stat.errorCode = ErrorCode.SUCCESS; - mCache.add(lookupParams.hostname, new LookupResult<>(rsp.ips, stat)); - cacheUpdateTask(lookupParams, rsp, lookupParams.hostname); - } } private void cacheUpdateTask(LookupParameters lookupParams, Response rsp, final String hostname) { diff --git a/src/main/java/com/tencent/msdk/dns/core/stat/AbsStatistics.java b/src/main/java/com/tencent/msdk/dns/core/stat/AbsStatistics.java index 672a383..5dae723 100644 --- a/src/main/java/com/tencent/msdk/dns/core/stat/AbsStatistics.java +++ b/src/main/java/com/tencent/msdk/dns/core/stat/AbsStatistics.java @@ -12,6 +12,7 @@ public abstract class AbsStatistics implements IDns.IStatistics { * 域名解析结果 */ public String[] ips = Const.EMPTY_IPS; + /** * 域名解析结果 */ @@ -33,6 +34,10 @@ public void endLookup() { costTimeMills = (int) (System.currentTimeMillis() - startLookupTimeMills); } + public boolean lookupPartCached() { + return false; + } + @Override public boolean lookupSuccess() { return Const.EMPTY_IPS != ips; diff --git a/src/main/java/com/tencent/msdk/dns/core/stat/StatisticsMerge.java b/src/main/java/com/tencent/msdk/dns/core/stat/StatisticsMerge.java index 465345e..809f005 100644 --- a/src/main/java/com/tencent/msdk/dns/core/stat/StatisticsMerge.java +++ b/src/main/java/com/tencent/msdk/dns/core/stat/StatisticsMerge.java @@ -72,6 +72,8 @@ public final class StatisticsMerge implements IStatisticsMerge { private boolean hasBeenMerge = false; + private boolean hasPartCache = false; + public StatisticsMerge(Context context) { if (null == context) { throw new IllegalArgumentException("context".concat(Const.NULL_POINTER_TIPS)); @@ -97,6 +99,7 @@ public void merge(IDns dns, Statistics sta lookupSuccess = lookupSuccess && stat.lookupSuccess(); } lookupFailed = lookupFailed && stat.lookupFailed(); + hasPartCache = stat.lookupPartCached(); DnsDescription dnsDesc = dns.getDescription(); if (Const.LOCAL_CHANNEL.equals(dnsDesc.channel)) { localDnsStat = (LocalDns.Statistics) stat; @@ -140,6 +143,12 @@ public boolean lookupFailed() { return lookupFailed; } + @Override + public boolean lookupPartCached() { + return hasPartCache; + } + + @Override public String toJsonResult() { JSONObject jsonObject = new JSONObject(); From 9eb98e9824c8a85b74f8260441f78f94d7f6ef71 Mon Sep 17 00:00:00 2001 From: sarahzzhang <743855454@qq.com> Date: Thu, 20 Jul 2023 19:40:13 +0800 Subject: [PATCH 06/15] =?UTF-8?q?feat:=20=E6=89=B9=E9=87=8F=E8=A7=A3?= =?UTF-8?q?=E6=9E=90=E9=83=A8=E5=88=86=E5=91=BD=E4=B8=AD=E7=BC=93=E5=AD=98?= =?UTF-8?q?=E5=A4=84=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/tencent/msdk/dns/core/DnsManager.java | 21 +++++-------------- .../com/tencent/msdk/dns/core/ISorter.java | 2 ++ .../msdk/dns/core/LookupParameters.java | 2 +- .../msdk/dns/core/rest/share/AbsRestDns.java | 4 ++-- .../tencent/msdk/dns/core/sorter/Sorter.java | 16 ++++++++++++++ 5 files changed, 26 insertions(+), 19 deletions(-) diff --git a/src/main/java/com/tencent/msdk/dns/core/DnsManager.java b/src/main/java/com/tencent/msdk/dns/core/DnsManager.java index 410de67..83b3549 100644 --- a/src/main/java/com/tencent/msdk/dns/core/DnsManager.java +++ b/src/main/java/com/tencent/msdk/dns/core/DnsManager.java @@ -249,6 +249,11 @@ LookupResult lookup(LookupParameters lookupParams DnsLog.d("DnsManager lookup getResultFromCache success"); return lookupResultFromCache; } + + if (lookupResultFromCache.stat.lookupPartCached()) { + lookupContext.sorter().putPartCache(lookupResultFromCache.ipSet); + + } // 打开Selector prepareTasks(restDnsGroup, lookupContext); if (!lookupContext.allDnsLookedUp() && null != localDnsGroup) { @@ -295,19 +300,6 @@ LookupResult lookup(LookupParameters lookupParams } } IpSet ipSet = sorter.sort(); - // todo 阻塞 合并 - if(lookupResultFromCache.stat.lookupPartCached()) { - IpSet ipSet1 = lookupResultFromCache.ipSet; - int ipSetLen = ipSet.ips.length; - int ipSetLen1 = ipSet1.ips.length; - String[] str1 = Arrays.copyOf(ipSet.ips, ipSetLen + ipSetLen1); - System.arraycopy(ipSet1, 0, str1, ipSetLen, ipSetLen1); -// ipSet.ips = str1; -// IpSet(str1); - ipSet = new IpSet(str1); - } - - statMerge.statResult(ipSet); LookupResult lookupResult = new LookupResult(ipSet, statMerge); @@ -354,9 +346,6 @@ LookupResult lookup(LookupParameters lookupParams } catch (Exception ignored) { } IpSet ipSet = sorter.sort(); - if (lookupResultFromCache.stat.lookupPartCached()) { - // todo part cached - } statMerge.statResult(ipSet); LookupResult lookupResult = new LookupResult(ipSet, statMerge); diff --git a/src/main/java/com/tencent/msdk/dns/core/ISorter.java b/src/main/java/com/tencent/msdk/dns/core/ISorter.java index 7a779e5..455b422 100644 --- a/src/main/java/com/tencent/msdk/dns/core/ISorter.java +++ b/src/main/java/com/tencent/msdk/dns/core/ISorter.java @@ -10,5 +10,7 @@ interface IFactory { // NOTE: ips可能为空数组 void put(IDns dns, String[] ips); + void putPartCache(IpSet ipSet); + IpSet sort(); } diff --git a/src/main/java/com/tencent/msdk/dns/core/LookupParameters.java b/src/main/java/com/tencent/msdk/dns/core/LookupParameters.java index a9b14b9..a7e9c9f 100644 --- a/src/main/java/com/tencent/msdk/dns/core/LookupParameters.java +++ b/src/main/java/com/tencent/msdk/dns/core/LookupParameters.java @@ -35,7 +35,6 @@ public final class LookupParameters { public String requestHostname; - private LookupParameters( Context appContext, String hostname, int timeoutMills, String dnsIp, LookupExtra lookupExtra, @@ -56,6 +55,7 @@ private LookupParameters( this.enableAsyncLookup = enableAsyncLookup; this.curRetryTime = curRetryTime; this.netChangeLookup = netChangeLookup; + setRequestHostname(hostname); } @Override diff --git a/src/main/java/com/tencent/msdk/dns/core/rest/share/AbsRestDns.java b/src/main/java/com/tencent/msdk/dns/core/rest/share/AbsRestDns.java index bac7969..5e18be2 100644 --- a/src/main/java/com/tencent/msdk/dns/core/rest/share/AbsRestDns.java +++ b/src/main/java/com/tencent/msdk/dns/core/rest/share/AbsRestDns.java @@ -48,7 +48,6 @@ protected boolean tryGetResultFromCache( // todo 解耦批量域名 final String[] hostnameArr = lookupParams.hostname.split(","); List tempCachedips = new ArrayList<>(); - String[] ips = new String[0]; String[] tempIps; // 对批量域名返回值做处理 boolean cached = true; @@ -72,9 +71,9 @@ protected boolean tryGetResultFromCache( } else { LookupResult lookupResult = mCacheHelper.get(hostnameArr[0]); if (null != lookupResult && !CommonUtils.isEmpty(tempIps = lookupResult.ipSet.ips)) { - cached = true; stat.ips = tempIps; } else { + cached = false; requestHostname = hostnameArr[0]; } } @@ -91,6 +90,7 @@ protected boolean tryGetResultFromCache( DnsLog.d("Lookup for %s, cache hit", lookupParams.hostname); return true; } + if (tempCachedips.size() > 0) { stat.hadPartCachedIps = true; lookupParams.setRequestHostname(requestHostname); diff --git a/src/main/java/com/tencent/msdk/dns/core/sorter/Sorter.java b/src/main/java/com/tencent/msdk/dns/core/sorter/Sorter.java index a5ea6d7..f492449 100644 --- a/src/main/java/com/tencent/msdk/dns/core/sorter/Sorter.java +++ b/src/main/java/com/tencent/msdk/dns/core/sorter/Sorter.java @@ -24,6 +24,8 @@ public final class Sorter implements ISorter { private List mV6IpFromLocal = Collections.emptyList(); private List mV4IpsFromRest = Collections.emptyList(); private List mV6IpsFromRest = Collections.emptyList(); + private List mV4IpsFromCache = Collections.emptyList(); + private List mV6IpsFromCache = Collections.emptyList(); private Sorter(int curNetStack) { mCurNetStack = curNetStack; @@ -62,11 +64,24 @@ public synchronized void put(IDns dns, String[] ips) { } } + @Override + public synchronized void putPartCache(IpSet ipSet){ + String[] v4Ips = ipSet.v4Ips; + String[] v6Ips = ipSet.v6Ips; + if (v4Ips.length > 0) { + mV4IpsFromCache = Arrays.asList(v4Ips); + } + if (v6Ips.length > 0) { + mV6IpsFromCache = Arrays.asList(v6Ips); + } + } + @Override public IpSet sort() { List v4IpSet = new ArrayList<>(); if (0 != (mCurNetStack & NetworkStack.IPV4_ONLY)) { if (!mV4IpsFromRest.isEmpty()) { + v4IpSet.addAll(mV4IpsFromCache); v4IpSet.addAll(mV4IpsFromRest); } else if (null != mV4IpFromLocal) { v4IpSet.addAll(mV4IpFromLocal); @@ -75,6 +90,7 @@ public IpSet sort() { List v6IpSet = new ArrayList<>(); if (0 != (mCurNetStack & NetworkStack.IPV6_ONLY)) { if (!mV6IpsFromRest.isEmpty()) { + v6IpSet.addAll(mV6IpsFromCache); v6IpSet.addAll(mV6IpsFromRest); } else if (null != mV6IpFromLocal) { v6IpSet.addAll(mV6IpFromLocal); From 10a899eeecdfa7b690b48e4ad0a0e3e9f325ca1b Mon Sep 17 00:00:00 2001 From: lemondark <743855454@qq.com> Date: Wed, 26 Jul 2023 19:33:56 +0800 Subject: [PATCH 07/15] =?UTF-8?q?fix:=20=E4=BF=AE=E5=A4=8D=E9=83=A8?= =?UTF-8?q?=E5=88=86=E5=91=BD=E4=B8=AD=E7=BC=93=E5=AD=98=E7=9A=84=E8=BF=94?= =?UTF-8?q?=E5=9B=9E=E7=BB=93=E6=9E=9C=E5=8F=8A=E6=8B=BC=E6=8E=A5=E6=A0=BC?= =?UTF-8?q?=E5=BC=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/tencent/msdk/dns/core/DnsManager.java | 19 ++++++------ .../msdk/dns/core/rest/share/AbsHttpDns.java | 2 +- .../msdk/dns/core/rest/share/AbsRestDns.java | 31 ++++++++++++------- 3 files changed, 30 insertions(+), 22 deletions(-) diff --git a/src/main/java/com/tencent/msdk/dns/core/DnsManager.java b/src/main/java/com/tencent/msdk/dns/core/DnsManager.java index 83b3549..f7b56b5 100644 --- a/src/main/java/com/tencent/msdk/dns/core/DnsManager.java +++ b/src/main/java/com/tencent/msdk/dns/core/DnsManager.java @@ -235,25 +235,24 @@ LookupResult lookup(LookupParameters lookupParams // NOTE: sessions仅会被当前线程访问 List sessions = new ArrayList<>(); lookupContext.sessions(sessions); - LookupResult lookupResultFromCache = new LookupResult( - IpSet.EMPTY, new StatisticsMerge(lookupParams.appContext)); try { // NOTE: 当前对外API上, 不支持AAAA记录的解析, 需要保留LocalDns的解析结果作为AAAA解析结果 // 暂时不忽略LocalDns解析结果(即超时时间内会等待LocalDns解析结果, 无论RestDns是否已经解析成功) if (null != restDnsGroup) { // 先查缓存,有其一即可 - lookupResultFromCache = getResultFromCache(lookupParams); + LookupResult lookupResultFromCache = getResultFromCache(lookupParams); DnsLog.d("getResultFromCache: " + lookupResultFromCache); if (lookupResultFromCache.stat.lookupSuccess()) { - lookupResultHolder.mLookupResult = lookupResultFromCache; - DnsLog.d("DnsManager lookup getResultFromCache success"); - return lookupResultFromCache; + if (lookupResultFromCache.stat.lookupPartCached()) { + // 仅部分命中缓存 + lookupContext.sorter().putPartCache(lookupResultFromCache.ipSet); + } else { + lookupResultHolder.mLookupResult = lookupResultFromCache; + DnsLog.d("DnsManager lookup getResultFromCache success"); + return lookupResultFromCache; + } } - if (lookupResultFromCache.stat.lookupPartCached()) { - lookupContext.sorter().putPartCache(lookupResultFromCache.ipSet); - - } // 打开Selector prepareTasks(restDnsGroup, lookupContext); if (!lookupContext.allDnsLookedUp() && null != localDnsGroup) { diff --git a/src/main/java/com/tencent/msdk/dns/core/rest/share/AbsHttpDns.java b/src/main/java/com/tencent/msdk/dns/core/rest/share/AbsHttpDns.java index 7506ed0..91955b5 100644 --- a/src/main/java/com/tencent/msdk/dns/core/rest/share/AbsHttpDns.java +++ b/src/main/java/com/tencent/msdk/dns/core/rest/share/AbsHttpDns.java @@ -142,7 +142,7 @@ public LookupResult lookup(LookupParameters lookupParams) { stat.clientIp = rsp.clientIp; stat.ttl = rsp.ttl; stat.expiredTime = System.currentTimeMillis() + rsp.ttl * 1000; - stat.ips = rsp.ips; + stat.ips = ipTemplate(rsp.ips, lookupParams); } catch (Exception e) { DnsLog.d(e, getTag() + "lookup failed"); } finally { diff --git a/src/main/java/com/tencent/msdk/dns/core/rest/share/AbsRestDns.java b/src/main/java/com/tencent/msdk/dns/core/rest/share/AbsRestDns.java index 5e18be2..3456321 100644 --- a/src/main/java/com/tencent/msdk/dns/core/rest/share/AbsRestDns.java +++ b/src/main/java/com/tencent/msdk/dns/core/rest/share/AbsRestDns.java @@ -44,8 +44,7 @@ protected boolean tryGetResultFromCache( if (lookupParams.enableAsyncLookup) { return false; } -// String hostname = lookupParams.hostname; - // todo 解耦批量域名 + final String[] hostnameArr = lookupParams.hostname.split(","); List tempCachedips = new ArrayList<>(); String[] tempIps; @@ -80,13 +79,11 @@ protected boolean tryGetResultFromCache( if (cached) { stat.cached = true; -// Statistics cachedStat = (Statistics) lookupResult.stat; stat.errorCode = ErrorCode.SUCCESS; // todo 命中缓存的clientIp和ttl如何处理 // stat.clientIp = cachedStat.clientIp; // stat.ttl = cachedStat.ttl; // stat.expiredTime = cachedStat.expiredTime; -// stat.cached = true; DnsLog.d("Lookup for %s, cache hit", lookupParams.hostname); return true; } @@ -94,7 +91,6 @@ protected boolean tryGetResultFromCache( if (tempCachedips.size() > 0) { stat.hadPartCachedIps = true; lookupParams.setRequestHostname(requestHostname); -// stat.cachedIps = tempCachedips.toArray(new String[tempCachedips.size()]); } return false; @@ -113,6 +109,20 @@ public LookupResult getResultFromCache(LookupParameters lookupParam return new LookupResult<>(stat.ips, stat); } + public String[] ipTemplate(String[] ips, LookupParameters lookupParameters) { + String requestHostname = lookupParameters.requestHostname; + if (ips.length > 0 && !lookupParameters.requestHostname.equals(lookupParameters.hostname) && requestHostname.split(",").length == 1) { + // 批量解析中单个域名下发请求的格式处理 + List list = new ArrayList<>(); + for (String ip : ips) { + list.add(requestHostname + ":" + ip); + } + return list.toArray(new String[list.size()]); + } else { + return ips; + } + } + /** * @hide 负责亲子关系管理,token管理 * 子类负责请求响应的具体实现,channel(DatagramChannel/SocketChannel/...)的管理和session实例的创建 @@ -203,15 +213,16 @@ public final String[] receiveResponse() { if (tryGetResultFromCache(mLookupContext.asLookupParameters(), mStat)) { return mStat.ips; } + LookupParameters lookupParameters = mLookupContext.asLookupParameters(); rsp = responseInternal(); if (rsp != Response.EMPTY && rsp != Response.NEED_CONTINUE) { mStat.errorCode = ErrorCode.SUCCESS; - mCacheHelper.put(mLookupContext.asLookupParameters(), rsp); + mCacheHelper.put(lookupParameters, rsp); } mStat.clientIp = rsp.clientIp; mStat.ttl = rsp.ttl; mStat.expiredTime = System.currentTimeMillis() + rsp.ttl * 1000; - mStat.ips = rsp.ips; + mStat.ips = ipTemplate(rsp.ips, lookupParameters); } finally { if (rsp != Response.NEED_CONTINUE) { end(); @@ -364,10 +375,8 @@ public static class Statistics extends AbsStatistics implements Serializable { } /** - * 批量解析部分缓存结果 + * 是否仅部分命中缓存(使用于批量解析中) */ - public String[] cachedIps = Const.EMPTY_IPS; - public boolean hadPartCachedIps = false; /** @@ -436,7 +445,7 @@ public boolean lookupPartCached() { @Override public boolean lookupSuccess() { - return Const.EMPTY_IPS != ips && !lookupPartCached(); + return Const.EMPTY_IPS != ips; } @Override From 46313591f131fb4d261f84d67dae5df04fa50369 Mon Sep 17 00:00:00 2001 From: sarahzzhang <743855454@qq.com> Date: Wed, 26 Jul 2023 20:07:43 +0800 Subject: [PATCH 08/15] =?UTF-8?q?fix:=20=E4=BF=AE=E5=A4=8D=E9=83=A8?= =?UTF-8?q?=E5=88=86=E5=91=BD=E4=B8=AD=E7=BC=93=E5=AD=98=E4=B8=94=E5=AD=98?= =?UTF-8?q?=E5=9C=A8ipv6=E7=BC=93=E5=AD=98=E7=BB=93=E6=9E=9C=E6=97=B6?= =?UTF-8?q?=EF=BC=8C=E6=9C=80=E7=BB=88=E6=9C=AA=E8=BF=94=E5=9B=9Eipv6?= =?UTF-8?q?=E7=BB=93=E6=9E=9C=E7=9A=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/com/tencent/msdk/dns/core/local/LocalDns.java | 2 +- .../java/com/tencent/msdk/dns/core/sorter/Sorter.java | 10 +++++++--- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/src/main/java/com/tencent/msdk/dns/core/local/LocalDns.java b/src/main/java/com/tencent/msdk/dns/core/local/LocalDns.java index 0295926..381a2f9 100644 --- a/src/main/java/com/tencent/msdk/dns/core/local/LocalDns.java +++ b/src/main/java/com/tencent/msdk/dns/core/local/LocalDns.java @@ -42,7 +42,7 @@ public LookupResult lookup(LookupParameters lookupParams) { Statistics stat = new Statistics(); stat.startLookup(); - String[] ips = lookup(lookupParams.hostname); + String[] ips = lookup(lookupParams.requestHostname); stat.endLookup(); stat.ips = ips; diff --git a/src/main/java/com/tencent/msdk/dns/core/sorter/Sorter.java b/src/main/java/com/tencent/msdk/dns/core/sorter/Sorter.java index f492449..1f46f08 100644 --- a/src/main/java/com/tencent/msdk/dns/core/sorter/Sorter.java +++ b/src/main/java/com/tencent/msdk/dns/core/sorter/Sorter.java @@ -65,7 +65,7 @@ public synchronized void put(IDns dns, String[] ips) { } @Override - public synchronized void putPartCache(IpSet ipSet){ + public synchronized void putPartCache(IpSet ipSet) { String[] v4Ips = ipSet.v4Ips; String[] v6Ips = ipSet.v6Ips; if (v4Ips.length > 0) { @@ -80,8 +80,10 @@ public synchronized void putPartCache(IpSet ipSet){ public IpSet sort() { List v4IpSet = new ArrayList<>(); if (0 != (mCurNetStack & NetworkStack.IPV4_ONLY)) { - if (!mV4IpsFromRest.isEmpty()) { + if (!mV4IpsFromCache.isEmpty()) { v4IpSet.addAll(mV4IpsFromCache); + } + if (!mV4IpsFromRest.isEmpty()) { v4IpSet.addAll(mV4IpsFromRest); } else if (null != mV4IpFromLocal) { v4IpSet.addAll(mV4IpFromLocal); @@ -89,8 +91,10 @@ public IpSet sort() { } List v6IpSet = new ArrayList<>(); if (0 != (mCurNetStack & NetworkStack.IPV6_ONLY)) { - if (!mV6IpsFromRest.isEmpty()) { + if (!mV6IpsFromCache.isEmpty()) { v6IpSet.addAll(mV6IpsFromCache); + } + if (!mV6IpsFromRest.isEmpty()) { v6IpSet.addAll(mV6IpsFromRest); } else if (null != mV6IpFromLocal) { v6IpSet.addAll(mV6IpFromLocal); From 32a26bf1ec9b82a4246aa33fccef6d17b50ebb00 Mon Sep 17 00:00:00 2001 From: sarahzzhang <743855454@qq.com> Date: Thu, 27 Jul 2023 15:48:39 +0800 Subject: [PATCH 09/15] =?UTF-8?q?fix:=20=E8=B0=83=E6=95=B4=E6=89=93?= =?UTF-8?q?=E5=8D=B0=E5=8F=8A=E4=B8=8A=E6=8A=A5=E6=97=A5=E5=BF=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/tencent/msdk/dns/core/rest/share/AbsHttpDns.java | 4 ++-- .../com/tencent/msdk/dns/core/rest/share/AbsRestDns.java | 9 +++++---- .../com/tencent/msdk/dns/core/stat/StatisticsMerge.java | 6 ++++++ .../java/com/tencent/msdk/dns/report/ReportConst.java | 1 + .../java/com/tencent/msdk/dns/report/ReportHelper.java | 9 ++++++--- 5 files changed, 20 insertions(+), 9 deletions(-) diff --git a/src/main/java/com/tencent/msdk/dns/core/rest/share/AbsHttpDns.java b/src/main/java/com/tencent/msdk/dns/core/rest/share/AbsHttpDns.java index 91955b5..d5e0342 100644 --- a/src/main/java/com/tencent/msdk/dns/core/rest/share/AbsHttpDns.java +++ b/src/main/java/com/tencent/msdk/dns/core/rest/share/AbsHttpDns.java @@ -142,14 +142,14 @@ public LookupResult lookup(LookupParameters lookupParams) { stat.clientIp = rsp.clientIp; stat.ttl = rsp.ttl; stat.expiredTime = System.currentTimeMillis() + rsp.ttl * 1000; - stat.ips = ipTemplate(rsp.ips, lookupParams); + stat.ips = rsp.ips; } catch (Exception e) { DnsLog.d(e, getTag() + "lookup failed"); } finally { CommonUtils.closeQuietly(reader); stat.endLookup(); } - return new LookupResult<>(stat.ips, stat); + return new LookupResult<>(ipTemplate(stat.ips, lookupParams), stat); } @Override diff --git a/src/main/java/com/tencent/msdk/dns/core/rest/share/AbsRestDns.java b/src/main/java/com/tencent/msdk/dns/core/rest/share/AbsRestDns.java index 3456321..29a1a81 100644 --- a/src/main/java/com/tencent/msdk/dns/core/rest/share/AbsRestDns.java +++ b/src/main/java/com/tencent/msdk/dns/core/rest/share/AbsRestDns.java @@ -209,11 +209,12 @@ public final String[] receiveResponse() { } Response rsp = Response.EMPTY; + LookupParameters lookupParameters = mLookupContext.asLookupParameters(); try { - if (tryGetResultFromCache(mLookupContext.asLookupParameters(), mStat)) { + if (tryGetResultFromCache(lookupParameters, mStat)) { return mStat.ips; } - LookupParameters lookupParameters = mLookupContext.asLookupParameters(); + rsp = responseInternal(); if (rsp != Response.EMPTY && rsp != Response.NEED_CONTINUE) { mStat.errorCode = ErrorCode.SUCCESS; @@ -222,7 +223,7 @@ public final String[] receiveResponse() { mStat.clientIp = rsp.clientIp; mStat.ttl = rsp.ttl; mStat.expiredTime = System.currentTimeMillis() + rsp.ttl * 1000; - mStat.ips = ipTemplate(rsp.ips, lookupParameters); + mStat.ips = rsp.ips; } finally { if (rsp != Response.NEED_CONTINUE) { end(); @@ -230,7 +231,7 @@ public final String[] receiveResponse() { syncState(); } } - return mStat.ips; + return ipTemplate(mStat.ips, lookupParameters); } @Override diff --git a/src/main/java/com/tencent/msdk/dns/core/stat/StatisticsMerge.java b/src/main/java/com/tencent/msdk/dns/core/stat/StatisticsMerge.java index 809f005..ad0f1e0 100644 --- a/src/main/java/com/tencent/msdk/dns/core/stat/StatisticsMerge.java +++ b/src/main/java/com/tencent/msdk/dns/core/stat/StatisticsMerge.java @@ -31,6 +31,11 @@ public final class StatisticsMerge implements IStatisticsMerge { * 域名 */ public String hostname = Const.INVALID_HOSTNAME; + + /** + * 下发解析域名(批量解析) + */ + public String requestHostname = Const.INVALID_HOSTNAME; /** * 访问HTTPDNS服务使用的协议, UDP或者HTTP */ @@ -115,6 +120,7 @@ public void statContext(LookupContext lookupContext) { } hostname = lookupContext.hostname(); + requestHostname = lookupContext.requestHostname(); channel = lookupContext.channel(); curNetStack = lookupContext.currentNetworkStack(); } diff --git a/src/main/java/com/tencent/msdk/dns/report/ReportConst.java b/src/main/java/com/tencent/msdk/dns/report/ReportConst.java index e17011f..fcc8d00 100644 --- a/src/main/java/com/tencent/msdk/dns/report/ReportConst.java +++ b/src/main/java/com/tencent/msdk/dns/report/ReportConst.java @@ -22,6 +22,7 @@ interface ReportConst { String CHANNEL_KEY = "channel"; String HOSTNAME_KEY = "domain"; + String REQUEST_HOSTNAME_KEY = "request_domain"; String NETWORK_STACK_KEY = "net_stack"; String LOCAL_LOOKUP_IPS_KEY = "ldns_ip"; diff --git a/src/main/java/com/tencent/msdk/dns/report/ReportHelper.java b/src/main/java/com/tencent/msdk/dns/report/ReportHelper.java index a0adf51..d7f3202 100644 --- a/src/main/java/com/tencent/msdk/dns/report/ReportHelper.java +++ b/src/main/java/com/tencent/msdk/dns/report/ReportHelper.java @@ -121,6 +121,9 @@ public static void reportLookupMethodCalledEvent(LookupResult lookupResult) { lookupMethodCalledEventMap.put(ReportConst.CHANNEL_KEY, statMerge.channel); lookupMethodCalledEventMap.put(ReportConst.NETWORK_TYPE_KEY, statMerge.netType); lookupMethodCalledEventMap.put(ReportConst.HOSTNAME_KEY, statMerge.hostname); + if (statMerge.requestHostname != statMerge.hostname) { + lookupMethodCalledEventMap.put(ReportConst.REQUEST_HOSTNAME_KEY, statMerge.requestHostname); + } lookupMethodCalledEventMap.put(ReportConst.NETWORK_STACK_KEY, String.valueOf(statMerge.curNetStack)); @@ -197,7 +200,7 @@ private static void attaReportLookupEvent(String eventName, LookupResult lookupR if (statMerge.restDnsStat.errorCode == 2 || (Const.HTTPS_CHANNEL.equals(sDnsConfig.channel) && (statMerge.restDnsStat.errorCode == 1))) { // 解析失败,仅当达到最大失败次数满足切换IP时候上报 if (sDnsConfig.enableReport && backupInfo.getCanReport(backupInfo.getErrorCount() + 1)) { - DnsExecutors.MAIN.execute(AttaHelper.report(carrierCode, statMerge.netType, sDnsConfig.lookupExtra.bizId, sDnsConfig.appId, sDnsConfig.channel, eventName, System.currentTimeMillis(), dnsIp, statMerge.restDnsStat.costTimeMills, statMerge.hostname, reqType, sDnsConfig.timeoutMills, statMerge.restDnsStat.ttl, statMerge.restDnsStat.errorCode, statMerge.restDnsStat.statusCode, statMerge.restDnsStat.cached, 1, CommonUtils.toStringList(statMerge.localDnsStat.ips, ReportConst.IP_SPLITTER), CommonUtils.toStringList(statMerge.restDnsStat.ips, ReportConst.IP_SPLITTER))); + DnsExecutors.MAIN.execute(AttaHelper.report(carrierCode, statMerge.netType, sDnsConfig.lookupExtra.bizId, sDnsConfig.appId, sDnsConfig.channel, eventName, System.currentTimeMillis(), dnsIp, statMerge.restDnsStat.costTimeMills, statMerge.requestHostname, reqType, sDnsConfig.timeoutMills, statMerge.restDnsStat.ttl, statMerge.restDnsStat.errorCode, statMerge.restDnsStat.statusCode, statMerge.restDnsStat.cached, 1, CommonUtils.toStringList(statMerge.localDnsStat.ips, ReportConst.IP_SPLITTER), CommonUtils.toStringList(statMerge.restDnsStat.ips, ReportConst.IP_SPLITTER))); } // 报错记录+1 backupInfo.incrementErrorCount(); @@ -209,7 +212,7 @@ private static void attaReportLookupEvent(String eventName, LookupResult lookupR // 请求成功后将ErrorCount置为0 backupInfo.setErrorCount(0); if (sDnsConfig.enableReport) { - DnsExecutors.MAIN.execute(AttaHelper.report(carrierCode, statMerge.netType, sDnsConfig.lookupExtra.bizId, sDnsConfig.appId, sDnsConfig.channel, eventName, System.currentTimeMillis(), dnsIp, statMerge.restDnsStat.costTimeMills, statMerge.hostname, reqType, sDnsConfig.timeoutMills, statMerge.restDnsStat.ttl, statMerge.restDnsStat.errorCode, statMerge.restDnsStat.statusCode, statMerge.restDnsStat.cached, 1, CommonUtils.toStringList(statMerge.localDnsStat.ips, ReportConst.IP_SPLITTER), CommonUtils.toStringList(statMerge.restDnsStat.ips, ReportConst.IP_SPLITTER))); + DnsExecutors.MAIN.execute(AttaHelper.report(carrierCode, statMerge.netType, sDnsConfig.lookupExtra.bizId, sDnsConfig.appId, sDnsConfig.channel, eventName, System.currentTimeMillis(), dnsIp, statMerge.restDnsStat.costTimeMills, statMerge.requestHostname, reqType, sDnsConfig.timeoutMills, statMerge.restDnsStat.ttl, statMerge.restDnsStat.errorCode, statMerge.restDnsStat.statusCode, statMerge.restDnsStat.cached, 1, CommonUtils.toStringList(statMerge.localDnsStat.ips, ReportConst.IP_SPLITTER), CommonUtils.toStringList(statMerge.restDnsStat.ips, ReportConst.IP_SPLITTER))); } } } @@ -244,7 +247,7 @@ private static void addCommonConfigInfo(Map eventMap) { eventMap.put(ReportConst.SDK_VERSION_KEY, BuildConfig.VERSION_NAME); eventMap.put(ReportConst.APP_ID_KEY, sDnsConfig.appId); eventMap.put(ReportConst.BIZ_ID_KEY, sDnsConfig.lookupExtra.bizId); - eventMap.put(ReportConst.USER_ID_KEY, sDnsConfig.userId); +// eventMap.put(ReportConst.USER_ID_KEY, sDnsConfig.userId); } private static void report(String eventName, Map eventMap) { From a32446c93ee58fc95059a86d7c5a47c418b98f9c Mon Sep 17 00:00:00 2001 From: sarahzzhang <743855454@qq.com> Date: Fri, 28 Jul 2023 15:54:41 +0800 Subject: [PATCH 10/15] =?UTF-8?q?feat:=20=E4=B9=90=E8=A7=82DNS=E5=9C=BA?= =?UTF-8?q?=E6=99=AF=E4=B8=8B=EF=BC=8C=E6=89=B9=E9=87=8F=E8=A7=A3=E6=9E=90?= =?UTF-8?q?=E4=B8=AD=E7=BC=93=E5=AD=98=E8=BF=87=E6=9C=9F=E8=AF=B7=E6=B1=82?= =?UTF-8?q?=E8=A7=A3=E6=9E=90=E5=9F=9F=E5=90=8D=E5=A4=84=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/com/tencent/msdk/dns/DnsService.java | 3 +- .../msdk/dns/core/rest/share/AbsRestDns.java | 31 ++++++++++++++++--- .../msdk/dns/core/stat/StatisticsMerge.java | 1 + 3 files changed, 30 insertions(+), 5 deletions(-) diff --git a/src/main/java/com/tencent/msdk/dns/DnsService.java b/src/main/java/com/tencent/msdk/dns/DnsService.java index e3742eb..dc15568 100644 --- a/src/main/java/com/tencent/msdk/dns/DnsService.java +++ b/src/main/java/com/tencent/msdk/dns/DnsService.java @@ -334,6 +334,7 @@ public void run() { try { JSONObject temp = new JSONObject(result); long expiredTime = Long.parseLong(temp.get("expired_time").toString()); + final String requestDomain = temp.get("request_name").toString(); long current = System.currentTimeMillis(); if (expiredTime < current) { // 缓存过期,发起异步请求 @@ -341,7 +342,7 @@ public void run() { @Override public void run() { DnsLog.d("async look up send"); - getAddrsByName(domain, true, true); + getAddrsByName(requestDomain, true, true); } }); // 缓存过期且不允许使用过期缓存 diff --git a/src/main/java/com/tencent/msdk/dns/core/rest/share/AbsRestDns.java b/src/main/java/com/tencent/msdk/dns/core/rest/share/AbsRestDns.java index 29a1a81..dca1214 100644 --- a/src/main/java/com/tencent/msdk/dns/core/rest/share/AbsRestDns.java +++ b/src/main/java/com/tencent/msdk/dns/core/rest/share/AbsRestDns.java @@ -2,6 +2,7 @@ import android.text.TextUtils; +import com.tencent.msdk.dns.DnsService; import com.tencent.msdk.dns.base.log.DnsLog; import com.tencent.msdk.dns.base.utils.CommonUtils; import com.tencent.msdk.dns.core.Const; @@ -50,7 +51,13 @@ protected boolean tryGetResultFromCache( String[] tempIps; // 对批量域名返回值做处理 boolean cached = true; + // 未命中缓存的请求域名 String requestHostname = ""; + // 缓存过期需要请求的域名,用于乐观dns场景 + String expiredHostname = ""; + int ttl = 600; + long expiredTime = System.currentTimeMillis() + ttl * 1000; + String clientIp = ""; if (hostnameArr.length > 1) { for (String hostname : hostnameArr) { LookupResult lookupResult = mCacheHelper.get(hostname); @@ -58,12 +65,20 @@ protected boolean tryGetResultFromCache( for (String ip : tempIps) { tempCachedips.add(hostname + ":" + ip); } + Statistics cachedStat = (Statistics) lookupResult.stat; + ttl = Math.min(ttl, cachedStat.ttl); + expiredTime = Math.min(expiredTime, cachedStat.expiredTime); + clientIp = cachedStat.clientIp; + if (cachedStat.expiredTime < System.currentTimeMillis()) { + expiredHostname += hostname + ','; + } } else { cached = false; requestHostname += hostname + ','; } } requestHostname = requestHostname.length() > 0 ? requestHostname.substring(0, requestHostname.length() - 1) : ""; + expiredHostname = expiredHostname.length() > 0 ? expiredHostname.substring(0, expiredHostname.length() - 1) : ""; if (tempCachedips.size() > 0) { stat.ips = tempCachedips.toArray(new String[tempCachedips.size()]); } @@ -71,6 +86,11 @@ protected boolean tryGetResultFromCache( LookupResult lookupResult = mCacheHelper.get(hostnameArr[0]); if (null != lookupResult && !CommonUtils.isEmpty(tempIps = lookupResult.ipSet.ips)) { stat.ips = tempIps; + Statistics cachedStat = (Statistics) lookupResult.stat; + ttl = cachedStat.ttl; + expiredTime = cachedStat.expiredTime; + clientIp = cachedStat.clientIp; + expiredHostname = expiredTime < System.currentTimeMillis() ? hostnameArr[0] : ""; } else { cached = false; requestHostname = hostnameArr[0]; @@ -80,11 +100,14 @@ protected boolean tryGetResultFromCache( if (cached) { stat.cached = true; stat.errorCode = ErrorCode.SUCCESS; - // todo 命中缓存的clientIp和ttl如何处理 -// stat.clientIp = cachedStat.clientIp; -// stat.ttl = cachedStat.ttl; -// stat.expiredTime = cachedStat.expiredTime; + stat.clientIp = clientIp; + stat.ttl = ttl; + stat.expiredTime = expiredTime; DnsLog.d("Lookup for %s, cache hit", lookupParams.hostname); + // 乐观DNS默认全部命中缓存,请求host设置 + if (DnsService.getDnsConfig().useExpiredIpEnable && !expiredHostname.isEmpty()) { + lookupParams.setRequestHostname(expiredHostname); + } return true; } diff --git a/src/main/java/com/tencent/msdk/dns/core/stat/StatisticsMerge.java b/src/main/java/com/tencent/msdk/dns/core/stat/StatisticsMerge.java index ad0f1e0..28c4520 100644 --- a/src/main/java/com/tencent/msdk/dns/core/stat/StatisticsMerge.java +++ b/src/main/java/com/tencent/msdk/dns/core/stat/StatisticsMerge.java @@ -161,6 +161,7 @@ public String toJsonResult() { try { jsonObject.put("v4_ips", ipSet == null ? "" : CommonUtils.toStringList(ipSet.v4Ips, ",")); jsonObject.put("v6_ips", ipSet == null ? "" : CommonUtils.toStringList(ipSet.v6Ips, ",")); + jsonObject.put("request_name", requestHostname); jsonObject.put("ttl", String.valueOf(restDnsStat.ttl)); jsonObject.put("client_ip", String.valueOf(restDnsStat.clientIp)); jsonObject.put("expired_time", String.valueOf(restDnsStat.expiredTime)); From 59b1f0e2be3f3b143e30efbf7aceb8810708917d Mon Sep 17 00:00:00 2001 From: sarahzzhang <743855454@qq.com> Date: Tue, 1 Aug 2023 15:14:23 +0800 Subject: [PATCH 11/15] =?UTF-8?q?fix:=20=E4=B9=90=E8=A7=82DNS=E5=9C=BA?= =?UTF-8?q?=E6=99=AF=E4=B8=8B=EF=BC=8C=E5=BC=82=E6=AD=A5=E8=AF=B7=E6=B1=82?= =?UTF-8?q?=E8=A7=A3=E6=9E=90=E8=AF=B7=E6=B1=82=E5=9F=9F=E5=90=8D=E5=A4=84?= =?UTF-8?q?=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/tencent/msdk/dns/core/DnsManager.java | 2 +- .../msdk/dns/core/rest/share/AbsRestDns.java | 23 +++++++++---------- 2 files changed, 12 insertions(+), 13 deletions(-) diff --git a/src/main/java/com/tencent/msdk/dns/core/DnsManager.java b/src/main/java/com/tencent/msdk/dns/core/DnsManager.java index f7b56b5..6d47f3c 100644 --- a/src/main/java/com/tencent/msdk/dns/core/DnsManager.java +++ b/src/main/java/com/tencent/msdk/dns/core/DnsManager.java @@ -117,7 +117,6 @@ LookupResult getResultFromCache(LookupParameters sStatMergeFactory.create( (Class) lookupExtra.getClass(), lookupParams.appContext); lookupContext.statisticsMerge(statMerge); - statMerge.statContext(lookupContext); IDns dns; switch (currentNetworkStack) { @@ -133,6 +132,7 @@ LookupResult getResultFromCache(LookupParameters } LookupResult lookupResultFromCache = dns.getResultFromCache(lookupParams); + statMerge.statContext(lookupContext); if (lookupResultFromCache.stat.lookupSuccess() || lookupResultFromCache.stat.lookupPartCached()) { lookupContext.sorter().put(dns, lookupResultFromCache.ipSet.ips); lookupContext.statisticsMerge() diff --git a/src/main/java/com/tencent/msdk/dns/core/rest/share/AbsRestDns.java b/src/main/java/com/tencent/msdk/dns/core/rest/share/AbsRestDns.java index dca1214..89a6836 100644 --- a/src/main/java/com/tencent/msdk/dns/core/rest/share/AbsRestDns.java +++ b/src/main/java/com/tencent/msdk/dns/core/rest/share/AbsRestDns.java @@ -51,13 +51,12 @@ protected boolean tryGetResultFromCache( String[] tempIps; // 对批量域名返回值做处理 boolean cached = true; - // 未命中缓存的请求域名 + // 未命中缓存的请求域名&乐观DNS场景下,缓存过期需要请求的域名 String requestHostname = ""; - // 缓存过期需要请求的域名,用于乐观dns场景 - String expiredHostname = ""; int ttl = 600; long expiredTime = System.currentTimeMillis() + ttl * 1000; String clientIp = ""; + Boolean useExpiredIpEnable = DnsService.getDnsConfig().useExpiredIpEnable; if (hostnameArr.length > 1) { for (String hostname : hostnameArr) { LookupResult lookupResult = mCacheHelper.get(hostname); @@ -69,8 +68,8 @@ protected boolean tryGetResultFromCache( ttl = Math.min(ttl, cachedStat.ttl); expiredTime = Math.min(expiredTime, cachedStat.expiredTime); clientIp = cachedStat.clientIp; - if (cachedStat.expiredTime < System.currentTimeMillis()) { - expiredHostname += hostname + ','; + if (useExpiredIpEnable && cachedStat.expiredTime < System.currentTimeMillis()) { + requestHostname += hostname + ','; } } else { cached = false; @@ -78,7 +77,6 @@ protected boolean tryGetResultFromCache( } } requestHostname = requestHostname.length() > 0 ? requestHostname.substring(0, requestHostname.length() - 1) : ""; - expiredHostname = expiredHostname.length() > 0 ? expiredHostname.substring(0, expiredHostname.length() - 1) : ""; if (tempCachedips.size() > 0) { stat.ips = tempCachedips.toArray(new String[tempCachedips.size()]); } @@ -90,13 +88,19 @@ protected boolean tryGetResultFromCache( ttl = cachedStat.ttl; expiredTime = cachedStat.expiredTime; clientIp = cachedStat.clientIp; - expiredHostname = expiredTime < System.currentTimeMillis() ? hostnameArr[0] : ""; + if (useExpiredIpEnable && cachedStat.expiredTime < System.currentTimeMillis()) { + requestHostname = hostnameArr[0]; + } } else { cached = false; requestHostname = hostnameArr[0]; } } + if (!requestHostname.isEmpty()) { + lookupParams.setRequestHostname(requestHostname); + } + if (cached) { stat.cached = true; stat.errorCode = ErrorCode.SUCCESS; @@ -104,16 +108,11 @@ protected boolean tryGetResultFromCache( stat.ttl = ttl; stat.expiredTime = expiredTime; DnsLog.d("Lookup for %s, cache hit", lookupParams.hostname); - // 乐观DNS默认全部命中缓存,请求host设置 - if (DnsService.getDnsConfig().useExpiredIpEnable && !expiredHostname.isEmpty()) { - lookupParams.setRequestHostname(expiredHostname); - } return true; } if (tempCachedips.size() > 0) { stat.hadPartCachedIps = true; - lookupParams.setRequestHostname(requestHostname); } return false; From 1e3747a584c5d568e7faf800f70c700024b85f17 Mon Sep 17 00:00:00 2001 From: sarahzzhang <743855454@qq.com> Date: Wed, 2 Aug 2023 11:39:58 +0800 Subject: [PATCH 12/15] =?UTF-8?q?fix:=20=E9=83=A8=E5=88=86=E5=91=BD?= =?UTF-8?q?=E4=B8=AD=E7=BC=93=E5=AD=98=EF=BC=8C=E5=8D=95=E4=B8=AA=E5=9F=9F?= =?UTF-8?q?=E5=90=8D=E4=B8=8B=E5=8F=91=E8=A7=A3=E6=9E=90=E6=9C=8D=E5=8A=A1?= =?UTF-8?q?=E5=BC=82=E5=B8=B8=E6=97=B6=EF=BC=8Clocaldns=E5=85=9C=E5=BA=95?= =?UTF-8?q?=E8=BF=94=E5=9B=9E=E8=A7=A3=E6=9E=90=E6=A0=BC=E5=BC=8F=E9=94=99?= =?UTF-8?q?=E8=AF=AF=E5=A4=84=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../msdk/dns/base/utils/CommonUtils.java | 18 ++++++++++++++++++ .../tencent/msdk/dns/core/local/LocalDns.java | 3 ++- .../msdk/dns/core/rest/share/AbsHttpDns.java | 2 +- .../msdk/dns/core/rest/share/AbsRestDns.java | 16 +--------------- 4 files changed, 22 insertions(+), 17 deletions(-) diff --git a/src/main/java/com/tencent/msdk/dns/base/utils/CommonUtils.java b/src/main/java/com/tencent/msdk/dns/base/utils/CommonUtils.java index 5ed79dc..b0ef5dd 100644 --- a/src/main/java/com/tencent/msdk/dns/base/utils/CommonUtils.java +++ b/src/main/java/com/tencent/msdk/dns/base/utils/CommonUtils.java @@ -4,11 +4,15 @@ import com.tencent.msdk.dns.base.log.DnsLog; import com.tencent.msdk.dns.core.IpSet; +import com.tencent.msdk.dns.core.LookupParameters; +import com.tencent.msdk.dns.core.rest.share.LookupExtra; import java.io.Closeable; import java.io.IOException; +import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; +import java.util.List; import java.util.Map; public final class CommonUtils { @@ -97,4 +101,18 @@ public static String getIpfromSet(IpSet ipSet) { } return v4Ip + ";" + v6Ip; } + + public static String[] templateIps(String[] ips, LookupParameters lookupParameters) { + String requestHostname = lookupParameters.requestHostname; + if (ips.length > 0 && !lookupParameters.requestHostname.equals(lookupParameters.hostname) && requestHostname.split(",").length == 1) { + // 批量解析中单个域名下发请求的格式处理 + List list = new ArrayList<>(); + for (String ip : ips) { + list.add(requestHostname + ":" + ip); + } + return list.toArray(new String[list.size()]); + } else { + return ips; + } + } } diff --git a/src/main/java/com/tencent/msdk/dns/core/local/LocalDns.java b/src/main/java/com/tencent/msdk/dns/core/local/LocalDns.java index 381a2f9..ab9f366 100644 --- a/src/main/java/com/tencent/msdk/dns/core/local/LocalDns.java +++ b/src/main/java/com/tencent/msdk/dns/core/local/LocalDns.java @@ -4,6 +4,7 @@ import android.util.Log; import com.tencent.msdk.dns.base.log.DnsLog; +import com.tencent.msdk.dns.base.utils.CommonUtils; import com.tencent.msdk.dns.core.Const; import com.tencent.msdk.dns.core.DnsDescription; import com.tencent.msdk.dns.core.IDns; @@ -46,7 +47,7 @@ public LookupResult lookup(LookupParameters lookupParams) { stat.endLookup(); stat.ips = ips; - return new LookupResult<>(ips, stat); + return new LookupResult<>(CommonUtils.templateIps(stat.ips, lookupParams), stat); } @Override diff --git a/src/main/java/com/tencent/msdk/dns/core/rest/share/AbsHttpDns.java b/src/main/java/com/tencent/msdk/dns/core/rest/share/AbsHttpDns.java index d5e0342..14647be 100644 --- a/src/main/java/com/tencent/msdk/dns/core/rest/share/AbsHttpDns.java +++ b/src/main/java/com/tencent/msdk/dns/core/rest/share/AbsHttpDns.java @@ -149,7 +149,7 @@ public LookupResult lookup(LookupParameters lookupParams) { CommonUtils.closeQuietly(reader); stat.endLookup(); } - return new LookupResult<>(ipTemplate(stat.ips, lookupParams), stat); + return new LookupResult<>(CommonUtils.templateIps(stat.ips, lookupParams), stat); } @Override diff --git a/src/main/java/com/tencent/msdk/dns/core/rest/share/AbsRestDns.java b/src/main/java/com/tencent/msdk/dns/core/rest/share/AbsRestDns.java index 89a6836..ea65e24 100644 --- a/src/main/java/com/tencent/msdk/dns/core/rest/share/AbsRestDns.java +++ b/src/main/java/com/tencent/msdk/dns/core/rest/share/AbsRestDns.java @@ -131,20 +131,6 @@ public LookupResult getResultFromCache(LookupParameters lookupParam return new LookupResult<>(stat.ips, stat); } - public String[] ipTemplate(String[] ips, LookupParameters lookupParameters) { - String requestHostname = lookupParameters.requestHostname; - if (ips.length > 0 && !lookupParameters.requestHostname.equals(lookupParameters.hostname) && requestHostname.split(",").length == 1) { - // 批量解析中单个域名下发请求的格式处理 - List list = new ArrayList<>(); - for (String ip : ips) { - list.add(requestHostname + ":" + ip); - } - return list.toArray(new String[list.size()]); - } else { - return ips; - } - } - /** * @hide 负责亲子关系管理,token管理 * 子类负责请求响应的具体实现,channel(DatagramChannel/SocketChannel/...)的管理和session实例的创建 @@ -253,7 +239,7 @@ public final String[] receiveResponse() { syncState(); } } - return ipTemplate(mStat.ips, lookupParameters); + return CommonUtils.templateIps(mStat.ips, lookupParameters); } @Override From 39cab1d60994d222ad31a2d9510677ed5458bd93 Mon Sep 17 00:00:00 2001 From: sarahzzhang <743855454@qq.com> Date: Wed, 2 Aug 2023 17:01:06 +0800 Subject: [PATCH 13/15] =?UTF-8?q?refactor:=20=E4=BB=A3=E7=A0=81=E8=B4=A8?= =?UTF-8?q?=E9=87=8F=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/com/tencent/msdk/dns/DnsService.java | 12 +++++----- .../msdk/dns/base/utils/HttpHelper.java | 6 ++--- .../com/tencent/msdk/dns/core/DnsManager.java | 1 - .../msdk/dns/core/rest/share/AbsRestDns.java | 22 +++++++++---------- .../msdk/dns/core/rest/share/CacheHelper.java | 2 +- .../msdk/dns/core/stat/StatisticsMerge.java | 8 +++---- 6 files changed, 25 insertions(+), 26 deletions(-) diff --git a/src/main/java/com/tencent/msdk/dns/DnsService.java b/src/main/java/com/tencent/msdk/dns/DnsService.java index dc15568..61e653f 100644 --- a/src/main/java/com/tencent/msdk/dns/DnsService.java +++ b/src/main/java/com/tencent/msdk/dns/DnsService.java @@ -8,7 +8,6 @@ import com.tencent.msdk.dns.base.log.DnsLog; import com.tencent.msdk.dns.base.log.ILogNode; import com.tencent.msdk.dns.base.network.NetworkChangeManager; -import com.tencent.msdk.dns.base.report.BeaconReporterInitParameters; import com.tencent.msdk.dns.base.report.IReporter; import com.tencent.msdk.dns.base.report.ReportManager; import com.tencent.msdk.dns.base.utils.CommonUtils; @@ -92,11 +91,11 @@ public void run() { ActivityLifecycleDetector.install(appContext); // Room 本地数据读取 DnsExecutors.WORK.execute(new Runnable() { - @Override - public void run() { - Cache.readFromDb(); - } - }); + @Override + public void run() { + Cache.readFromDb(); + } + }); ReportHelper.init(config); DnsExecutors.sExecutorSupplier = sConfig.executorSupplier; setLookedUpListener(config.lookedUpListener); @@ -171,6 +170,7 @@ public static synchronized void setCachedIpEnable(boolean mCachedIpEnable) { /** * 设置是否上报,是否启用域名服务(获取底层配置) + * * @param mEnableReport * @param mEnableDomainServer */ diff --git a/src/main/java/com/tencent/msdk/dns/base/utils/HttpHelper.java b/src/main/java/com/tencent/msdk/dns/base/utils/HttpHelper.java index 1481713..dc938a6 100644 --- a/src/main/java/com/tencent/msdk/dns/base/utils/HttpHelper.java +++ b/src/main/java/com/tencent/msdk/dns/base/utils/HttpHelper.java @@ -31,9 +31,9 @@ public static String getRequest(String urlStr) { String host = url.getHost(); String file = url.getFile(); return GET_METHOD + ' ' + file + ' ' + HTTP_VERSION + CRLF + - "Connection: close" + CRLF + - HOST_HEADER + ": " + host + CRLF + - CRLF; + "Connection: close" + CRLF + + HOST_HEADER + ": " + host + CRLF + + CRLF; } catch (MalformedURLException e) { return ""; } diff --git a/src/main/java/com/tencent/msdk/dns/core/DnsManager.java b/src/main/java/com/tencent/msdk/dns/core/DnsManager.java index 6d47f3c..9b18f22 100644 --- a/src/main/java/com/tencent/msdk/dns/core/DnsManager.java +++ b/src/main/java/com/tencent/msdk/dns/core/DnsManager.java @@ -17,7 +17,6 @@ import java.io.IOException; import java.nio.channels.Selector; import java.util.ArrayList; -import java.util.Arrays; import java.util.Collections; import java.util.Iterator; import java.util.List; diff --git a/src/main/java/com/tencent/msdk/dns/core/rest/share/AbsRestDns.java b/src/main/java/com/tencent/msdk/dns/core/rest/share/AbsRestDns.java index ea65e24..a4f1469 100644 --- a/src/main/java/com/tencent/msdk/dns/core/rest/share/AbsRestDns.java +++ b/src/main/java/com/tencent/msdk/dns/core/rest/share/AbsRestDns.java @@ -52,11 +52,11 @@ protected boolean tryGetResultFromCache( // 对批量域名返回值做处理 boolean cached = true; // 未命中缓存的请求域名&乐观DNS场景下,缓存过期需要请求的域名 - String requestHostname = ""; + StringBuilder requestHostname = new StringBuilder(); int ttl = 600; long expiredTime = System.currentTimeMillis() + ttl * 1000; String clientIp = ""; - Boolean useExpiredIpEnable = DnsService.getDnsConfig().useExpiredIpEnable; + boolean useExpiredIpEnable = DnsService.getDnsConfig().useExpiredIpEnable; if (hostnameArr.length > 1) { for (String hostname : hostnameArr) { LookupResult lookupResult = mCacheHelper.get(hostname); @@ -69,14 +69,14 @@ protected boolean tryGetResultFromCache( expiredTime = Math.min(expiredTime, cachedStat.expiredTime); clientIp = cachedStat.clientIp; if (useExpiredIpEnable && cachedStat.expiredTime < System.currentTimeMillis()) { - requestHostname += hostname + ','; + requestHostname.append(hostname).append(','); } } else { cached = false; - requestHostname += hostname + ','; + requestHostname.append(hostname).append(','); } } - requestHostname = requestHostname.length() > 0 ? requestHostname.substring(0, requestHostname.length() - 1) : ""; + requestHostname = new StringBuilder(requestHostname.length() > 0 ? requestHostname.substring(0, requestHostname.length() - 1) : ""); if (tempCachedips.size() > 0) { stat.ips = tempCachedips.toArray(new String[tempCachedips.size()]); } @@ -89,16 +89,16 @@ protected boolean tryGetResultFromCache( expiredTime = cachedStat.expiredTime; clientIp = cachedStat.clientIp; if (useExpiredIpEnable && cachedStat.expiredTime < System.currentTimeMillis()) { - requestHostname = hostnameArr[0]; + requestHostname = new StringBuilder(hostnameArr[0]); } } else { cached = false; - requestHostname = hostnameArr[0]; + requestHostname = new StringBuilder(hostnameArr[0]); } } - if (!requestHostname.isEmpty()) { - lookupParams.setRequestHostname(requestHostname); + if (requestHostname.length() > 0) { + lookupParams.setRequestHostname(requestHostname.toString()); } if (cached) { @@ -230,7 +230,7 @@ public final String[] receiveResponse() { } mStat.clientIp = rsp.clientIp; mStat.ttl = rsp.ttl; - mStat.expiredTime = System.currentTimeMillis() + rsp.ttl * 1000; + mStat.expiredTime = System.currentTimeMillis() + rsp.ttl * 1000L; mStat.ips = rsp.ips; } finally { if (rsp != Response.NEED_CONTINUE) { @@ -444,7 +444,7 @@ public Statistics() { this.ips = ips; this.clientIp = clientIp; this.ttl = ttl; - this.expiredTime = System.currentTimeMillis() + ttl * 1000; + this.expiredTime = System.currentTimeMillis() + ttl * 1000L; } @Override diff --git a/src/main/java/com/tencent/msdk/dns/core/rest/share/CacheHelper.java b/src/main/java/com/tencent/msdk/dns/core/rest/share/CacheHelper.java index b89d8dd..d362c50 100644 --- a/src/main/java/com/tencent/msdk/dns/core/rest/share/CacheHelper.java +++ b/src/main/java/com/tencent/msdk/dns/core/rest/share/CacheHelper.java @@ -2,9 +2,9 @@ import android.text.TextUtils; +import com.tencent.msdk.dns.BackupResolver; import com.tencent.msdk.dns.BuildConfig; import com.tencent.msdk.dns.DnsService; -import com.tencent.msdk.dns.BackupResolver; import com.tencent.msdk.dns.base.compat.CollectionCompat; import com.tencent.msdk.dns.base.executor.DnsExecutors; import com.tencent.msdk.dns.base.log.DnsLog; diff --git a/src/main/java/com/tencent/msdk/dns/core/stat/StatisticsMerge.java b/src/main/java/com/tencent/msdk/dns/core/stat/StatisticsMerge.java index 28c4520..8f615f1 100644 --- a/src/main/java/com/tencent/msdk/dns/core/stat/StatisticsMerge.java +++ b/src/main/java/com/tencent/msdk/dns/core/stat/StatisticsMerge.java @@ -149,10 +149,10 @@ public boolean lookupFailed() { return lookupFailed; } - @Override - public boolean lookupPartCached() { - return hasPartCache; - } + @Override + public boolean lookupPartCached() { + return hasPartCache; + } @Override From 3579ad3e35234dbf382a00dd7e18c37261d574d3 Mon Sep 17 00:00:00 2001 From: lemondark <743855454@qq.com> Date: Mon, 21 Aug 2023 11:07:01 +0800 Subject: [PATCH 14/15] =?UTF-8?q?feat:=20=E5=85=BC=E5=AE=B9=E5=9B=BD?= =?UTF-8?q?=E9=99=85=E7=AB=99=EF=BC=8C=E9=BB=98=E8=AE=A4=E8=B6=85=E6=97=B6?= =?UTF-8?q?=E6=97=B6=E9=97=B4=E8=AE=BE=E7=BD=AE=E4=B8=BA2000ms?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 10 +++++----- src/main/java/com/tencent/msdk/dns/DnsConfig.java | 4 ++-- .../java/com/tencent/msdk/dns/MSDKDnsResolver.java | 2 +- 3 files changed, 8 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index 4d356b3..624ed77 100644 --- a/README.md +++ b/README.md @@ -83,8 +83,8 @@ DnsConfig dnsConfigBuilder = DnsConfig.Builder() .preLookupDomains("baidu.com", "qq.com") //(可选)手动指定网络栈支持情况,仅进行 IPv4 解析传 1,仅进行 IPv6 解析传 2,进行 IPv4、IPv6 双栈解析传 3。默认为根据客户端本地网络栈支持情况发起对应的解析请求。 .setCustomNetStack(3) - //(可选)设置域名解析请求超时时间,默认为1000ms - .timeoutMills(1000) + //(可选)设置域名解析请求超时时间,默认为2000ms + .timeoutMills(2000) //(可选)[V4.4.0废弃]是否开启解析异常上报,默认false,不上报 .enableReport(true) //(可选)[V4.1.0] 解析缓存自动刷新, 以域名形式进行配置,填写形式:"baidu.com", "qq.com"。配置的域名会在 TTL * 75% 时自动发起解析请求更新缓存,实现配置域名解析时始终命中缓存。此项建议不要设置太多域名,当前限制为最多 10 个域名。与预解析分开独立配置。 @@ -126,7 +126,7 @@ ii. 老版本初始化方法 * @param dnskey dns解析key,即授权id对应的 key(加密密钥),在申请 SDK 后的邮箱里,腾讯云官网(https://console.cloud.tencent.com/httpdns)申请获得,用于域名解析鉴权 * @param dnsIp 由外部传入的dnsIp,可选:"119.29.29.98",以腾讯云文档(https://cloud.tencent.com/document/product/379/54976)提供的 IP 为准 * @param debug 是否开启 debug 日志,true 为打开,false 为关闭,建议测试阶段打开,正式上线时关闭 - * @param timeout dns请求超时时间,单位ms,建议设置1000 + * @param timeout dns请求超时时间,单位ms,建议设置2000 */ MSDKDnsResolver.getInstance().init(MainActivity.this, appkey, dnsid, dnskey, dnsIp, debug, timeout); ``` @@ -144,7 +144,7 @@ MSDKDnsResolver.getInstance().init(MainActivity.this, appkey, dnsid, dnskey, dns * @param dnskey dns解析key,即授权id对应的 key(加密密钥),在申请 SDK 后的邮箱里,腾讯云官网(https://console.cloud.tencent.com/httpdns)申请获得,用于域名解析鉴权 * @param dnsIp 由外部传入的dnsIp,可选:"119.29.29.98"(仅支持 http 请求,channel为DesHttp和AesHttp时选择),"119.29.29.99"(仅支持 https 请求,channel为Https时选择)以腾讯云文档(https://cloud.tencent.com/document/product/379/54976)提供的 IP 为准 * @param debug 是否开启 debug 日志,true 为打开,false 为关闭,建议测试阶段打开,正式上线时关闭 - * @param timeout dns请求超时时间,单位ms,建议设置1000 + * @param timeout dns请求超时时间,单位ms,建议设置2000 * @param enableReport 是否开启解析异常上报,默认false,不上报 */ MSDKDnsResolver.getInstance().init(MainActivity.this, appkey, dnsid, dnskey, dnsIp, debug, timeout, enableReport); @@ -163,7 +163,7 @@ MSDKDnsResolver.getInstance().init(MainActivity.this, appkey, dnsid, dnskey, dns * @param dnskey dns解析key,即授权id对应的 key(加密密钥),在申请 SDK 后的邮箱里,腾讯云官网(https://console.cloud.tencent.com/httpdns)申请获得,用于域名解析鉴权 * @param dnsIp 由外部传入的dnsIp,可选:"119.29.29.98"(仅支持 http 请求,channel为DesHttp和AesHttp时选择),"119.29.29.99"(仅支持 https 请求,channel为Https时选择)以腾讯云文档(https://cloud.tencent.com/document/product/379/54976)提供的 IP 为准 * @param debug 是否开启 debug 日志,true 为打开,false 为关闭,建议测试阶段打开,正式上线时关闭 - * @param timeout dns请求超时时间,单位ms,建议设置1000 + * @param timeout dns请求超时时间,单位ms,建议设置2000 * @param channel 设置 channel,可选:DesHttp(默认), AesHttp, Https * @param token 腾讯云官网(https://console.cloud.tencent.com/httpdns)申请获得,用于 HTTPS 校验 * @param enableReport 是否开启解析异常上报,默认false,不上报 diff --git a/src/main/java/com/tencent/msdk/dns/DnsConfig.java b/src/main/java/com/tencent/msdk/dns/DnsConfig.java index ee6b402..af8625b 100644 --- a/src/main/java/com/tencent/msdk/dns/DnsConfig.java +++ b/src/main/java/com/tencent/msdk/dns/DnsConfig.java @@ -199,7 +199,7 @@ public static final class Builder { private String mDnsKey = ""; private String mToken = ""; - private int mTimeoutMills = 1000; + private int mTimeoutMills = 2000; private int mMaxNumOfPreLookupDomains = DEFAULT_MAX_NUM_OF_PRE_LOOKUP_DOMAINS; private int mMaxNumOfIpRankItems = DEFAULT_MAX_NUM_OF_IP_RANK_ITEMS; @@ -372,7 +372,7 @@ public Builder token(String token) { /** * 设置域名解析请求超时时间 - * 不设置时, 默认为1000ms + * 不设置时, 默认为2000ms * * @param timeoutMills 域名解析请求超时时间, 单位为ms * @return 当前Builder实例, 方便链式调用 diff --git a/src/main/java/com/tencent/msdk/dns/MSDKDnsResolver.java b/src/main/java/com/tencent/msdk/dns/MSDKDnsResolver.java index a97128f..46d0694 100644 --- a/src/main/java/com/tencent/msdk/dns/MSDKDnsResolver.java +++ b/src/main/java/com/tencent/msdk/dns/MSDKDnsResolver.java @@ -130,7 +130,7 @@ public void init(Context context, String appID, String dnsId, String dnsKey, Str * @param dnsKey dns解析key,即授权id对应的 key(加密密钥),在申请 SDK 后的邮箱里,腾讯云官网(https://console.cloud.tencent.com/httpdns)申请获得,用于域名解析鉴权 * @param dnsIp 由外部传入的dnsIp,可选:"119.29.29.98"(仅支持 http 请求),"119.29.29.99"(仅支持 https 请求)以腾讯云文档(https://cloud.tencent.com/document/product/379/54976)提供的 IP 为准 * @param debug 是否开启 debug 日志,true 为打开,false 为关闭,建议测试阶段打开,正式上线时关闭 - * @param timeout dns请求超时时间,单位ms,建议设置1000 + * @param timeout dns请求超时时间,单位ms,建议设置2000 * @param channel 设置 channel,可选:DesHttp(默认), AesHttp, Https * @param token 腾讯云官网(https://console.cloud.tencent.com/httpdns)申请获得,用于 HTTPS 校验 */ From d99219c5ecb42484b4c4ca39de6c45466e73d9c6 Mon Sep 17 00:00:00 2001 From: lemondark <743855454@qq.com> Date: Tue, 22 Aug 2023 16:46:15 +0800 Subject: [PATCH 15/15] =?UTF-8?q?fix:=20=E8=A7=A3=E5=86=B3=E4=BB=A3?= =?UTF-8?q?=E7=A0=81=E5=90=88=E5=B9=B6=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/com/tencent/msdk/dns/base/utils/HttpHelper.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/com/tencent/msdk/dns/base/utils/HttpHelper.java b/src/main/java/com/tencent/msdk/dns/base/utils/HttpHelper.java index dc938a6..675383d 100644 --- a/src/main/java/com/tencent/msdk/dns/base/utils/HttpHelper.java +++ b/src/main/java/com/tencent/msdk/dns/base/utils/HttpHelper.java @@ -31,7 +31,7 @@ public static String getRequest(String urlStr) { String host = url.getHost(); String file = url.getFile(); return GET_METHOD + ' ' + file + ' ' + HTTP_VERSION + CRLF + - "Connection: close" + CRLF + + "Connection: keep-alive" + CRLF + HOST_HEADER + ": " + host + CRLF + CRLF; } catch (MalformedURLException e) {