Skip to content

Commit dfc5472

Browse files
committed
fix: ConcurrentDictionary 쓰레드 안정성 확보
Dictionary -> ConcurrentDictionary
1 parent 0fad943 commit dfc5472

File tree

1 file changed

+7
-6
lines changed

1 file changed

+7
-6
lines changed

ProjectVG.Api/Services/PerformanceCounterService.cs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
using System.Collections.Concurrent;
12
using System.Diagnostics;
23
using System.Diagnostics.Tracing;
34

@@ -7,14 +8,14 @@ public class PerformanceCounterService : IDisposable
78
{
89
private readonly ILogger<PerformanceCounterService> _logger;
910
private readonly Timer _metricsTimer;
10-
private readonly Dictionary<string, object> _lastMetrics;
11+
private readonly ConcurrentDictionary<string, object> _lastMetrics;
1112
private readonly Process _currentProcess;
1213
private bool _disposed = false;
1314

1415
public PerformanceCounterService(ILogger<PerformanceCounterService> logger)
1516
{
1617
_logger = logger;
17-
_lastMetrics = new Dictionary<string, object>();
18+
_lastMetrics = new ConcurrentDictionary<string, object>();
1819
_currentProcess = Process.GetCurrentProcess();
1920

2021
// 5초마다 메트릭 수집
@@ -73,10 +74,10 @@ private void CollectMetrics(object? state)
7374
}
7475

7576
// 최신 메트릭 캐시
76-
_lastMetrics["LastUpdate"] = metrics.Timestamp;
77-
_lastMetrics["WorkingMemory"] = metrics.WorkingSetMemoryMB;
78-
_lastMetrics["CpuUsage"] = metrics.CpuUsagePercent;
79-
_lastMetrics["ThreadPoolPending"] = metrics.ThreadPoolPendingWorkItems;
77+
_lastMetrics.AddOrUpdate("LastUpdate", metrics.Timestamp, (k, v) => metrics.Timestamp);
78+
_lastMetrics.AddOrUpdate("WorkingMemory", metrics.WorkingSetMemoryMB, (k, v) => metrics.WorkingSetMemoryMB);
79+
_lastMetrics.AddOrUpdate("CpuUsage", metrics.CpuUsagePercent, (k, v) => metrics.CpuUsagePercent);
80+
_lastMetrics.AddOrUpdate("ThreadPoolPending", metrics.ThreadPoolPendingWorkItems, (k, v) => metrics.ThreadPoolPendingWorkItems);
8081
}
8182
catch (Exception ex)
8283
{

0 commit comments

Comments
 (0)