Skip to content

Commit cebe48f

Browse files
authored
Merge pull request #248 from qiniu/fix/list-domains
update Domains and queryZone
2 parents 4020f2d + b93265f commit cebe48f

File tree

6 files changed

+217
-105
lines changed

6 files changed

+217
-105
lines changed

src/Qiniu/Storage/BucketManager.cs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -83,14 +83,13 @@ public BucketsResult Buckets(bool shared)
8383

8484
try
8585
{
86-
string scheme = this.config.UseHttps ? "https://" : "http://";
87-
string rsHost = string.Format("{0}{1}", scheme, Config.DefaultRsHost);
86+
string ucHost = this.config.UcHost();
8887
string sharedStr = "false";
8988
if (shared)
9089
{
9190
sharedStr = "true";
9291
}
93-
string bucketsUrl = string.Format("{0}/buckets?shared={1}", rsHost, sharedStr);
92+
string bucketsUrl = string.Format("{0}/buckets?shared={1}", ucHost, sharedStr);
9493

9594
HttpResult hr = httpManager.Get(bucketsUrl, null, auth);
9695
result.Shadow(hr);
@@ -550,9 +549,8 @@ public DomainsResult Domains(string bucket)
550549

551550
try
552551
{
553-
string scheme = this.config.UseHttps ? "https://" : "http://";
554-
string rsHost = string.Format("{0}{1}", scheme, Config.DefaultApiHost);
555-
string domainsUrl = string.Format("{0}{1}", rsHost, "/v6/domain/list");
552+
string ucHost = this.config.UcHost();
553+
string domainsUrl = string.Format("{0}{1}", ucHost, "/v2/domains");
556554
string body = string.Format("tbl={0}", bucket);
557555

558556
HttpResult hr = httpManager.PostForm(domainsUrl, null, body, auth);

src/Qiniu/Storage/Config.cs

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@ namespace Qiniu.Storage
99
/// </summary>
1010
public class Config
1111
{
12+
/// <summary>
13+
/// 默认空间管理域名
14+
/// </summary>
15+
public static string DefaultUcHost = "uc.qbox.me";
1216
/// <summary>
1317
/// 默认高级资源管理域名
1418
/// </summary>
@@ -18,6 +22,14 @@ public class Config
1822
/// </summary>
1923
public static string DefaultApiHost = "api.qiniuapi.com";
2024
/// <summary>
25+
/// 默认数据处理域名
26+
/// </summary>
27+
public static string DefaultIoHost = "iovip.qiniuio.com";
28+
/// <summary>
29+
/// 默认数据处理域名
30+
/// </summary>
31+
public static string DefaultRsfHost = "rsf.qiniu.com";
32+
/// <summary>
2133
/// 空间所在的区域(Zone)
2234
/// </summary>
2335
public Zone Zone = null;
@@ -42,6 +54,19 @@ public class Config
4254
/// </summary>
4355
/// 默认值应与 <see cref="PutExtra.MaxRetryTimes"/> 一致
4456
public int MaxRetryTimes { set; get; } = 3;
57+
58+
private string _ucHost = DefaultUcHost;
59+
60+
public void SetUcHost(string val)
61+
{
62+
_ucHost = val;
63+
}
64+
65+
public string UcHost()
66+
{
67+
string scheme = UseHttps ? "https://" : "http://";
68+
return string.Format("{0}{1}", scheme, _ucHost);
69+
}
4570

4671
/// <summary>
4772
/// 获取资源管理域名
@@ -55,7 +80,7 @@ public string RsHost(string ak, string bucket)
5580
Zone z = this.Zone;
5681
if (z == null)
5782
{
58-
z = ZoneHelper.QueryZone(ak, bucket);
83+
z = ZoneHelper.QueryZone(ak, bucket, UcHost());
5984
}
6085
return string.Format("{0}{1}", scheme, z.RsHost);
6186
}
@@ -72,7 +97,7 @@ public string RsfHost(string ak, string bucket)
7297
Zone z = this.Zone;
7398
if (z == null)
7499
{
75-
z = ZoneHelper.QueryZone(ak, bucket);
100+
z = ZoneHelper.QueryZone(ak, bucket, UcHost());
76101
}
77102
return string.Format("{0}{1}", scheme, z.RsfHost);
78103
}
@@ -89,7 +114,7 @@ public string ApiHost(string ak, string bucket)
89114
Zone z = this.Zone;
90115
if (z == null)
91116
{
92-
z = ZoneHelper.QueryZone(ak, bucket);
117+
z = ZoneHelper.QueryZone(ak, bucket, UcHost());
93118
}
94119
return string.Format("{0}{1}", scheme, z.ApiHost);
95120
}
@@ -106,7 +131,7 @@ public string IovipHost(string ak, string bucket)
106131
Zone z = this.Zone;
107132
if (z == null)
108133
{
109-
z = ZoneHelper.QueryZone(ak, bucket);
134+
z = ZoneHelper.QueryZone(ak, bucket, UcHost());
110135
}
111136
return string.Format("{0}{1}", scheme, z.IovipHost);
112137
}
@@ -123,7 +148,7 @@ public string UpHost(string ak, string bucket)
123148
Zone z = this.Zone;
124149
if (z == null)
125150
{
126-
z = ZoneHelper.QueryZone(ak, bucket);
151+
z = ZoneHelper.QueryZone(ak, bucket, UcHost());
127152
}
128153
string upHost = z.SrcUpHosts[0];
129154
if (this.UseCdnDomains)

