Skip to content

Commit 8f26b4f

Browse files
Merge pull request #60 from tencentyun/feature_nedzzhang_54a490c9
更新 C# SDK 的重试机制,支持自定义域名切换和重试配置优化
2 parents 0f8e502 + 428ec96 commit 8f26b4f

File tree

9 files changed

+32
-12
lines changed

9 files changed

+32
-12
lines changed

QCloudCSharpSDK/COSXML/COSXML-Netcore.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
<Deterministic>true</Deterministic>
1212

1313
<PackageId>Tencent.QCloud.Cos.Sdk</PackageId>
14-
<Version>5.4.46.0</Version>
14+
<Version>5.4.47.0</Version>
1515
<Authors>Tencent</Authors>
1616
<Company>Tencent</Company>
1717
<description>Tencent Cloud COS(Cloud Object Service) .Net SDK</description>

QCloudCSharpSDK/COSXML/Common/CosVersion.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ namespace COSXML.Common
99
{
1010
public sealed class CosVersion
1111
{
12-
private static string SDKVersion = "5.4.46.0";
12+
private static string SDKVersion = "5.4.47.0";
1313
// 主版本号:第一个数字,产品改动较大,可能无法向后兼容(要看具体项目)
1414
// 子版本号:第二个数字,增加了新功能,向后兼容
1515
// 修正版本号:第三个数字,修复BUG 或优化代码,一般没有添加新功能,向后兼容

QCloudCSharpSDK/COSXML/CosXmlConfig.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ public sealed class CosXmlConfig
2828
/// </summary>
2929
/// <value></value>
3030
public string host { get; private set; }
31+
32+
public string retryHost;
3133

3234
private CosXmlConfig(Builder builder)
3335
{
@@ -38,6 +40,7 @@ private CosXmlConfig(Builder builder)
3840
this.isDebug = builder.isDebug;
3941
this.endpointSuffix = builder.endpointSuffix;
4042
this.host = builder.host;
43+
this.retryHost = builder.retryHost;
4144
}
4245

4346
/// <summary>
@@ -119,6 +122,8 @@ public sealed class Builder
119122

120123
internal string host;
121124

125+
internal string retryHost;
126+
122127
/// <summary>
123128
/// 初始化一个构造器
124129
/// </summary>
@@ -138,6 +143,12 @@ public Builder SetAppid(string appid)
138143

139144
return this;
140145
}
146+
147+
public Builder SetRetryHost(string host)
148+
{
149+
this.retryHost = host;
150+
return this;
151+
}
141152

142153
/// <summary>
143154
/// 存储桶所属地域

QCloudCSharpSDK/COSXML/Model/CosRequest.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,9 @@ public bool? IsHttps
8282
set { isHttps = value; }
8383
}
8484

85+
86+
public int MaxRetry = 3;
87+
8588
/// <summary>
8689
/// http method
8790
/// </summary>

QCloudCSharpSDK/COSXML/Model/Object/ObjectRequest.cs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,12 @@ public override Network.RequestBody GetRequestBody()
9494
public override string GetHost()
9595
{
9696
StringBuilder hostBuilder = new StringBuilder();
97-
97+
if (!String.IsNullOrEmpty(serviceConfig.retryHost) && !RetryKeepDefaultDomain && RetryUseBackupDomain)
98+
{
99+
hostBuilder.Append(serviceConfig.retryHost);
100+
return hostBuilder.ToString();
101+
}
102+
98103
if (!String.IsNullOrEmpty(serviceConfig.host))
99104
{
100105
hostBuilder.Append(serviceConfig.host);
@@ -125,7 +130,7 @@ public override string GetHost()
125130
String hostStr = hostBuilder.ToString();
126131

127132
//用户保持原域名 和 条件判断重试使用备用域名
128-
if (RetryKeepDefaultDomain && RetryUseBackupDomain)
133+
if (!RetryKeepDefaultDomain && RetryUseBackupDomain)
129134
{
130135
StringBuilder pattern = new StringBuilder();
131136
pattern.Append(".cos.").Append(region).Append(".myqcloud.com");

QCloudCSharpSDK/COSXML/Network/CommandTask.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,10 @@ private static void HandleHttpWebRequest(HttpWebRequest httpWebRequest, Request
138138
// httpWebRequest.ContentLength = request.Body.ContentLength;
139139
request.Body.OnWrite(httpWebRequest.GetRequestStream());
140140
}
141+
else
142+
{
143+
httpWebRequest.ContentLength = 0L;
144+
}
141145
//print request start log
142146
PrintReqeustInfo(httpWebRequest);
143147
}

QCloudCSharpSDK/COSXML/Network/HttpClient.cs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,7 @@ public sealed class HttpClient
3434
private static Object syncInstance = new Object();
3535

3636
private const int MAX_ACTIVIE_TASKS = 5;
37-
38-
public int MaxRetry { private get; set;} = 3;
37+
3938

4039
private volatile int activieTasks = 0;
4140

@@ -146,7 +145,7 @@ public void InternalExcute(CosRequest cosRequest, CosResult cosResult, QCloudCre
146145
catch (CosServerException serverException)
147146
{
148147
// 状态码为301/302/307时,满足域名切换条件(域名匹配myqcloud.com & 响应不含cos requestid & 开启域名切换开关)时,进行3次重试;其余不重试
149-
if (retryIndex < MaxRetry &&
148+
if (retryIndex < cosRequest.MaxRetry &&
150149
(serverException.statusCode == 301 || serverException.statusCode == 302 || serverException.statusCode == 307))
151150
{
152151
if (request.Host.Contains("myqcloud.com") && serverException.requestId == String.Empty)
@@ -156,9 +155,9 @@ public void InternalExcute(CosRequest cosRequest, CosResult cosResult, QCloudCre
156155
cosRequest.SetRequestHeader("x-cos-sdk-retry", "true");
157156
InternalExcute(cosRequest, cosResult, credentialProvider, retryIndex + 1);
158157
}
159-
else if (retryIndex < MaxRetry && serverException.statusCode >= 500)
158+
else if (retryIndex < cosRequest.MaxRetry && serverException.statusCode >= 500)
160159
{
161-
if (retryIndex == MaxRetry - 1)
160+
if (retryIndex == cosRequest.MaxRetry - 1 || !cosRequest.RetryKeepDefaultDomain)//最后一次切换域名
162161
{
163162
cosRequest.RetryUseBackupDomain = true;
164163
}

QCloudCSharpSDK/COSXMLTests/ObjectTest.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1973,6 +1973,7 @@ public async Task TestDownloadTask()
19731973
{
19741974
GetObjectRequest request = new GetObjectRequest(bucket, commonKey, localDir, localFileName);
19751975
request.LimitTraffic(8 * 1024 * 1024);
1976+
request.MaxRetry = 2;
19761977

19771978
//执行请求
19781979
COSXMLDownloadTask downloadTask = new COSXMLDownloadTask(request);

QCloudCSharpSDK/COSXMLTests/Utils/RequestTest.cs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,6 @@ public void InitTest()
4242
}
4343

4444
req.RequestUrlString = "test-url";
45-
HttpClient httpClient = HttpClient.GetInstance();
46-
var maxRetry = httpClient.MaxRetry = 5;
47-
Assert.AreEqual(5, maxRetry);
4845
// CosXmlConfig.Builder cosXmlConfig = new CosXmlConfig();
4946
try
5047
{

0 commit comments

Comments
 (0)