Skip to content

Commit f92ad20

Browse files
authored
Merge pull request #3 from shabicheng/dev
[modify] refine network operation & rename namespace
2 parents b96eb1d + 552c443 commit f92ad20

File tree

7 files changed

+235
-202
lines changed

7 files changed

+235
-202
lines changed

SLSSDK/Client.cs

Lines changed: 201 additions & 185 deletions
Large diffs are not rendered by default.

SLSSDK/Common/Communication/ServiceClient.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,6 @@ public static ServiceClient Create(ClientConfiguration configuration)
5656
public ServiceResponse Send(ServiceRequest request, ExecutionContext context)
5757
{
5858
Debug.Assert(request != null);
59-
6059
SignRequest(request, context);
6160
ServiceResponse response = SendCore(request, context);
6261
HandleResponse(response, context.ResponseHandlers);

SLSSDK/Common/Communication/ServiceClientImpl.cs

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
using Aliyun.Api.LOG;
1515
using Aliyun.Api.LOG.Common.Utilities;
1616
using Aliyun.Api.LOG.Utilities;
17-
using System.Threading;
17+
1818
namespace Aliyun.Api.LOG.Common.Communication
1919
{
2020
/// <summary>
@@ -235,14 +235,14 @@ private static ServiceResponse HandleException(WebException ex)
235235
protected override ServiceResponse SendCore(ServiceRequest serviceRequest,
236236
ExecutionContext context)
237237
{
238-
Random rnd = new Random();
239238
try
240239
{
241-
HttpWebRequest request = HttpFactory.CreateWebRequest(serviceRequest, Configuration);
240+
serviceRequest.HttpRequest = HttpFactory.CreateWebRequest(serviceRequest, Configuration);
242241
#if(!__UT_TEST_0EC173788C65DD08DA60575219707632__)
243-
SetRequestContent(request, serviceRequest);
242+
SetRequestContent(serviceRequest.HttpRequest, serviceRequest);
244243
#endif
245-
return SendMethod(request);
244+
245+
return SendMethod(serviceRequest.HttpRequest);
246246
}
247247
catch (WebException ex)
248248
{
@@ -268,25 +268,33 @@ private static void SetRequestContent(HttpWebRequest webRequest,
268268

269269
// Write data to the request stream.
270270
long userSetContentLength = -1;
271-
if (serviceRequest.Headers.ContainsKey(HttpHeaders.ContentLength))
271+
if (serviceRequest.Headers.ContainsKey(Aliyun.Api.LOG.Common.Utilities.HttpHeaders.ContentLength))
272272
{
273-
userSetContentLength = long.Parse(serviceRequest.Headers[HttpHeaders.ContentLength]);
273+
userSetContentLength = long.Parse(serviceRequest.Headers[Aliyun.Api.LOG.Common.Utilities.HttpHeaders.ContentLength]);
274274
}
275275

276276
long streamLength = data.Length - data.Position;
277277
webRequest.ContentLength = (userSetContentLength >= 0 && userSetContentLength <= streamLength) ?
278278
userSetContentLength : streamLength;
279279

280-
//webRequest.
281-
//webRequest.KeepAlive = true;
282-
//webRequest.ReadWriteTimeout = 100000000;
283-
//webRequest.ServicePoint.ConnectionLimit = 100000;
284-
//webRequest.Timeout = Configuration
280+
webRequest.KeepAlive = false;
281+
webRequest.ServicePoint.ConnectionLeaseTimeout = 5000;
282+
webRequest.ServicePoint.MaxIdleTime = 5000;
283+
webRequest.ServicePoint.ConnectionLimit = 1000;
284+
webRequest.ServicePoint.Expect100Continue = false;
285+
// Generate GUID for connection group name, force close it when request is done.
286+
webRequest.ConnectionGroupName = Guid.NewGuid().ToString();
287+
285288
using (var requestStream = webRequest.GetRequestStream())
286289
{
287290
data.WriteTo(requestStream, webRequest.ContentLength);
288291
data.Seek(0, SeekOrigin.Begin);
292+
requestStream.Flush();
293+
requestStream.Close();
294+
289295
}
296+
data.Close();
297+
data.Dispose();
290298
}
291299

292300
#endregion

SLSSDK/Common/Communication/ServiceRequest.cs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
using System.Text;
1212

1313
using Aliyun.Api.LOG.Common.Utilities;
14+
using System.Net;
1415

1516
namespace Aliyun.Api.LOG.Common.Communication
1617
{
@@ -38,7 +39,9 @@ internal class ServiceRequest : ServiceMessage, IDisposable
3839
/// Gets or sets the HTTP method.
3940
/// </summary>
4041
public HttpMethod Method { get; set; }
41-
42+
43+
public HttpWebRequest HttpRequest { get; set; }
44+
4245
/// <summary>
4346
/// Gets the dictionary of the request parameters.
4447
/// </summary>
@@ -145,6 +148,14 @@ protected virtual void Dispose(bool disposing)
145148
Content.Close();
146149
Content = null;
147150
}
151+
if (HttpRequest != null)
152+
{
153+
// force close connection group
154+
//Console.WriteLine(HttpRequest.ConnectionGroupName + "[]" + HttpRequest.ConnectionGroupName.Length.ToString() + "[]" + HttpRequest.ServicePoint.CurrentConnections.ToString());
155+
HttpRequest.ServicePoint.CloseConnectionGroup(HttpRequest.ConnectionGroupName);
156+
HttpRequest.Abort();
157+
HttpRequest = null;
158+
}
148159
_disposed = true;
149160
}
150161
}

SLSSDK/Response/BatchGetLogsResponse.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
using System.Linq;
88
using System.Text;
99

10-
namespace Aliyun.Api.SLS.Response
10+
namespace Aliyun.Api.LOG.Response
1111
{
1212
public class BatchGetLogsResponse : LogResponse
1313
{

SLSSDK/Response/ListShardsResponse.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
using System.Linq;
66
using System.Text;
77

8-
namespace Aliyun.Api.SLS.Response
8+
namespace Aliyun.Api.LOG.Response
99
{
1010
public class ListShardsResponse: LogResponse
1111
{

SLSSDK/sample/LoghubSample.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
using Aliyun.Api.LOG.Request;
44
using Aliyun.Api.LOG.Response;
55
using Aliyun.Api.LOG.Utilities;
6-
using Aliyun.Api.SLS.Response;
76
using System;
87
using System.Collections.Generic;
98
using System.IO;

0 commit comments

Comments
 (0)