File: webeventbuffer.cs

package info (click to toggle)
mono 6.8.0.105%2Bdfsg-3.3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 1,284,512 kB
  • sloc: cs: 11,172,132; xml: 2,850,069; ansic: 671,653; cpp: 122,091; perl: 59,366; javascript: 30,841; asm: 22,168; makefile: 20,093; sh: 15,020; python: 4,827; pascal: 925; sql: 859; sed: 16; php: 1
file content (647 lines) | stat: -rw-r--r-- 28,824 bytes parent folder | download | duplicates (7)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
//------------------------------------------------------------------------------
// <copyright file="WebEventBuffer.cs" company="Microsoft">
//     Copyright (c) Microsoft Corporation.  All rights reserved.
// </copyright>                                                                
//------------------------------------------------------------------------------

namespace System.Web.Management {
    using System.Configuration;
    using System.Web.Configuration;
    using System.Configuration.Provider;
    using System.Collections.Specialized;
    using System.Collections;
    using System.Web.Util;
    using System.Web.Mail;
    using System.Globalization;
    using System.Xml;
    using System.Threading;
    using System.Web.Hosting;
    using System.Security.Permissions;

    public enum EventNotificationType
    {
        // regularly scheduled notification
        Regular,
        
        // urgent notification
        Urgent,
        
        // notification triggered by a user requested flush
        Flush,

        // Notification fired when buffer=false
        Unbuffered,
    }

    internal enum FlushCallReason {
        UrgentFlushThresholdExceeded,
        Timer,
        StaticFlush
    }

    public sealed class WebEventBufferFlushInfo {
        WebBaseEventCollection  _events;
        DateTime                _lastNotification;
        int                     _eventsDiscardedSinceLastNotification;
        int                     _eventsInBuffer;
        int                     _notificationSequence;
        EventNotificationType   _notificationType;
        
        internal WebEventBufferFlushInfo(  WebBaseEventCollection events,
                                            EventNotificationType notificationType,
                                            int notificationSequence,
                                            DateTime lastNotification,
                                            int eventsDiscardedSinceLastNotification,
                                            int eventsInBuffer) {
            _events = events;
            _notificationType = notificationType;
            _notificationSequence = notificationSequence;
            _lastNotification = lastNotification;
            _eventsDiscardedSinceLastNotification = eventsDiscardedSinceLastNotification;
            _eventsInBuffer = eventsInBuffer;
        }

        public WebBaseEventCollection  Events {
            get { return _events; }
        }
        
        public DateTime LastNotificationUtc {
            get { return _lastNotification; }
        }
        
        public int EventsDiscardedSinceLastNotification {
            get { return _eventsDiscardedSinceLastNotification; }
        }
        
        public int EventsInBuffer {
            get { return _eventsInBuffer; }
        }
        
        public int NotificationSequence {
            get { return _notificationSequence; }
        }
        
        public EventNotificationType NotificationType {
            get { return _notificationType; }
        }

    }
    
    internal delegate void WebEventBufferFlushCallback(WebEventBufferFlushInfo flushInfo);

    internal sealed class WebEventBuffer {

        static long Infinite = Int64.MaxValue;
        
        long        _burstWaitTimeMs = 2 * 1000;  

        BufferedWebEventProvider    _provider;
        
        long        _regularFlushIntervalMs;
        int         _urgentFlushThreshold;
        int         _maxBufferSize;
        int         _maxFlushSize;
        long        _urgentFlushIntervalMs;
        int         _maxBufferThreads;
        
        Queue       _buffer = null;
        Timer       _timer;
        DateTime    _lastFlushTime = DateTime.MinValue;
        DateTime    _lastScheduledFlushTime = DateTime.MinValue;
        DateTime    _lastAdd = DateTime.MinValue;
        DateTime    _startTime = DateTime.MinValue;
        bool        _urgentFlushScheduled;
        int         _discardedSinceLastFlush = 0;
        int         _threadsInFlush = 0;
        int         _notificationSequence = 0;
        bool        _regularTimeoutUsed;

#if DBG
        DateTime    _nextFlush = DateTime.MinValue;
        DateTime    _lastRegularFlush = DateTime.MinValue;
        DateTime    _lastUrgentFlush = DateTime.MinValue;
        int         _totalAdded = 0;
        int         _totalFlushed = 0;
        int         _totalAbandoned = 0;
#endif        

