Files
spark/src/DataStreamSuite/PubSub/Services/PubSubGarbageCollectorService.cs
T
John Colvin b4cb6ac856 Add PubSub service modules designed for Dependency Injection
* Get rid of JsonPropertyName attributes in PubSubConfig
  - The Dependency Injection framework (based around dotnet ApplicationBuilder)
    does not support this attribute, and maps property names verbatim to json fields.

* Change PubSubServer json fields to match cs class property names verbatim
2025-11-03 16:13:18 -05:00

48 lines
1.7 KiB
C#

namespace Bts.PubSub.Services;
using bts;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Options;
using System;
using System.Threading;
using System.Threading.Tasks;
class PubSubGarbageCollectorService : BackgroundService {
readonly PubSubGarbageCollectorConfig config;
public PubSubGarbageCollectorService(IOptions<PubSubGarbageCollectorConfig> config)
: this(config.Value) { }
public PubSubGarbageCollectorService(PubSubGarbageCollectorConfig config) =>
this.config = config;
protected override Task ExecuteAsync(CancellationToken cancellationToken) {
cancellationToken.ThrowIfCancellationRequested();
return Task.Factory.StartNew(
Execute, cancellationToken, TaskCreationOptions.LongRunning, TaskScheduler.Default);
void Execute() {
long currentMemory = 0;
var defaultWaitTime = TimeSpan.FromMilliseconds(100);
bool gced = false;
Log.Gen.info("PubSubGarbageCollectorService STARTED");
Log.Gen.info($"Starting PubSubGarbageCollectorService execution loop");
while (!cancellationToken.IsCancellationRequested) {
gced = false;
currentMemory = GC.GetTotalMemory(false);
if (config.ActivationThreshold > 0 && currentMemory > config.ActivationThreshold) {
Console.WriteLine($"GCing memory, mem before GC = {currentMemory}");
GC.Collect();
gced = true;
}
Thread.Sleep(gced && config.Interval.HasValue
? config.Interval.Value
: defaultWaitTime);
}
Log.Gen.info("PubSubGarbageCollectorService STOPPED");
}
}
}