Skip to content

Commit ccad4a2

Browse files
committed
add query zone with backup hosts
1 parent 8a25a28 commit ccad4a2

File tree

3 files changed

+71
-7
lines changed

3 files changed

+71
-7
lines changed

src/Qiniu/Storage/Config.cs

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11

2+
using System.Collections.Generic;
3+
24
namespace Qiniu.Storage
35
{
46
/// <summary>
@@ -13,6 +15,15 @@ public class Config
1315
/// 默认空间管理域名
1416
/// </summary>
1517
public static string DefaultUcHost = "uc.qbox.me";
18+
/// <summary>
19+
/// 默认备用空间管理域名
20+
/// </summary>
21+
public static List<string> DefaultBackupUcHosts = new List<string>
22+
{
23+
"kodo-config.qiniuapi.com",
24+
"api.qiniu.com"
25+
};
26+
1627
/// <summary>
1728
/// 默认高级资源管理域名
1829
/// </summary>
@@ -57,9 +68,12 @@ public class Config
5768

5869
private string _ucHost = DefaultUcHost;
5970

71+
private List<string> _backupUcHosts = DefaultBackupUcHosts;
72+
6073
public void SetUcHost(string val)
6174
{
6275
_ucHost = val;
76+
_backupUcHosts.Clear();
6377
}
6478

6579
public string UcHost()
@@ -68,6 +82,16 @@ public string UcHost()
6882
return string.Format("{0}{1}", scheme, _ucHost);
6983
}
7084

85+
public void SetBackupUcHost(List<string> val)
86+
{
87+
_backupUcHosts = val;
88+
}
89+
90+
public List<string> BackupUcHost()
91+
{
92+
return _backupUcHosts;
93+
}
94+
7195
/// <summary>
7296
/// 获取资源管理域名
7397
/// </summary>
@@ -80,7 +104,7 @@ public string RsHost(string ak, string bucket)
80104
Zone z = this.Zone;
81105
if (z == null)
82106
{
83-
z = ZoneHelper.QueryZone(ak, bucket, UcHost());
107+
z = ZoneHelper.QueryZone(ak, bucket, UcHost(), BackupUcHost());
84108
}
85109
return string.Format("{0}{1}", scheme, z.RsHost);
86110
}
@@ -97,7 +121,7 @@ public string RsfHost(string ak, string bucket)
97121
Zone z = this.Zone;
98122
if (z == null)
99123
{
100-
z = ZoneHelper.QueryZone(ak, bucket, UcHost());
124+
z = ZoneHelper.QueryZone(ak, bucket, UcHost(), BackupUcHost());
101125
}
102126
return string.Format("{0}{1}", scheme, z.RsfHost);
103127
}
@@ -114,7 +138,7 @@ public string ApiHost(string ak, string bucket)
114138
Zone z = this.Zone;
115139
if (z == null)
116140
{
117-
z = ZoneHelper.QueryZone(ak, bucket, UcHost());
141+
z = ZoneHelper.QueryZone(ak, bucket, UcHost(), BackupUcHost());
118142
}
119143
return string.Format("{0}{1}", scheme, z.ApiHost);
120144
}
@@ -131,7 +155,7 @@ public string IovipHost(string ak, string bucket)
131155
Zone z = this.Zone;
132156
if (z == null)
133157
{
134-
z = ZoneHelper.QueryZone(ak, bucket, UcHost());
158+
z = ZoneHelper.QueryZone(ak, bucket, UcHost(), BackupUcHost());
135159
}
136160
return string.Format("{0}{1}", scheme, z.IovipHost);
137161
}
@@ -148,7 +172,7 @@ public string UpHost(string ak, string bucket)
148172
Zone z = this.Zone;
149173
if (z == null)
150174
{
151-
z = ZoneHelper.QueryZone(ak, bucket, UcHost());
175+
z = ZoneHelper.QueryZone(ak, bucket, UcHost(), BackupUcHost());
152176
}
153177
string upHost = z.SrcUpHosts[0];
154178
if (this.UseCdnDomains)

src/Qiniu/Storage/ZoneHelper.cs

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,12 @@ public class ZoneHelper
2626
/// <param name="accessKey">AccessKey</param>
2727
/// <param name="bucket">空间名称</param>
2828
/// <param name="ucHost">UC 域名</param>
29-
public static Zone QueryZone(string accessKey, string bucket, string ucHost = null)
29+
public static Zone QueryZone(
30+
string accessKey,
31+
string bucket,
32+
string ucHost = null,
33+
List<string> backupUcHosts = null
34+
)
3035
{
3136
ZoneCacheValue zoneCacheValue = null;
3237
string cacheKey = string.Format("{0}:{1}", accessKey, bucket);
@@ -56,6 +61,12 @@ public static Zone QueryZone(string accessKey, string bucket, string ucHost = nu
5661
{
5762
ucHost = "https://" + Config.DefaultUcHost;
5863
}
64+
65+
if (backupUcHosts == null)
66+
{
67+
backupUcHosts = Config.DefaultBackupUcHosts;
68+
}
69+
5970
try
6071
{
6172
string queryUrl = string.Format("{0}/v4/query?ak={1}&bucket={2}",
@@ -64,7 +75,13 @@ public static Zone QueryZone(string accessKey, string bucket, string ucHost = nu
6475
bucket
6576
);
6677
HttpManager httpManager = new HttpManager();
67-
hr = httpManager.Get(queryUrl, null);
78+
List<IMiddleware> middlewares = new List<IMiddleware>
79+
{
80+
new RetryDomainsMiddleware(
81+
backupUcHosts
82+
)
83+
};
84+
hr = httpManager.Get(queryUrl, null, null, middlewares);
6885
if (hr.Code != (int) HttpCode.OK || string.IsNullOrEmpty(hr.Text))
6986
{
7087
throw new Exception("code: " + hr.Code + ", text: " + hr.Text + ", ref-text:" + hr.RefText);

src/QiniuTests/Storage/ZoneHelperTests.cs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
using System.Collections.Generic;
12
using NUnit.Framework;
23
using Qiniu.Tests;
34

@@ -10,7 +11,29 @@ public class ZoneHelperTests : TestEnv
1011
public void QueryZoneTest()
1112
{
1213
Zone zone = ZoneHelper.QueryZone(AccessKey, Bucket);
14+
15+
Assert.NotNull(zone);
16+
}
17+
18+
[Test]
19+
public void QueryZoneWithBackupHostsTest()
20+
{
21+
Config config = new Config();
22+
config.SetUcHost("fake-uc.csharp.qiniu.com");
23+
config.SetBackupUcHost(new List<string>
24+
{
25+
"unavailable-uc.csharp.qiniu.com",
26+
"uc.qbox.me"
27+
}
28+
);
29+
config.UseHttps = true;
1330

31+
Zone zone = ZoneHelper.QueryZone(
32+
AccessKey,
33+
Bucket,
34+
config.UcHost(),
35+
config.BackupUcHost()
36+
);
1437
Assert.NotNull(zone);
1538
}
1639
}

0 commit comments

Comments
 (0)