        WebEventBufferFlushCallback _flushCallback;

        internal WebEventBuffer(BufferedWebEventProvider provider, string bufferMode,
                        WebEventBufferFlushCallback callback) {
            Debug.Assert(callback != null, "callback != null");

            _provider = provider;
            
            HealthMonitoringSection section = RuntimeConfig.GetAppLKGConfig().HealthMonitoring;

            BufferModesCollection bufferModes = section.BufferModes;

            BufferModeSettings bufferModeInfo = bufferModes[bufferMode];
            if (bufferModeInfo == null) {
                throw new ConfigurationErrorsException(SR.GetString(SR.Health_mon_buffer_mode_not_found, bufferMode));
            }

            if (bufferModeInfo.RegularFlushInterval == TimeSpan.MaxValue) {
                _regularFlushIntervalMs = Infinite;
            }
            else {
                try {
                    _regularFlushIntervalMs = (long)bufferModeInfo.RegularFlushInterval.TotalMilliseconds;
                }
                catch (OverflowException) {
                    _regularFlushIntervalMs = Infinite;
                }
            }
            
            if (bufferModeInfo.UrgentFlushInterval == TimeSpan.MaxValue) {
                _urgentFlushIntervalMs = Infinite;
            }
            else {
                try {
                    _urgentFlushIntervalMs = (long)bufferModeInfo.UrgentFlushInterval.TotalMilliseconds;
                }
                catch (OverflowException) {
                    _urgentFlushIntervalMs = Infinite;
                }
            }

            _urgentFlushThreshold = bufferModeInfo.UrgentFlushThreshold;
            _maxBufferSize = bufferModeInfo.MaxBufferSize;
            _maxFlushSize = bufferModeInfo.MaxFlushSize;
            _maxBufferThreads = bufferModeInfo.MaxBufferThreads;

            _burstWaitTimeMs = Math.Min(_burstWaitTimeMs, _urgentFlushIntervalMs);
            
            _flushCallback = callback;

            _buffer = new Queue();

            if (_regularFlushIntervalMs != Infinite) {
                _startTime = DateTime.UtcNow;
                _regularTimeoutUsed = true;
                _urgentFlushScheduled = false;
                SetTimer(GetNextRegularFlushDueTimeInMs());
            }

            Debug.Trace("WebEventBuffer",   
                        "\n_regularFlushIntervalMs=" + _regularFlushIntervalMs +
                        "\n_urgentFlushThreshold=" + _urgentFlushThreshold +
                        "\n_maxBufferSize=" + _maxBufferSize +
                        "\n_maxFlushSize=" + _maxFlushSize +
                        "\n_urgentFlushIntervalMs=" + _urgentFlushIntervalMs);
        }

        void FlushTimerCallback(object state) {
            Flush(_maxFlushSize, FlushCallReason.Timer);
        }

        //
        // If we're in notification mode, meaning urgentFlushThreshold="1", we'll flush
        // as soon as there's an event in the buffer.
        // 
        // For example, if bufferMode == "notification", we have this setting:
        //  <add name="Notification" maxBufferSize="300" maxFlushSize="20"
        //    urgentFlushThreshold="1" regularFlushInterval="Infinite" 
        //    urgentFlushInterval="00:01:00" maxBufferThreads="1" />
        //
        // The ideal situation is that we have events coming in regularly,
        // and we flush (max 20 events at a time), wait for _urgentFlushIntervalMs (1 minute), 
        // then flush the buffer, then wait 1 minute, then flush, and so on and on.
        //
        // However, there is a scenario where there's been no event coming in, and suddenly  
        // a burst of events (e.g. 20) arrive. If we flush immediately when the 1st event comes in, 
        // we then have to wait for 1 minute before we can flush the remaining 19 events.
        //
        // To solve this problem, we demand that if we're in notification mode, and
        // we just added an event to an empty buffer, then we may anticipate a burst
        // by waiting _burstWaitTimeMs amount of time (2s).
        //
        // But how long does a buffer needs to be empty before we consider
        // waiting for a burst?  We cannot come up with a good formula, and thus
        // pick this:
        //      ((now - _lastAdd).TotalMilliseconds) >= _urgentFlushIntervalMs
        // 
        bool AnticipateBurst(DateTime now) {
            // Please note this is called while we're within the lock held in AddEvent.
            return _urgentFlushThreshold == 1 &&    // we're in notification mode
                    _buffer.Count == 1 &&           // we just added an event to an empty buffer
                    ((now - _lastAdd).TotalMilliseconds) >= _urgentFlushIntervalMs;
        }

