Skip to content

Commit

Permalink
Merge pull request #22 from lemondark/feature/getAddrEnableExpired
Browse files Browse the repository at this point in the history
feature: 提高缓存命中率
  • Loading branch information
ilovetochangetheworld authored Sep 13, 2022
2 parents cb7d33f + b32c2b1 commit 4946870
Show file tree
Hide file tree
Showing 23 changed files with 641 additions and 364 deletions.
3 changes: 2 additions & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,8 @@ dependencies {
androidTestImplementation('com.android.support.test.espresso:espresso-core:3.0.2', {
exclude group: 'com.android.support', module: 'support-annotations'
})

implementation 'android.arch.persistence.room:runtime:2.1.1'
annotationProcessor 'android.arch.persistence.room:compiler:2.1.1'

doclava 'com.google.doclava:doclava:1.0.6'
}
Expand Down
4 changes: 2 additions & 2 deletions src/main/cpp/stack/NetworkStack.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,11 @@ int NetworkStack::get() {
int curNetworkStackType = NetworkStack::NONE;

if (isSupportIPv4()) {
DEBUG("support IPv4");
// DEBUG("support IPv4");
curNetworkStackType |= NetworkStack::IPV4_ONLY;
}
if (isSupportIPv6()) {
DEBUG("support IPv6");
// DEBUG("support IPv6");
curNetworkStackType |= NetworkStack::IPV6_ONLY;
}

Expand Down
25 changes: 23 additions & 2 deletions src/main/java/com/tencent/msdk/dns/DnsConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,10 @@ public final class DnsConfig {
*/
/* @Nullable */ public final List<IReporter> reporters;

public boolean useExpiredIpEnable = false;

public boolean cachedIpEnable = false;

private DnsConfig(int logLevel,
String appId, String userId, boolean initBuiltInReporters,
String dnsIp, String dnsId, String dnsKey, String token,
Expand All @@ -65,7 +69,7 @@ private DnsConfig(int logLevel,
Set<IpRankItem> ipRankItems, String channel, boolean enableReport, boolean blockFirst,
int customNetStack, DnsExecutors.ExecutorSupplier executorSupplier,
ILookedUpListener lookedUpListener, List<ILogNode> logNodes,
List<IReporter> reporters) {
List<IReporter> reporters, boolean useExpiredIpEnable, boolean cachedIpEnable) {
this.logLevel = logLevel;
this.appId = appId;
this.userId = userId;
Expand All @@ -86,6 +90,8 @@ private DnsConfig(int logLevel,
this.lookedUpListener = lookedUpListener;
this.logNodes = logNodes;
this.reporters = reporters;
this.useExpiredIpEnable = useExpiredIpEnable;
this.cachedIpEnable = cachedIpEnable;
}

boolean needProtect(/* @Nullable */String hostname) {
Expand Down Expand Up @@ -125,6 +131,8 @@ public String toString() {
", lookedUpListener=" + lookedUpListener +
", logNodes=" + CommonUtils.toString(logNodes) +
", reporters=" + CommonUtils.toString(reporters) +
", useExpiredIpEnable=" + useExpiredIpEnable +
", cachedIpEnable=" + cachedIpEnable +
'}';
}

Expand Down Expand Up @@ -210,6 +218,8 @@ public static final class Builder {
private List<ILogNode> mLogNodes = null;

private List<IReporter> mReporters = null;
private boolean mUseExpiredIpEnable = false;
private boolean mCachedIpEnable = false;

/**
* 设置最低日志等级, 低于设置等级的日志不会输出
Expand Down Expand Up @@ -652,6 +662,17 @@ public Builder setCustomNetStack(int customNetStack) {
return this;
}

public Builder setUseExpiredIpEnable(boolean useExpiredIpEnable) {
mUseExpiredIpEnable = useExpiredIpEnable;

return this;
}

public Builder setCachedIpEnable(boolean cachedIpEnable) {
mCachedIpEnable = cachedIpEnable;
return this;
}

/**
* 构建DnsConfig实例
*
Expand All @@ -665,7 +686,7 @@ public DnsConfig build() {
mIpRankItems, mChannel, mEnableReport, mBlockFirst,
mCustomNetStack, mExecutorSupplier,
mLookedUpListener, mLogNodes,
mReporters);
mReporters, mUseExpiredIpEnable, mCachedIpEnable);
}
}
}
50 changes: 47 additions & 3 deletions src/main/java/com/tencent/msdk/dns/DnsService.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,13 @@
import com.tencent.msdk.dns.core.IpSet;
import com.tencent.msdk.dns.core.LookupParameters;
import com.tencent.msdk.dns.core.LookupResult;
import com.tencent.msdk.dns.core.cache.Cache;
import com.tencent.msdk.dns.core.cache.database.LookupCacheDatabase;
import com.tencent.msdk.dns.core.rest.share.LookupExtra;
import com.tencent.msdk.dns.core.stat.StatisticsMerge;
import com.tencent.msdk.dns.report.ReportHelper;
import com.tencent.msdk.dns.report.SpendReportResolver;

import java.util.Collections;
import java.util.List;
import java.util.Set;
import java.util.concurrent.CountDownLatch;
Expand All @@ -47,6 +48,10 @@ public static DnsConfig getDnsConfig() {
return sConfig;
}

public static Context getAppContext() {
return sAppContext;
}

/**
* 初始化SDK
*
Expand Down Expand Up @@ -76,6 +81,15 @@ public static void init(Context context, /* @Nullable */DnsConfig config) {
SpendReportResolver.getInstance().init();
NetworkChangeManager.install(appContext);
ActivityLifecycleDetector.install(appContext);
// Room 本地数据读取
if (config.cachedIpEnable == true) {
DnsExecutors.WORK.execute(new Runnable() {
@Override
public void run() {
Cache.readFromDb();
}
});
}
// NOTE: 当前版本暂时不会提供为OneSdk版本, 默认使用灯塔上报
ReportManager.init(ReportManager.Channel.BEACON);
if (config.initBuiltInReporters) {
Expand Down Expand Up @@ -115,8 +129,9 @@ public static synchronized void setUserId(String userId) {

/**
* 启停缓存自动刷新功能
*
* @param mEnablePersistentCache false:关闭,true:开启
* @throws IllegalStateException 没有初始化时抛出
* @throws IllegalStateException 没有初始化时抛出
*/
public static synchronized void enablePersistentCache(boolean mEnablePersistentCache) {
if (!sInited) {
Expand All @@ -125,6 +140,35 @@ public static synchronized void enablePersistentCache(boolean mEnablePersistentC
sConfig.enablePersistentCache = mEnablePersistentCache;
}

/**
* 设置是否使用过期缓存IP(乐观DNS)
*
* @param mUseExpiredIpEnable false:不使用过期(默认),true:使用过期缓存
* @throws IllegalStateException 没有初始化时抛出
*/
public static synchronized void setUseExpiredIpEnable(boolean mUseExpiredIpEnable) {
if (!sInited) {
throw new IllegalStateException("DnsService".concat(Const.NOT_INIT_TIPS));
}
sConfig.useExpiredIpEnable = mUseExpiredIpEnable;
}

/**
* 设置是否使用本地缓存
*
* @param mCachedIpEnable false:不使用过期(默认),true:使用过期缓存
* @throws IllegalStateException 没有初始化时抛出
*/
public static synchronized void setCachedIpEnable(boolean mCachedIpEnable) {
if (!sInited) {
throw new IllegalStateException("DnsService".concat(Const.NOT_INIT_TIPS));
}
sConfig.cachedIpEnable = mCachedIpEnable;
if (mCachedIpEnable == true) {
LookupCacheDatabase.creat(sAppContext);
}
}

public static String getDnsDetail(String hostname) {
String dnsIp = BackupResolver.getInstance().getDnsIp();
LookupResult<IStatisticsMerge> lookupResult = DnsManager.getResultFromCache(new LookupParameters.Builder<LookupExtra>()
Expand Down Expand Up @@ -181,7 +225,7 @@ public static IpSet getAddrsByName(
* @return {@link IpSet}实例, 即解析得到的Ip集合
* @throws IllegalStateException 没有初始化时抛出
*/
private static IpSet getAddrsByName(
public static IpSet getAddrsByName(
/* @Nullable */String hostname, boolean fallback2Local, boolean enableAsyncLookup) {
return getAddrsByName(hostname, sConfig.channel, fallback2Local, enableAsyncLookup);
}
Expand Down
Loading

0 comments on commit 4946870

Please sign in to comment.