Skip to content

Commit 0dd4fd7

Browse files
authored
Do not use DateTime.Now to compute elapsed time. (#1315)
* Do not use DateTime.Now to compute elapsed time. * Remove extra blank line
1 parent 4e3cac6 commit 0dd4fd7

File tree

1 file changed

+18
-5
lines changed

1 file changed

+18
-5
lines changed

Minio/RequestExtensions.cs

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using System.Diagnostics.CodeAnalysis;
1+
using System.Diagnostics;
2+
using System.Diagnostics.CodeAnalysis;
23
using System.Net;
34
using System.Web;
45
using Minio.Credentials;
@@ -80,7 +81,7 @@ private static async Task<ResponseResult> ExecuteTaskCoreAsync(this IMinioClient
8081
bool isSts = false,
8182
CancellationToken cancellationToken = default)
8283
{
83-
var startTime = DateTime.Now;
84+
var startTime = Stopwatch.GetTimestamp();
8485
var v4Authenticator = new V4Authenticator(minioClient.Config.Secure,
8586
minioClient.Config.AccessKey, minioClient.Config.SecretKey, minioClient.Config.Region,
8687
minioClient.Config.SessionToken);
@@ -369,13 +370,13 @@ internal static async Task<string> GetRegion(this IMinioClient minioClient, stri
369370
/// <param name="startTime"></param>
370371
private static void HandleIfErrorResponse(this IMinioClient minioClient, ResponseResult response,
371372
IEnumerable<IApiResponseErrorHandler> handlers,
372-
DateTime startTime)
373+
long startTime)
373374
{
374375
// Logs Response if HTTP tracing is enabled
375376
if (minioClient.Config.TraceHttp)
376377
{
377-
var now = DateTime.Now;
378-
minioClient.LogRequest(response.Request, response, (now - startTime).TotalMilliseconds);
378+
var elapsed = GetElapsedTime(startTime);
379+
minioClient.LogRequest(response.Request, response, elapsed.TotalMilliseconds);
379380
}
380381

381382
if (response.Exception is not null)
@@ -388,4 +389,16 @@ private static void HandleIfErrorResponse(this IMinioClient minioClient, Respons
388389
else
389390
minioClient.DefaultErrorHandler.Handle(response);
390391
}
392+
393+
private static TimeSpan GetElapsedTime(long startTimestamp)
394+
{
395+
#if NET8_0_OR_GREATER
396+
return Stopwatch.GetElapsedTime(startTimestamp);
397+
#else
398+
var endTimestamp = Stopwatch.GetTimestamp();
399+
var elapsedTicks = endTimestamp - startTimestamp;
400+
var seconds = (double)elapsedTicks / Stopwatch.Frequency;
401+
return TimeSpan.FromSeconds(seconds);
402+
#endif
403+
}
391404
}

0 commit comments

Comments
 (0)