Skip to content

Commit 9e978b4

Browse files
authored
reorder query region hosts (#257)
1 parent 897e658 commit 9e978b4

File tree

3 files changed

+77
-20
lines changed

3 files changed

+77
-20
lines changed

src/Qiniu/Storage/Config.cs

Lines changed: 33 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,15 @@ public class Config
1616
/// </summary>
1717
public static string DefaultUcHost = "uc.qbox.me";
1818
/// <summary>
19-
/// 默认备用空间管理域名
19+
/// 默认查询区域域名
2020
/// </summary>
21-
public static List<string> DefaultBackupUcHosts = new List<string>
21+
public static string DefaultQueryRegionHost = "kodo-config.qiniuapi.com";
22+
/// <summary>
23+
/// 默认备用查询区域域名
24+
/// </summary>
25+
public static List<string> DefaultBackupQueryRegionHosts = new List<string>
2226
{
23-
"kodo-config.qiniuapi.com",
27+
"uc.qbox.me",
2428
"api.qiniu.com"
2529
};
2630

@@ -68,12 +72,15 @@ public class Config
6872

6973
private string _ucHost = DefaultUcHost;
7074

71-
private List<string> _backupUcHosts = DefaultBackupUcHosts;
75+
private string _queryRegionHost = DefaultQueryRegionHost;
76+
77+
private List<string> _backupQueryRegionHosts = DefaultBackupQueryRegionHosts;
7278

7379
public void SetUcHost(string val)
7480
{
7581
_ucHost = val;
76-
_backupUcHosts.Clear();
82+
_queryRegionHost = val;
83+
_backupQueryRegionHosts.Clear();
7784
}
7885

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

85-
public void SetBackupUcHost(List<string> val)
92+
public void SetQueryRegionHost(string val)
93+
{
94+
_queryRegionHost = val;
95+
_backupQueryRegionHosts.Clear();
96+
}
97+
98+
public string QueryRegionHost()
99+
{
100+
string scheme = UseHttps ? "https://" : "http://";
101+
return string.Format("{0}{1}", scheme, _queryRegionHost);
102+
}
103+
104+
public void SetBackupQueryRegionHosts(List<string> val)
86105
{
87-
_backupUcHosts = val;
106+
_backupQueryRegionHosts = val;
88107
}
89108

90-
public List<string> BackupUcHost()
109+
public List<string> BackupQueryRegionHosts()
91110
{
92-
return _backupUcHosts;
111+
return _backupQueryRegionHosts;
93112
}
94113

95114
/// <summary>
@@ -104,7 +123,7 @@ public string RsHost(string ak, string bucket)
104123
Zone z = this.Zone;
105124
if (z == null)
106125
{
107-
z = ZoneHelper.QueryZone(ak, bucket, UcHost(), BackupUcHost());
126+
z = ZoneHelper.QueryZone(ak, bucket, QueryRegionHost(), BackupQueryRegionHosts());
108127
}
109128
return string.Format("{0}{1}", scheme, z.RsHost);
110129
}
@@ -121,7 +140,7 @@ public string RsfHost(string ak, string bucket)
121140
Zone z = this.Zone;
122141
if (z == null)
123142
{
124-
z = ZoneHelper.QueryZone(ak, bucket, UcHost(), BackupUcHost());
143+
z = ZoneHelper.QueryZone(ak, bucket, QueryRegionHost(), BackupQueryRegionHosts());
125144
}
126145
return string.Format("{0}{1}", scheme, z.RsfHost);
127146
}
@@ -138,7 +157,7 @@ public string ApiHost(string ak, string bucket)
138157
Zone z = this.Zone;
139158
if (z == null)
140159
{
141-
z = ZoneHelper.QueryZone(ak, bucket, UcHost(), BackupUcHost());
160+
z = ZoneHelper.QueryZone(ak, bucket, QueryRegionHost(), BackupQueryRegionHosts());
142161
}
143162
return string.Format("{0}{1}", scheme, z.ApiHost);
144163
}
@@ -155,7 +174,7 @@ public string IovipHost(string ak, string bucket)
155174
Zone z = this.Zone;
156175
if (z == null)
157176
{
158-
z = ZoneHelper.QueryZone(ak, bucket, UcHost(), BackupUcHost());
177+
z = ZoneHelper.QueryZone(ak, bucket, QueryRegionHost(), BackupQueryRegionHosts());
159178
}
160179
return string.Format("{0}{1}", scheme, z.IovipHost);
161180
}
@@ -172,7 +191,7 @@ public string UpHost(string ak, string bucket)
172191
Zone z = this.Zone;
173192
if (z == null)
174193
{
175-
z = ZoneHelper.QueryZone(ak, bucket, UcHost(), BackupUcHost());
194+
z = ZoneHelper.QueryZone(ak, bucket, QueryRegionHost(), BackupQueryRegionHosts());
176195
}
177196
string upHost = z.SrcUpHosts[0];
178197
if (this.UseCdnDomains)

