@@ -10,6 +10,7 @@ namespace Exceptionless.Plugins.Default {
10
10
public class DuplicateCheckerPlugin : IEventPlugin , IDisposable {
11
11
private readonly ConcurrentQueue < Tuple < int , DateTimeOffset > > _processed = new ConcurrentQueue < Tuple < int , DateTimeOffset > > ( ) ;
12
12
private readonly ConcurrentQueue < MergedEvent > _mergedEvents = new ConcurrentQueue < MergedEvent > ( ) ;
13
+ private readonly object _lock = new object ( ) ;
13
14
private readonly TimeSpan _interval ;
14
15
private Timer _timer ;
15
16
@@ -31,40 +32,47 @@ public void Run(EventPluginContext context) {
31
32
int hashCode = context . Event . GetHashCode ( ) ;
32
33
int count = context . Event . Count ?? 1 ;
33
34
context . Log . FormattedTrace ( typeof ( DuplicateCheckerPlugin ) , String . Concat ( "Checking event: " , context . Event . Message , " with hash: " , hashCode ) ) ;
35
+
36
+ lock ( _lock ) {
37
+ context . Log . Debug ( "Got Lock on thread" + Thread . CurrentThread . ManagedThreadId ) ;
34
38
35
- // Increment the occurrence count if the event is already queued for submission.
36
- var merged = _mergedEvents . FirstOrDefault ( s => s . HashCode == hashCode ) ;
37
- if ( merged != null ) {
38
- merged . IncrementCount ( count ) ;
39
- context . Log . FormattedInfo ( typeof ( DuplicateCheckerPlugin ) , String . Concat ( "Ignoring duplicate event with hash:" , hashCode ) ) ;
40
- context . Cancel = true ;
41
- return ;
42
- }
39
+ // Increment the occurrence count if the event is already queued for submission.
40
+ var merged = _mergedEvents . FirstOrDefault ( s => s . HashCode == hashCode ) ;
41
+ if ( merged != null ) {
42
+ merged . IncrementCount ( count ) ;
43
+ context . Log . FormattedInfo ( typeof ( DuplicateCheckerPlugin ) , String . Concat ( "Ignoring duplicate event with hash:" , hashCode ) ) ;
44
+ context . Cancel = true ;
45
+ return ;
46
+ }
47
+
48
+ DateTimeOffset repeatWindow = DateTimeOffset . UtcNow . Subtract ( _interval ) ;
49
+ if ( _processed . Any ( s => s . Item1 == hashCode && s . Item2 >= repeatWindow ) ) {
50
+ context . Log . FormattedInfo ( typeof ( DuplicateCheckerPlugin ) , String . Concat ( "Adding event with hash:" , hashCode , " to cache." ) ) ;
51
+ // This event is a duplicate for the first time, lets save it so we can delay it while keeping count
52
+ _mergedEvents . Enqueue ( new MergedEvent ( hashCode , context , count ) ) ;
53
+ context . Cancel = true ;
54
+ return ;
55
+ }
43
56
44
- DateTimeOffset repeatWindow = DateTimeOffset . UtcNow . Subtract ( _interval ) ;
45
- if ( _processed . Any ( s => s . Item1 == hashCode && s . Item2 >= repeatWindow ) ) {
46
- context . Log . FormattedInfo ( typeof ( DuplicateCheckerPlugin ) , String . Concat ( "Adding event with hash:" , hashCode , " to cache." ) ) ;
47
- // This event is a duplicate for the first time, lets save it so we can delay it while keeping count
48
- _mergedEvents . Enqueue ( new MergedEvent ( hashCode , context , count ) ) ;
49
- context . Cancel = true ;
50
- } else {
51
57
context . Log . FormattedInfo ( typeof ( DuplicateCheckerPlugin ) , String . Concat ( "Enqueueing event with hash:" , hashCode , " to cache." ) ) ;
52
58
_processed . Enqueue ( Tuple . Create ( hashCode , DateTimeOffset . UtcNow ) ) ;
59
+
60
+ Tuple < int , DateTimeOffset > temp ;
61
+ while ( _processed . Count > 50 )
62
+ _processed . TryDequeue ( out temp ) ;
53
63
}
54
-
55
- Tuple < int , DateTimeOffset > temp ;
56
- while ( _processed . Count > 50 )
57
- _processed . TryDequeue ( out temp ) ;
58
64
}
59
65
60
66
private void OnTimer ( object state ) {
61
67
EnqueueMergedEvents ( ) ;
62
68
}
63
69
64
70
private void EnqueueMergedEvents ( ) {
65
- MergedEvent mergedEvent ;
66
- while ( _mergedEvents . TryDequeue ( out mergedEvent ) )
67
- mergedEvent . Enqueue ( ) ;
71
+ lock ( _lock ) {
72
+ MergedEvent mergedEvent ;
73
+ while ( _mergedEvents . TryDequeue ( out mergedEvent ) )
74
+ mergedEvent . Enqueue ( ) ;
75
+ }
68
76
}
69
77
70
78
public void Dispose ( ) {
0 commit comments