        long GetNextRegularFlushDueTimeInMs() {
            long   nextRegularFlushFromStartTime;
            long   nowFromStartTime;
            long   regularFlushIntervalms = _regularFlushIntervalMs;

            // Need to calculate in milliseconds in order to avoid time shift due to round-down
            if (_regularFlushIntervalMs == Infinite) {
                return Infinite;
            }

            DateTime    now = DateTime.UtcNow;
            nowFromStartTime = (long)((now - _startTime).TotalMilliseconds);

            // For some unknown reason the Timer may fire prematurely (usually less than 50ms).  This will bring
            // us into a situation where the timer fired just tens of milliseconds before the originally planned 
            // fire time, and this method will return a due time == tens of milliseconds.
            // To workaround this problem, I added 499 ms when doing the calculation to compensate for a
            // premature firing.
            nextRegularFlushFromStartTime = ((nowFromStartTime + regularFlushIntervalms + 499) / regularFlushIntervalms) * regularFlushIntervalms;

            Debug.Assert(nextRegularFlushFromStartTime >= nowFromStartTime);

            return nextRegularFlushFromStartTime - nowFromStartTime;
        }

        void SetTimer(long waitTimeMs) {
            if (_timer == null) {
                _timer = new System.Threading.Timer(new TimerCallback(this.FlushTimerCallback),
                                                null, waitTimeMs, Timeout.Infinite);
            }
            else {
                _timer.Change(waitTimeMs, Timeout.Infinite);
            }

#if DBG
            _nextFlush = DateTime.UtcNow.AddMilliseconds(waitTimeMs);
#endif            
        }

        // This method can be called by the timer, or by AddEvent.
        //
        // Basic design:
        // - We have one timer, and one buffer.
        // - We flush periodically every _regularFlushIntervalMs ms
        // - But if # of items in buffer has reached _urgentFlushThreshold, we will flush more frequently,
        //   but at most once every _urgentFlushIntervalMs ms.  However, these urgent flushes will not
        //   prevent the regular flush from happening.
        // - We never flush synchronously, meaning if we're called by AddEvent and decide to flush
        //   because we've reached the _urgentFlushThreshold, we will still use the timer thread
        //   to flush the buffer.
        // - At any point only a maximum of _maxBufferThreads threads can be flushing.  If exceeded,
        //   we will delay a flush.
        //
        //

