Skip to content

Commit 7664d35

Browse files
committed
Added the ability to set an interval on the heartbeat plugin
1 parent 38a6b78 commit 7664d35

File tree

1 file changed

+14
-4
lines changed

1 file changed

+14
-4
lines changed

Source/Shared/Plugins/Default/100_HeartbeatPlugin.cs

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,15 @@ namespace Exceptionless.Plugins.Default {
77
[Priority(100)]
88
public class HeartbeatPlugin : IEventPlugin, IDisposable {
99
private SessionHeartbeat _heartbeat;
10+
private readonly TimeSpan _interval;
11+
12+
/// <summary>
13+
/// Controls whether session heartbeats are sent.
14+
/// </summary>
15+
/// <param name="interval">The interval at which heartbeats are sent after the last sent event. The default is 30 seconds.</param>
16+
public HeartbeatPlugin(TimeSpan? interval = null) {
17+
_interval = interval.HasValue && interval.Value.Ticks > 0 ? interval.Value : TimeSpan.FromSeconds(30);
18+
}
1019

1120
public void Run(EventPluginContext context) {
1221
if (context.Event.IsSessionHeartbeat())
@@ -26,12 +35,12 @@ public void Run(EventPluginContext context) {
2635
return;
2736

2837
if (_heartbeat == null) {
29-
_heartbeat = new SessionHeartbeat(user, context.Client);
38+
_heartbeat = new SessionHeartbeat(user, _interval, context.Client);
3039
} else if (_heartbeat.User.Identity != user.Identity) {
3140
if (_heartbeat != null)
3241
_heartbeat.Dispose();
3342

34-
_heartbeat = new SessionHeartbeat(user, context.Client);
43+
_heartbeat = new SessionHeartbeat(user, _interval, context.Client);
3544
} else {
3645
if (_heartbeat != null)
3746
_heartbeat.DelayNext();
@@ -48,11 +57,12 @@ public void Dispose() {
4857

4958
public class SessionHeartbeat : IDisposable {
5059
private readonly Timer _timer;
51-
private readonly int _interval = 30 * 1000;
60+
private readonly TimeSpan _interval;
5261
private readonly ExceptionlessClient _client;
5362

54-
public SessionHeartbeat(UserInfo user, ExceptionlessClient client) {
63+
public SessionHeartbeat(UserInfo user, TimeSpan interval, ExceptionlessClient client) {
5564
User = user;
65+
_interval = interval;
5666
_client = client;
5767
_timer = new Timer(SendHeartbeat, null, _interval, _interval);
5868
}

0 commit comments

Comments
 (0)