aboutsummaryrefslogtreecommitdiffstats
path: root/src/slskd/Common
diff options
context:
space:
mode:
authorJP Dillingham <jp@dillingham.me>2025-08-03 15:44:41 -0500
committerJP Dillingham <jp@dillingham.me>2025-08-03 15:44:41 -0500
commit39bb1b9cc7f1e9afcefa535f4dcc2344fa023da1 (patch)
treed38aace784a3f111a79fe5a11374f6c354a9da1c /src/slskd/Common
parent4759027023ded889c32940828db1ccfe69f5e52b (diff)
downloadslskd-39bb1b9cc7f1e9afcefa535f4dcc2344fa023da1.tar.gz
add CurrentRequestReceiveRate metric and TimedCounter to implement it
Diffstat (limited to 'src/slskd/Common')
-rw-r--r--src/slskd/Common/TimedCounter.cs89
1 files changed, 89 insertions, 0 deletions
diff --git a/src/slskd/Common/TimedCounter.cs b/src/slskd/Common/TimedCounter.cs
new file mode 100644
index 00000000..c65ca7e3
--- /dev/null
+++ b/src/slskd/Common/TimedCounter.cs
@@ -0,0 +1,89 @@
+// <copyright file="TimedCounter.cs" company="slskd Team">
+// Copyright (c) slskd Team. All rights reserved.
+//
+// This program is free software: you can redistribute it and/or modify
+// it under the terms of the GNU Affero General Public License as published
+// by the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+//
+// This program is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU Affero General Public License for more details.
+//
+// You should have received a copy of the GNU Affero General Public License
+// along with this program. If not, see https://www.gnu.org/licenses/.
+// </copyright>
+
+using System;
+using System.Timers;
+
+namespace slskd
+{
+ /// <summary>
+ /// A counter that counts up over a set time period.
+ /// </summary>
+ public class TimedCounter : IDisposable
+ {
+ /// <summary>
+ /// Initializes a new instance of the <see cref="TimedCounter"/> class.
+ /// </summary>
+ /// <param name="interval">The interval at which the OnElapsed delegate is called and the count resets.</param>
+ /// <param name="onElapsed">The delagate to invoke with the current count when the timer elapses.</param>
+ public TimedCounter(TimeSpan interval, Action<long> onElapsed)
+ {
+ OnElapsed = onElapsed;
+
+ Timer = new Timer(interval);
+ Timer.Elapsed += Elapsed;
+ Timer.Start();
+ }
+
+ /// <summary>
+ /// Gets the current count.
+ /// </summary>
+ public long Count { get; private set; } = 0;
+
+ private Timer Timer { get; }
+ private Action<long> OnElapsed { get; }
+ private bool Disposed { get; set; }
+
+ /// <summary>
+ /// Counts up by the specified <paramref name="count"/>.
+ /// </summary>
+ /// <param name="count">The number to add to the count.</param>
+ public void CountUp(long count = 1)
+ {
+ Count += count;
+ }
+
+ /// <summary>
+ /// Disposes this instance.
+ /// </summary>
+ public void Dispose()
+ {
+ Dispose(true);
+ GC.SuppressFinalize(this);
+ }
+
+ protected virtual void Dispose(bool disposing)
+ {
+ if (!Disposed)
+ {
+ if (disposing)
+ {
+ Timer.Dispose();
+ }
+
+ Disposed = true;
+ }
+ }
+
+ private void Elapsed(object sender, ElapsedEventArgs args)
+ {
+ var count = Count;
+ Count = 0;
+ OnElapsed?.Invoke(count);
+ }
+ }
+}