        // For example, let's say we have this setting:
        // "1 minute urgentFlushInterval and 5 minute regularFlushInterval"
        //
        // Assume regular flush timer starts at 10:00am.  It means regular 
        // flush will happen at 10:05am, 10:10am, 10:15am, and so on, 
        // regardless of when urgent flush happens.  
        // 
        // An "urgent flush" happens whenever urgentFlushThreshold is reached.
        // However, when we schedule an "urgent flush", we ensure that the time
        // between an urgent flush and the last flush (no matter it's urgent or
        // regular) will be at least urgentFlushInterval.
        //
        // One interesting case here.  Assume at 10:49:30 we had an urgent 
        // flush, but the # of events left is still above urgentFlushThreshold.
        // You may think we'll schedule the next urgent flush at 10:50:30
        // (urgentFlushInterval == 1 min).  However, because we know we will 
        // have a regular flush at 10:50:00, we won't schedule the next urgent
        // flush.  Instead, during the regular flush at 10:50:00 happens, we'll
        // check if there're still too many events; and if so, we will schedule
        // the next urgent flush at 10:51:00.
        //
        internal void Flush(int max, FlushCallReason reason) {
            WebBaseEvent[]  events = null;
            DateTime    nowUtc = DateTime.UtcNow;
            long        waitTime = 0;
            DateTime    lastFlushTime = DateTime.MaxValue;
            int         discardedSinceLastFlush = -1;
            int         eventsInBuffer = -1;
            int         toFlush = 0;
            EventNotificationType   notificationType = EventNotificationType.Regular;

            // By default, this call will flush, but will not schedule the next flush.
            bool        flushThisTime = true;
            bool        scheduleNextFlush = false;
            bool        nextFlushIsUrgent = false;

            lock(_buffer) {
                Debug.Assert(max > 0, "max > 0");

                if (_buffer.Count == 0) {
                    // We have nothing in the buffer.  Don't flush this time.
                    Debug.Trace("WebEventBufferExtended", "Flush: buffer is empty, don't flush");
                    flushThisTime = false;
                }

                switch (reason) {
                case FlushCallReason.StaticFlush:
                    // It means somebody calls provider.Flush()
                    break;

                case FlushCallReason.Timer:
                    // It's a callback from a timer.  We will schedule the next regular flush if needed.
                    
                    if (_regularFlushIntervalMs != Infinite) {
                        scheduleNextFlush = true;
                        waitTime = GetNextRegularFlushDueTimeInMs();
                    }
                    break;

                case FlushCallReason.UrgentFlushThresholdExceeded:
                    // It means this method is called by AddEvent because the urgent flush threshold is reached.
                    
                    // If an urgent flush has already been scheduled by someone else, we don't need to duplicate the
                    // effort.  Just return.
                    if (_urgentFlushScheduled) {
                        return;
                    }

                    // Flush triggered by AddEvent isn't synchronous, so we won't flush this time, but will 
                    // schedule an urgent flush instead.
                    flushThisTime = false;      
                    scheduleNextFlush = true;
                    nextFlushIsUrgent = true;         

                    // Calculate how long we have to wait when scheduling the flush
                    if (AnticipateBurst(nowUtc)) {
                        Debug.Trace("WebEventBuffer", "Flush: Called by AddEvent.  Waiting for burst");
                        waitTime = _burstWaitTimeMs;
                    }
                    else {
                        Debug.Trace("WebEventBuffer", "Flush: Called by AddEvent.  Schedule an immediate flush");
                        waitTime = 0;
                    }
                    
                    // Have to wait longer because of _urgentFlushIntervalMs
                    long    msSinceLastScheduledFlush = (long)(nowUtc - _lastScheduledFlushTime).TotalMilliseconds;
                    if (msSinceLastScheduledFlush + waitTime < _urgentFlushIntervalMs ) {
                        
                        Debug.Trace("WebEventBuffer", "Flush: Called by AddEvent.  Have to wait longer because of _urgentFlushIntervalMs.");
                        waitTime = _urgentFlushIntervalMs - msSinceLastScheduledFlush;
                    }
                    
                    Debug.Trace("WebEventBuffer", "Wait time=" + waitTime +
                        "; nowUtc=" + PrintTime(nowUtc) +
                        "; _lastScheduledFlushTime=" + PrintTime(_lastScheduledFlushTime) + 
                        "; _urgentFlushIntervalMs=" + _urgentFlushIntervalMs);
                    
                    break;
                }
                
                Debug.Trace("WebEventBuffer", "Flush called: max=" + max + 
                    "; reason=" + reason);
                    
                if (flushThisTime) {
                    // Check if we've exceeded the # of flushing threads.  If so,
                    // don't flush this time.
                    
                    if (_threadsInFlush >= _maxBufferThreads) {
                        // Won't set flushThisTime to false because we depend on
                        // the logic inside the next "if" block to schedule the
                        // next urgent flush as needed.
                        toFlush = 0;
                    }
                    else {
                        toFlush = Math.Min(_buffer.Count, max);
                    }
                }
                
#if DBG
                DebugUpdateStats(flushThisTime, nowUtc, toFlush, reason);
#endif

                if (flushThisTime) {
                    Debug.Assert(reason != FlushCallReason.UrgentFlushThresholdExceeded, "reason != FlushCallReason.UrgentFlushThresholdExceeded");

                    if (toFlush > 0) {
                        // Move the to-be-flushed events to an array                
                        events = new WebBaseEvent[toFlush];

                        for (int i = 0; i < toFlush; i++) {
                            events[i] = (WebBaseEvent)_buffer.Dequeue();
                        }

                        lastFlushTime = _lastFlushTime;

                        // Update _lastFlushTime and _lastScheduledFlushTime.
                        // These information are used when Flush is called the next time.
                        _lastFlushTime = nowUtc;
                        if (reason == FlushCallReason.Timer) {
                            _lastScheduledFlushTime = nowUtc;
                        }

                        discardedSinceLastFlush = _discardedSinceLastFlush;
                        _discardedSinceLastFlush = 0;

                        if (reason == FlushCallReason.StaticFlush) {
                            notificationType = EventNotificationType.Flush;
                        }
                        else {
                            Debug.Assert(!(!_regularTimeoutUsed && !_urgentFlushScheduled),
                                "It's impossible to have a non-regular flush and yet the flush isn't urgent");

                            notificationType = _regularTimeoutUsed ? 
                                               EventNotificationType.Regular :
                                               EventNotificationType.Urgent;
                        }
                    }

                    eventsInBuffer = _buffer.Count;

                    // If we still have at least _urgentFlushThreshold left, set timer
                    // to flush asap.
                    if (eventsInBuffer >= _urgentFlushThreshold) {
                        Debug.Trace("WebEventBuffer", "Flush: going to flush " + toFlush + " events, but still have at least _urgentFlushThreshold left. Schedule a flush");
                        scheduleNextFlush = true;
                        nextFlushIsUrgent = true;
                        waitTime = _urgentFlushIntervalMs;
                    }
                    else {
                        Debug.Trace("WebEventBuffer", "Flush: going to flush " + toFlush + " events");
                    }
                }

                // We are done moving the flushed events to the 'events' array.  
                // Now schedule the next flush if needed.

                _urgentFlushScheduled = false;
                
                if (scheduleNextFlush) {
                    if (nextFlushIsUrgent) {
                        long nextRegular = GetNextRegularFlushDueTimeInMs();

                        // If next regular flush is closer than next urgent flush,
                        // use regular flush instead.
                        if (nextRegular < waitTime) {
                            Debug.Trace("WebEventBuffer", "Switch to use regular timeout");
                            waitTime = nextRegular;
                            _regularTimeoutUsed = true;
                        }
                        else {
                            _regularTimeoutUsed = false;
                        }
                    }
                    else {
                        _regularTimeoutUsed = true;
                    }
                    
                    SetTimer(waitTime);
                    _urgentFlushScheduled = nextFlushIsUrgent;
#if DBG
                    Debug.Trace("WebEventBuffer", "Flush: Registered for a flush.  Waittime = " + waitTime + "ms" +
                        "; _nextFlush=" + PrintTime(_nextFlush) +
                        "; _urgentFlushScheduled=" + _urgentFlushScheduled);
#endif

                }

                // Cleanup.  If we are called by a timer callback, but we haven't scheduled for the next
                // one (can only happen if _regularFlushIntervalMs == Infinite), we should dispose the timer
                if (reason == FlushCallReason.Timer && !scheduleNextFlush) {
                    Debug.Trace("WebEventBuffer", "Flush: Disposing the timer");
                    Debug.Assert(_regularFlushIntervalMs == Infinite, "We can dispose the timer only if _regularFlushIntervalMs == Infinite");
                    ((IDisposable)_timer).Dispose();
                    _timer = null;
                    _urgentFlushScheduled = false;
                }

                // We want to increment the thread count within the lock to ensure we don't let too many threads in
                if (events != null) {
                    Interlocked.Increment(ref _threadsInFlush);
                }
            } // Release lock

            // Now call the providers to flush the events
            if (events != null) {
                Debug.Assert(lastFlushTime != DateTime.MaxValue, "lastFlushTime != DateTime.MaxValue");
                Debug.Assert(discardedSinceLastFlush != -1, "discardedSinceLastFlush != -1");
                Debug.Assert(eventsInBuffer != -1, "eventsInBuffer != -1");

                Debug.Trace("WebEventBufferSummary", "_threadsInFlush=" + _threadsInFlush);

                using (new ApplicationImpersonationContext()) {
                    try {
                        WebEventBufferFlushInfo flushInfo = new WebEventBufferFlushInfo(
                                                                new WebBaseEventCollection(events),
                                                                notificationType,
                                                                Interlocked.Increment(ref _notificationSequence),
                                                                lastFlushTime,
                                                                discardedSinceLastFlush,
                                                                eventsInBuffer);

                        _flushCallback(flushInfo);
                    }
                    catch (Exception e) {
                        try {
                            _provider.LogException(e);
                        }
                        catch {
                            // Ignore all errors
                        }
                    }
#pragma warning disable 1058
                    catch { // non compliant exceptions are caught and logged as Unknown
                        try {
                            _provider.LogException(new Exception(SR.GetString(SR.Provider_Error)));
                        }
                        catch {
                            // Ignore all errors
                        }
                    }
#pragma warning restore 1058
                }

                Interlocked.Decrement(ref _threadsInFlush);
            }
        }
        
