Skip to content

Commit

Permalink
reorder query region hosts (#257)
Browse files Browse the repository at this point in the history
  • Loading branch information
lihsai0 authored Jul 25, 2023
1 parent 897e658 commit 9e978b4
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 20 deletions.
47 changes: 33 additions & 14 deletions src/Qiniu/Storage/Config.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,15 @@ public class Config
/// </summary>
public static string DefaultUcHost = "uc.qbox.me";
/// <summary>
/// 默认备用空间管理域名
/// 默认查询区域域名
/// </summary>
public static List<string> DefaultBackupUcHosts = new List<string>
public static string DefaultQueryRegionHost = "kodo-config.qiniuapi.com";
/// <summary>
/// 默认备用查询区域域名
/// </summary>
public static List<string> DefaultBackupQueryRegionHosts = new List<string>
{
"kodo-config.qiniuapi.com",
"uc.qbox.me",
"api.qiniu.com"
};

Expand Down Expand Up @@ -68,12 +72,15 @@ public class Config

private string _ucHost = DefaultUcHost;

private List<string> _backupUcHosts = DefaultBackupUcHosts;
private string _queryRegionHost = DefaultQueryRegionHost;

private List<string> _backupQueryRegionHosts = DefaultBackupQueryRegionHosts;

public void SetUcHost(string val)
{
_ucHost = val;
_backupUcHosts.Clear();
_queryRegionHost = val;
_backupQueryRegionHosts.Clear();
}

public string UcHost()
Expand All @@ -82,14 +89,26 @@ public string UcHost()
return string.Format("{0}{1}", scheme, _ucHost);
}

public void SetBackupUcHost(List<string> val)
public void SetQueryRegionHost(string val)
{
_queryRegionHost = val;
_backupQueryRegionHosts.Clear();
}

public string QueryRegionHost()
{
string scheme = UseHttps ? "https://" : "http://";
return string.Format("{0}{1}", scheme, _queryRegionHost);
}

public void SetBackupQueryRegionHosts(List<string> val)
{
_backupUcHosts = val;
_backupQueryRegionHosts = val;
}

public List<string> BackupUcHost()
public List<string> BackupQueryRegionHosts()
{
return _backupUcHosts;
return _backupQueryRegionHosts;
}

/// <summary>
Expand All @@ -104,7 +123,7 @@ public string RsHost(string ak, string bucket)
Zone z = this.Zone;
if (z == null)
{
z = ZoneHelper.QueryZone(ak, bucket, UcHost(), BackupUcHost());
z = ZoneHelper.QueryZone(ak, bucket, QueryRegionHost(), BackupQueryRegionHosts());
}
return string.Format("{0}{1}", scheme, z.RsHost);
}
Expand All @@ -121,7 +140,7 @@ public string RsfHost(string ak, string bucket)
Zone z = this.Zone;
if (z == null)
{
z = ZoneHelper.QueryZone(ak, bucket, UcHost(), BackupUcHost());
z = ZoneHelper.QueryZone(ak, bucket, QueryRegionHost(), BackupQueryRegionHosts());
}
return string.Format("{0}{1}", scheme, z.RsfHost);
}
Expand All @@ -138,7 +157,7 @@ public string ApiHost(string ak, string bucket)
Zone z = this.Zone;
if (z == null)
{
z = ZoneHelper.QueryZone(ak, bucket, UcHost(), BackupUcHost());
z = ZoneHelper.QueryZone(ak, bucket, QueryRegionHost(), BackupQueryRegionHosts());
}
return string.Format("{0}{1}", scheme, z.ApiHost);
}
Expand All @@ -155,7 +174,7 @@ public string IovipHost(string ak, string bucket)
Zone z = this.Zone;
if (z == null)
{
z = ZoneHelper.QueryZone(ak, bucket, UcHost(), BackupUcHost());
z = ZoneHelper.QueryZone(ak, bucket, QueryRegionHost(), BackupQueryRegionHosts());
}
return string.Format("{0}{1}", scheme, z.IovipHost);
}
Expand All @@ -172,7 +191,7 @@ public string UpHost(string ak, string bucket)
Zone z = this.Zone;
if (z == null)
{
z = ZoneHelper.QueryZone(ak, bucket, UcHost(), BackupUcHost());
z = ZoneHelper.QueryZone(ak, bucket, QueryRegionHost(), BackupQueryRegionHosts());
}
string upHost = z.SrcUpHosts[0];
if (this.UseCdnDomains)
Expand Down
7 changes: 4 additions & 3 deletions src/Qiniu/Storage/ZoneHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ public class ZoneHelper
/// </summary>
/// <param name="accessKey">AccessKey</param>
/// <param name="bucket">空间名称</param>
/// <param name="ucHost">UC 域名</param>
/// <param name="ucHost">查询域名</param>
/// <param name="backupUcHosts">备用查询域名</param>
public static Zone QueryZone(
string accessKey,
string bucket,
Expand Down Expand Up @@ -59,12 +60,12 @@ public static Zone QueryZone(
HttpResult hr = null;
if (String.IsNullOrEmpty(ucHost))
{
ucHost = "https://" + Config.DefaultUcHost;
ucHost = "https://" + Config.DefaultQueryRegionHost;
}

if (backupUcHosts == null)
{
backupUcHosts = Config.DefaultBackupUcHosts;
backupUcHosts = Config.DefaultBackupQueryRegionHosts;
}

try
Expand Down
43 changes: 40 additions & 3 deletions src/QiniuTests/Storage/ZoneHelperTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,50 @@ public void QueryZoneTest()

Assert.NotNull(zone);
}


[Test]
public void QueryZoneWithCustomQueryRegionHost()
{
Config config = new Config();
config.SetQueryRegionHost("uc.qbox.me");
config.UseHttps = true;

Zone zone = ZoneHelper.QueryZone(
AccessKey,
Bucket,
config.UcHost()
);
Assert.NotNull(zone);
}

[Test]
public void QueryZoneWithBackupHostsTest()
{
Config config = new Config();
config.SetQueryRegionHost("fake-uc.csharp.qiniu.com");
config.SetBackupQueryRegionHosts(new List<string>
{
"unavailable-uc.csharp.qiniu.com",
"uc.qbox.me"
}
);
config.UseHttps = true;

Zone zone = ZoneHelper.QueryZone(
AccessKey,
Bucket,
config.UcHost(),
config.BackupQueryRegionHosts()
);
Assert.NotNull(zone);
}

[Test]
public void QueryZoneWithUcAndBackupHostsTest()
{
Config config = new Config();
config.SetUcHost("fake-uc.csharp.qiniu.com");
config.SetBackupUcHost(new List<string>
config.SetBackupQueryRegionHosts(new List<string>
{
"unavailable-uc.csharp.qiniu.com",
"uc.qbox.me"
Expand All @@ -32,7 +69,7 @@ public void QueryZoneWithBackupHostsTest()
AccessKey,
Bucket,
config.UcHost(),
config.BackupUcHost()
config.BackupQueryRegionHosts()
);
Assert.NotNull(zone);
}
Expand Down

0 comments on commit 9e978b4

Please sign in to comment.