Skip to content

Commit

Permalink
确保内存池回收
Browse files Browse the repository at this point in the history
  • Loading branch information
nnhy committed Oct 22, 2024
1 parent ea51b3b commit 05282cb
Showing 1 changed file with 12 additions and 6 deletions.
18 changes: 12 additions & 6 deletions Stardust.Extensions/TracerMiddleware.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,25 +49,28 @@ public async Task Invoke(HttpContext ctx)
{
var flag = false;
if (req.ContentLength != null &&
req.ContentLength < 1024 * 8 &&
req.ContentLength < 1024 &&
req.ContentType != null &&
req.ContentType.StartsWithIgnoreCase(TagTypes))
{
var buf = Pool.Shared.Rent(1024);
try
{
req.EnableBuffering();

var buf = Pool.Shared.Rent(1024);
var count = await req.Body.ReadAsync(buf, 0, buf.Length);
span.AppendTag("\r\n<=\r\n" + buf.ToStr(null, 0, count));
req.Body.Position = 0;
Pool.Shared.Return(buf);
flag = true;
}
catch (Exception ex)
{
XTrace.Log.Error("[{0}]读取请求主体失败:{1}", action, ex.Message);
}
finally
{
Pool.Shared.Return(buf);
}
}

if (span.Tag.Length < 500)
Expand Down Expand Up @@ -107,25 +110,28 @@ public async Task Invoke(HttpContext ctx)
{
var flag = false;
if (res.ContentLength != null &&
res.ContentLength < 1024 * 8 &&
res.ContentLength < 1024 &&
res.Body.CanSeek &&
res.ContentType != null &&
res.ContentType.StartsWithIgnoreCase(TagTypes))
{
var buf = Pool.Shared.Rent(1024);
try
{
var buf = Pool.Shared.Rent(1024);
var p = res.Body.Position;
var count = await res.Body.ReadAsync(buf, 0, buf.Length);
span.AppendTag("\r\n=>\r\n" + buf.ToStr(null, 0, count));
res.Body.Position = p;
Pool.Shared.Return(buf);
flag = true;
}
catch (Exception ex)
{
XTrace.Log.Error("[{0}]读取响应主体失败:{1}", action, ex.Message);
}
finally
{
Pool.Shared.Return(buf);
}
}

if (span.Tag == null || span.Tag.Length < 500)
Expand Down

0 comments on commit 05282cb

Please sign in to comment.