        internal void AddEvent(WebBaseEvent webEvent) {
            lock(_buffer) {
#if DBG
                _totalAdded++;
#endif
                // If we have filled up the buffer, remove items using FIFO order.
                if (_buffer.Count == _maxBufferSize) {
                    Debug.Trace("WebEventBuffer", "Buffer is full.  Need to remove one from the tail");
                    _buffer.Dequeue();
                    _discardedSinceLastFlush++;
#if DBG
                    _totalAbandoned++;
#endif
                }

                _buffer.Enqueue(webEvent);
                
                // If we have at least _urgentFlushThreshold, flush.  Please note the flush is async.
                if (_buffer.Count >= _urgentFlushThreshold) {
                    Flush(_maxFlushSize, FlushCallReason.UrgentFlushThresholdExceeded);
                }
                
                // Note that Flush uses _lastAdd, which is the time an event (not including this one) 
                // was last added.  That's why we call it after calling Flush.
                _lastAdd = DateTime.UtcNow;
            }   // Release the lock
        }

        internal void Shutdown() {
            if (_timer != null) {
                _timer.Dispose();
                _timer = null;
            }
        }

        string PrintTime(DateTime t) {
            return t.ToString("T", DateTimeFormatInfo.InvariantInfo) + "." + t.Millisecond.ToString("d03", CultureInfo.InvariantCulture);
        }


#if DBG
        void DebugUpdateStats(bool flushThisTime, DateTime now, int toFlush, FlushCallReason reason) {
            Debug.Assert(_totalAdded == _totalAbandoned + _totalFlushed + _buffer.Count, 
                    "_totalAdded == _totalAbandoned + _totalFlushed + _buffer.Count");
            
            _totalFlushed += toFlush;
    
            if (reason != FlushCallReason.Timer) {
                return;
            }
            
            Debug.Trace("WebEventBufferSummary", 
                "_Added=" + _totalAdded + "; deleted=" + _totalAbandoned +
                "; Flushed=" + _totalFlushed + "; buffer=" + _buffer.Count +
                "; toFlush=" + toFlush +
                "; lastFlush=" + PrintTime(_lastRegularFlush) +
                "; lastUrgentFlush=" + PrintTime(_lastUrgentFlush) +
                "; GetRegFlushDueTime=" + GetNextRegularFlushDueTimeInMs() +
                "; toFlush=" + toFlush +
                "; now=" + PrintTime(now));

            if (!_regularTimeoutUsed) {
                if (!flushThisTime) {
                    return;
                }
                
                Debug.Assert((now - _lastUrgentFlush).TotalMilliseconds + 499 > _urgentFlushIntervalMs, 
                    "(now - _lastUrgentFlush).TotalMilliseconds + 499 > _urgentFlushIntervalMs" +
                    "\n_lastUrgentFlush=" + PrintTime(_lastUrgentFlush) +
                    "\nnow=" + PrintTime(now) +
                    "\n_urgentFlushIntervalMs=" + _urgentFlushIntervalMs);
                
                _lastUrgentFlush = now;
            }
            else {
                /*
                // It's a regular callback
                if (_lastRegularFlush != DateTime.MinValue) {
                    Debug.Assert(Math.Abs((now - _lastRegularFlush).TotalMilliseconds - _regularFlushIntervalMs) < 2000, 
                        "Math.Abs((now - _lastRegularFlush).TotalMilliseconds - _regularFlushIntervalMs) < 2000" +
                        "\n_lastRegularFlush=" + PrintTime(_lastRegularFlush) +
                        "\nnow=" + PrintTime(now) +
                        "\n_regularFlushIntervalMs=" + _regularFlushIntervalMs);
                }
                */
                
                _lastRegularFlush = now;
            }
        }

#endif
    }
}