Skip to content

Commit

Permalink
高级接口支持自动断点续传, 全局支持配置重试
Browse files Browse the repository at this point in the history
  • Loading branch information
shawnnqin committed Jul 21, 2022
1 parent a6757d5 commit 5108ca5
Show file tree
Hide file tree
Showing 7 changed files with 283 additions and 13 deletions.
2 changes: 1 addition & 1 deletion QCloudCSharpSDK/COSXML/COSXML-Netcore.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
<Deterministic>true</Deterministic>

<PackageId>Tencent.QCloud.Cos.Sdk</PackageId>
<Version>5.4.31.0</Version>
<Version>5.4.32.0</Version>
<Authors>Tencent</Authors>
<Company>Tencent</Company>
<description>Tencent Cloud COS(Cloud Object Service) .Net SDK</description>
Expand Down
2 changes: 1 addition & 1 deletion QCloudCSharpSDK/COSXML/Common/CosVersion.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ namespace COSXML.Common
{
public sealed class CosVersion
{
private static string SDKVersion = "5.4.31.0";
private static string SDKVersion = "5.4.32.0";

public static string GetUserAgent()
{
Expand Down
34 changes: 32 additions & 2 deletions QCloudCSharpSDK/COSXML/Network/CommandTask.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ public sealed class CommandTask
{
public const string TAG = "CommandTask";

public static int MaxRetries = 3;

public static HttpClientConfig config;

/// <summary>
/// init connectionLimit and statueCode = 100 action
/// </summary>
Expand All @@ -33,6 +37,8 @@ public static void Init(HttpClientConfig config)
{
ServicePointManager.Expect100Continue = false;
ServicePointManager.DefaultConnectionLimit = config.ConnectionLimit;
CommandTask.MaxRetries = config.MaxRetry;
CommandTask.config = config;
}

/// <summary>
Expand Down Expand Up @@ -160,7 +166,7 @@ private static void HandleHttpWebResponse(HttpWebResponse httpWebResponse, Respo
/// <param name="request"></param>
/// <param name="response"></param>
/// <param name="config"></param>
public static void Schedue(Request request, Response response, HttpClientConfig config)
public static void Schedue(Request request, Response response, HttpClientConfig config, int retryIndex = 0)
{
HttpWebRequest httpWebRequest = null;
RequestState requestState = new RequestState();
Expand All @@ -183,6 +189,7 @@ public static void Schedue(Request request, Response response, HttpClientConfig

requestState.httpWebRequest = httpWebRequest;

requestState.retryIndex = retryIndex;
//handle request body
if (request.Body != null)
{
Expand Down Expand Up @@ -238,6 +245,12 @@ public static void AsyncRequestCallback(IAsyncResult ar)
if (exception != null)
{
// handle request body throw exception
if (requestState.retryIndex < MaxRetries)
{
QLog.Error(TAG, exception.Message, exception);
Schedue(requestState.request, requestState.response, config, requestState.retryIndex + 1);
return;
}
requestState.response.OnFinish(false, exception);
//abort
requestState.Clear();
Expand All @@ -253,6 +266,12 @@ public static void AsyncRequestCallback(IAsyncResult ar)
}
catch (Exception ex)
{
if (requestState.retryIndex < MaxRetries)
{
QLog.Error(TAG, ex.Message, ex);
Schedue(requestState.request, requestState.response, config, requestState.retryIndex + 1);
return;
}
requestState.response.OnFinish(false, ex);
//abort
requestState.Clear();
Expand Down Expand Up @@ -291,7 +310,11 @@ public static void AsyncResponseCallback(IAsyncResult ar)
}
catch (WebException webEx)
{

if (requestState.retryIndex < MaxRetries)
{
Schedue(requestState.request, requestState.response, config, requestState.retryIndex + 1);
return;
}
if (webEx.Response != null && webEx.Response is HttpWebResponse)
{
//nofity get response
Expand Down Expand Up @@ -326,6 +349,11 @@ public static void AsyncResponseCallback(IAsyncResult ar)
}
catch (Exception ex)
{
if (requestState.retryIndex < MaxRetries)
{
Schedue(requestState.request, requestState.response, config, requestState.retryIndex + 1);
return;
}
requestState.response.OnFinish(false, ex);
//abort
requestState.Clear();
Expand Down Expand Up @@ -608,6 +636,8 @@ internal class RequestState

public Request request;

public int retryIndex;

public RequestState()
{
httpWebRequest = null;
Expand Down
39 changes: 33 additions & 6 deletions QCloudCSharpSDK/COSXML/Network/HttpClient.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System;
using System;
using System.Collections.Generic;

using System.Text;
Expand Down Expand Up @@ -35,6 +35,8 @@ public sealed class HttpClient

private const int MAX_ACTIVIE_TASKS = 5;

public int MaxRetry { private get; set;} = 3;

private volatile int activieTasks = 0;

public static HttpClient GetInstance()
Expand Down Expand Up @@ -110,7 +112,7 @@ public void Schedue(CosRequest cosRequest, CosResult cosResult, COSXML.Callback.
/// <param name="credentialProvider"></param>
/// <exception cref="COSXML.CosException.CosClientException">CosClientException</exception>
/// <exception cref="COSXML.CosException.CosServerException">CosServerException</exception>
public void InternalExcute(CosRequest cosRequest, CosResult cosResult, QCloudCredentialProvider credentialProvider)
public void InternalExcute(CosRequest cosRequest, CosResult cosResult, QCloudCredentialProvider credentialProvider, int retryIndex = 0)
{

try
Expand Down Expand Up @@ -141,17 +143,42 @@ public void InternalExcute(CosRequest cosRequest, CosResult cosResult, QCloudCre
cosRequest.BindRequest(request);
CommandTask.Excute(request, response, config);
}
catch (CosServerException)
catch (CosServerException serverException)
{
throw;
// 服务端5xx才重试
if (serverException.statusCode >= 500 && retryIndex < MaxRetry)
{
InternalExcute(cosRequest, cosResult, credentialProvider, retryIndex + 1);
}
else
{
throw;
}
}
catch (CosClientException)
{
throw;
// 客户端异常都重试
if (retryIndex < MaxRetry)
{
InternalExcute(cosRequest, cosResult, credentialProvider, retryIndex + 1);
}
else
{
throw;
}

}
catch (Exception ex)
{
throw new CosClientException((int)CosClientError.BadRequest, ex.Message, ex);
// 未知异常也重试
if (retryIndex < MaxRetry)
{
InternalExcute(cosRequest, cosResult, credentialProvider, retryIndex + 1);
}
else
{
throw new CosClientException((int)CosClientError.BadRequest, ex.Message, ex);
}
}

}
Expand Down
16 changes: 16 additions & 0 deletions QCloudCSharpSDK/COSXML/Network/HttpClientConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,15 @@ public bool KeepAlive
}
}

public int MaxRetry
{
get
{

return maxRetry;
}
}

public class Builder
{
internal string userAgent = CosVersion.GetUserAgent();
Expand Down Expand Up @@ -272,6 +281,13 @@ public Builder SetHttpKeepAlive(bool keepAlive)
return this;
}

public Builder SetMaxRetry(int maxRetry)
{
this.maxRetry = maxRetry;

return this;
}

public HttpClientConfig Build()
{

Expand Down
Loading

0 comments on commit 5108ca5

Please sign in to comment.