src/Qiniu/Storage/ZoneHelper.cs

Lines changed: 90 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -6,111 +6,130 @@
66

77
namespace Qiniu.Storage
88
{
9+
internal class ZoneCacheValue
10+
{
11+
public DateTime Deadline { get; set; }
12+
public Zone Zone { get; set; }
13+
}
14+
915
/// <summary>
1016
/// Zone辅助类,查询及配置Zone
1117
/// </summary>
1218
public class ZoneHelper
1319
{
14-
private static Dictionary<string, Zone> zoneCache = new Dictionary<string, Zone>();
20+
private static Dictionary<string, ZoneCacheValue> zoneCache = new Dictionary<string, ZoneCacheValue>();
1521
private static object rwLock = new object();
1622

1723
/// <summary>
18-
/// 从uc.qbox.me查询得到回复后,解析出upHost,然后根据upHost确定Zone
24+
/// 从 UC 服务查询得到各个服务域名,生成 Zone 对象并返回
1925
/// </summary>
20-
/// <param name="accessKey">AccessKek</param>
26+
/// <param name="accessKey">AccessKey</param>
2127
/// <param name="bucket">空间名称</param>
22-
public static Zone QueryZone(string accessKey, string bucket)
28+
/// <param name="ucHost">UC 域名</param>
29+
public static Zone QueryZone(string accessKey, string bucket, string ucHost = null)
2330
{
24-
Zone zone = null;
25-
31+
ZoneCacheValue zoneCacheValue = null;
2632
string cacheKey = string.Format("{0}:{1}", accessKey, bucket);
2733

2834
//check from cache
2935
lock (rwLock)
3036
{
3137
if (zoneCache.ContainsKey(cacheKey))
3238
{
33-
zone = zoneCache[cacheKey];
39+
zoneCacheValue = zoneCache[cacheKey];
3440
}
3541
}
3642

37-
if (zone != null)
43+
if (
44+
zoneCacheValue != null &&
45+
DateTime.Now < zoneCacheValue.Deadline &&
46+
zoneCacheValue.Zone != null
47+
)
3848
{
39-
return zone;
49+
return zoneCacheValue.Zone;
4050
}
4151

4252
//query from uc api
53+
Zone zone;
4354
HttpResult hr = null;
55+
if (String.IsNullOrEmpty(ucHost))
56+
{
57+
ucHost = "https://" + Config.DefaultUcHost;
58+
}
4459
try
4560
{
46-
string queryUrl = string.Format("https://uc.qbox.me/v2/query?ak={0}&bucket={1}", accessKey, bucket);
61+
string queryUrl = string.Format("{0}/v4/query?ak={1}&bucket={2}",
62+
ucHost,
63+
accessKey,
64+
bucket
65+
);
4766
HttpManager httpManager = new HttpManager();
4867
hr = httpManager.Get(queryUrl, null);
49-
if (hr.Code == (int)HttpCode.OK)
68+
if (hr.Code != (int) HttpCode.OK || string.IsNullOrEmpty(hr.Text))
5069
{
51-
ZoneInfo zInfo = JsonConvert.DeserializeObject<ZoneInfo>(hr.Text);
52-
if (zInfo != null)
53-
{
54-
zone = new Zone();
55-
zone.SrcUpHosts = zInfo.Up.Src.Main;
56-
zone.CdnUpHosts = zInfo.Up.Acc.Main;
57-
zone.IovipHost = zInfo.Io.Src.Main[0];
58-
if (zone.IovipHost.Contains("z1"))
59-
{
60-
zone.ApiHost = "api-z1.qiniuapi.com";
61-
zone.RsHost = "rs-z1.qiniu.com";
62-
zone.RsfHost = "rsf-z1.qiniu.com";
63-
}
64-
else if (zone.IovipHost.Contains("z2"))
65-
{
66-
zone.ApiHost = "api-z2.qiniuapi.com";
67-
zone.RsHost = "rs-z2.qiniu.com";
68-
zone.RsfHost = "rsf-z2.qiniu.com";
69-
}
70-
else if (zone.IovipHost.Contains("na0"))
71-
{
72-
zone.ApiHost = "api-na0.qiniuapi.com";
73-
zone.RsHost = "rs-na0.qiniu.com";
74-
zone.RsfHost = "rsf-na0.qiniu.com";
75-
}
76-
else if (zone.IovipHost.Contains("as0"))
77-
{
78-
zone.ApiHost = "api-as0.qiniuapi.com";
79-
zone.RsHost = "rs-as0.qiniu.com";
80-
zone.RsfHost = "rsf-as0.qiniu.com";
81-
}
82-
else if (zone.IovipHost.Contains("cn-east-2"))
83-
{
84-
zone.ApiHost = "api-cn-east-2.qiniuapi.com";
85-
zone.RsHost = "rs-cn-east-2.qiniuapi.com";
86-
zone.RsfHost = "rsf-cn-east-2.qiniuapi.com";
87-
}
88-
else if (zone.IovipHost.Contains("ap-northeast-1"))
89-
{
90-
zone.ApiHost = "api-ap-northeast-1.qiniuapi.com";
91-
zone.RsHost = "rs-ap-northeast-1.qiniuapi.com";
92-
zone.RsfHost = "rsf-ap-northeast-1.qiniuapi.com";
93-
}
94-
else
95-
{
96-
zone.ApiHost = "api.qiniuapi.com";
97-
zone.RsHost = "rs.qiniu.com";
98-
zone.RsfHost = "rsf.qiniu.com";
99-
}
100-
101-
lock (rwLock)
102-
{
103-
zoneCache[cacheKey] = zone;
104-
}
105-
}
106-
else
107-
{
108-
throw new Exception("JSON Deserialize failed: " + hr.Text);
109-
}
70+
throw new Exception("code: " + hr.Code + ", text: " + hr.Text + ", ref-text:" + hr.RefText);
71+
}
72+
73+
ZoneInfo zInfo = JsonConvert.DeserializeObject<ZoneInfo>(hr.Text);
74+
if (zInfo == null)
75+
{
76+
throw new Exception("JSON Deserialize failed: " + hr.Text);
77+
}
78+
79+
if (zInfo.Hosts.Length == 0)
80+
{
81+
throw new Exception("There are no hosts available: " + hr.Text);
82+
}
83+
84+
ZoneHost zHost = zInfo.Hosts[0];
85+
86+
zone = new Zone();
87+
zone.SrcUpHosts = zHost.Up.Domains;
88+
zone.CdnUpHosts = zHost.Up.Domains;
89+
90+
if (!string.IsNullOrEmpty(zHost.Io.Domains[0]))
91+
{
92+
zone.IovipHost = zHost.Io.Domains[0];
11093
}
11194
else
11295
{
113-
throw new Exception("code: " + hr.Code + ", text: " + hr.Text + ", ref-text:" + hr.RefText);
96+
zone.IovipHost = Config.DefaultIoHost;
97+
}
98+
99+
if (!string.IsNullOrEmpty(zHost.Api.Domains[0]))
100+
{
101+
zone.ApiHost = zHost.Api.Domains[0];
102+
}
103+
else
104+
{
105+
zone.ApiHost = Config.DefaultApiHost;
106+
}
107+
108+
if (!string.IsNullOrEmpty(zHost.Rs.Domains[0]))
109+
{
110+
zone.RsHost = zHost.Rs.Domains[0];
111+
}
112+
else
113+
{
114+
zone.RsHost = Config.DefaultRsHost;
115+
}
116+
117+
if (!string.IsNullOrEmpty(zHost.Rsf.Domains[0]))
118+
{
119+
zone.RsfHost = zHost.Rsf.Domains[0];
120+
}
121+
else
122+
{
123+
zone.RsfHost = Config.DefaultRsfHost;
124+
}
125+
126+
lock (rwLock)
127+
{
128+
zoneCacheValue = new ZoneCacheValue();
129+
TimeSpan ttl = TimeSpan.FromSeconds(zHost.Ttl);
130+
zoneCacheValue.Deadline = DateTime.Now.Add(ttl);
131+
zoneCacheValue.Zone = zone;
132+
zoneCache[cacheKey] = zoneCacheValue;
114133
}
115134
}
116135
catch (Exception ex)

0 commit comments

Comments
 (0)