src/Qiniu/Storage/ZoneHelper.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@ public class ZoneHelper
2525
/// </summary>
2626
/// <param name="accessKey">AccessKey</param>
2727
/// <param name="bucket">空间名称</param>
28-
/// <param name="ucHost">UC 域名</param>
28+
/// <param name="ucHost">查询域名</param>
29+
/// <param name="backupUcHosts">备用查询域名</param>
2930
public static Zone QueryZone(
3031
string accessKey,
3132
string bucket,
@@ -59,12 +60,12 @@ public static Zone QueryZone(
5960
HttpResult hr = null;
6061
if (String.IsNullOrEmpty(ucHost))
6162
{
62-
ucHost = "https://" + Config.DefaultUcHost;
63+
ucHost = "https://" + Config.DefaultQueryRegionHost;
6364
}
6465

6566
if (backupUcHosts == null)
6667
{
67-
backupUcHosts = Config.DefaultBackupUcHosts;
68+
backupUcHosts = Config.DefaultBackupQueryRegionHosts;
6869
}
6970

7071
try

src/QiniuTests/Storage/ZoneHelperTests.cs

Lines changed: 40 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,50 @@ public void QueryZoneTest()
1414

1515
Assert.NotNull(zone);
1616
}
17-
17+
18+
[Test]
19+
public void QueryZoneWithCustomQueryRegionHost()
20+
{
21+
Config config = new Config();
22+
config.SetQueryRegionHost("uc.qbox.me");
23+
config.UseHttps = true;
24+
25+
Zone zone = ZoneHelper.QueryZone(
26+
AccessKey,
27+
Bucket,
28+
config.UcHost()
29+
);
30+
Assert.NotNull(zone);
31+
}
32+
1833
[Test]
1934
public void QueryZoneWithBackupHostsTest()
35+
{
36+
Config config = new Config();
37+
config.SetQueryRegionHost("fake-uc.csharp.qiniu.com");
38+
config.SetBackupQueryRegionHosts(new List<string>
39+
{
40+
"unavailable-uc.csharp.qiniu.com",
41+
"uc.qbox.me"
42+
}
43+
);
44+
config.UseHttps = true;
45+
46+
Zone zone = ZoneHelper.QueryZone(
47+
AccessKey,
48+
Bucket,
49+
config.UcHost(),
50+
config.BackupQueryRegionHosts()
51+
);
52+
Assert.NotNull(zone);
53+
}
54+
55+
[Test]
56+
public void QueryZoneWithUcAndBackupHostsTest()
2057
{
2158
Config config = new Config();
2259
config.SetUcHost("fake-uc.csharp.qiniu.com");
23-
config.SetBackupUcHost(new List<string>
60+
config.SetBackupQueryRegionHosts(new List<string>
2461
{
2562
"unavailable-uc.csharp.qiniu.com",
2663
"uc.qbox.me"
@@ -32,7 +69,7 @@ public void QueryZoneWithBackupHostsTest()
3269
AccessKey,
3370
Bucket,
3471
config.UcHost(),
35-
config.BackupUcHost()
72+
config.BackupQueryRegionHosts()
3673
);
3774
Assert.NotNull(zone);
3875
}

0 commit comments

Comments
 (0)