File: RequestTimeoutManager.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 (191 lines) | stat: -rw-r--r-- 5,897 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
//------------------------------------------------------------------------------
// <copyright file="RequestTimeoutManager.cs" company="Microsoft">
//     Copyright (c) Microsoft Corporation.  All rights reserved.
// </copyright>                                                                
//------------------------------------------------------------------------------

/*
 * Request timeout manager -- implements the request timeout mechanism
 */
namespace System.Web {
    using System.Threading;
    using System.Collections;
    using System.Web.Util;

    internal class RequestTimeoutManager {
        private int                 _requestCount;
        private DoubleLinkList[]    _lists;           // partitioned to avoid contention
        private int                 _currentList;
        private int                 _inProgressLock;  // only 1 thread can be cancelling
        private readonly TimeSpan   _timerPeriod = new TimeSpan(0, 0, 15); // 15 second init precision
        private Timer               _timer;

        internal RequestTimeoutManager() {
            // initialize request lists

            _requestCount = 0;

            _lists = new DoubleLinkList[13];
            for (int i = 0; i < _lists.Length; i++)
                _lists[i] = new DoubleLinkList();
            _currentList = 0;

            // init lock

            _inProgressLock = 0;

            // create the timer

#if DBG
            if (!Debug.IsTagPresent("Timer") || Debug.IsTagEnabled("Timer"))
#endif
            {
                _timer = new Timer(new TimerCallback(this.TimerCompletionCallback), null, _timerPeriod, _timerPeriod);
            }

        }

        internal void Stop() {
            // stop the timer

            if (_timer != null) {
                ((IDisposable)_timer).Dispose();
                _timer = null;
            }

            while (_inProgressLock != 0)
                Thread.Sleep(100);

            // cancel all cancelable requests

            if (_requestCount > 0)
                CancelTimedOutRequests(DateTime.UtcNow.AddYears(1)); // future date
        }

        private void TimerCompletionCallback(Object state) {
            if (_requestCount > 0)
                CancelTimedOutRequests(DateTime.UtcNow);
        }

        private void CancelTimedOutRequests(DateTime now) {

            // only one thread can be doing it

            if (Interlocked.CompareExchange(ref _inProgressLock, 1, 0) != 0)
                return;

            // collect them all into a separate list with minimal locking

            ArrayList entries = new ArrayList(_requestCount); // size can change
            DoubleLinkListEnumerator en;

            for (int i = 0; i < _lists.Length; i++) {
                lock (_lists[i]) {
                    en = _lists[i].GetEnumerator();

                    while (en.MoveNext())
                        entries.Add(en.GetDoubleLink());

                    en = null;
                }
            }

            // walk through the collected list to timeout what's needed

            int n = entries.Count;

            for (int i = 0; i < n; i++)
                ((RequestTimeoutEntry)entries[i]).TimeoutIfNeeded(now);

            // this thread is done -- unlock

            Interlocked.Exchange(ref _inProgressLock, 0);
        }

        internal void Add(HttpContext context) {
            if (context.TimeoutLink != null) {
                ((RequestTimeoutEntry)context.TimeoutLink).IncrementCount();
                return;
            }
           
            // create new entry

            RequestTimeoutEntry entry = new RequestTimeoutEntry(context);

            // add it to the list

            int i = _currentList++;
            if (i >= _lists.Length) {
                i = 0;
                _currentList = 0;
            }

            entry.AddToList(_lists[i]);
            Interlocked.Increment(ref _requestCount);

            // update HttpContext
            context.TimeoutLink = entry;
        }

        internal void Remove(HttpContext context) {
            RequestTimeoutEntry entry = (RequestTimeoutEntry)context.TimeoutLink;

            // remove from the list
            if (entry != null) {
                if( entry.DecrementCount() == 0 ) {
                    entry.RemoveFromList();
                    Interlocked.Decrement(ref _requestCount);
                } else {
                    return;
                }
            }

            // update HttpContext
            context.TimeoutLink = null;
        }

        private class RequestTimeoutEntry : DoubleLink {
            private  HttpContext    _context;   // the request
            private  DoubleLinkList _list;
            private int _count;

            internal RequestTimeoutEntry(HttpContext context) {
                _context = context;
                _count = 1;
            }

            internal void AddToList(DoubleLinkList list) {
                lock(list) {
                    list.InsertTail(this);
                    _list = list;
                }
            }

            internal void RemoveFromList() {
                if (_list != null) {
                    lock(_list) {
                        Remove();
                        _list = null;
                    }
                }
            }

            internal void TimeoutIfNeeded(DateTime now) {
                Thread thread = _context.MustTimeout(now);
                if (thread != null) {
                    RemoveFromList();
                    thread.Abort(new HttpApplication.CancelModuleException(true));
                }
            }

            internal void IncrementCount() {
                Interlocked.Increment( ref _count );
            }

            internal int DecrementCount() {
                return Interlocked.Decrement( ref _count );
            }
        }
    }

}