Minor style changes to tenor/speed-up-date

- Removed some code required to support sub-second timer intervals
This commit is contained in:
Stephen Halter 2015-12-02 16:49:31 -08:00
parent 6251b8f3e6
commit b84d8dbd07
1 changed files with 3 additions and 36 deletions

View File

@ -26,8 +26,6 @@ namespace Microsoft.AspNet.Server.Kestrel.Http
private volatile bool _hadRequestsSinceLastTimerTick = false; private volatile bool _hadRequestsSinceLastTimerTick = false;
private Timer _dateValueTimer; private Timer _dateValueTimer;
private long _lastRequestSeenTicks; private long _lastRequestSeenTicks;
private long _lastReadDateTimeTicks;
private readonly bool _is64BitSystem;
private volatile bool _timerIsRunning; private volatile bool _timerIsRunning;
/// <summary> /// <summary>
@ -51,7 +49,6 @@ namespace Microsoft.AspNet.Server.Kestrel.Http
_timeWithoutRequestsUntilIdle = timeWithoutRequestsUntilIdle; _timeWithoutRequestsUntilIdle = timeWithoutRequestsUntilIdle;
_timerInterval = timerInterval; _timerInterval = timerInterval;
_dateValueTimer = new Timer(TimerLoop, state: null, dueTime: Timeout.Infinite, period: Timeout.Infinite); _dateValueTimer = new Timer(TimerLoop, state: null, dueTime: Timeout.Infinite, period: Timeout.Infinite);
_is64BitSystem = IntPtr.Size >= 8;
} }
/// <summary> /// <summary>
@ -73,7 +70,6 @@ namespace Microsoft.AspNet.Server.Kestrel.Http
return _activeDateBytes ? _dateBytes0 : _dateBytes1; return _activeDateBytes ? _dateBytes0 : _dateBytes1;
} }
/// <summary> /// <summary>
/// Releases all resources used by the current instance of <see cref="DateHeaderValueManager"/>. /// Releases all resources used by the current instance of <see cref="DateHeaderValueManager"/>.
/// </summary> /// </summary>
@ -95,7 +91,6 @@ namespace Microsoft.AspNet.Server.Kestrel.Http
} }
} }
/// <summary> /// <summary>
/// Starts the timer /// Starts the timer
/// </summary> /// </summary>
@ -103,7 +98,6 @@ namespace Microsoft.AspNet.Server.Kestrel.Http
{ {
var now = _systemClock.UtcNow; var now = _systemClock.UtcNow;
SetDateValues(now); SetDateValues(now);
WriteLongThreadSafe(ref _lastReadDateTimeTicks, now.Ticks);
if (!_isDisposed) if (!_isDisposed)
{ {
@ -140,26 +134,19 @@ namespace Microsoft.AspNet.Server.Kestrel.Http
private void TimerLoop(object state) private void TimerLoop(object state)
{ {
var now = _systemClock.UtcNow; var now = _systemClock.UtcNow;
var lastReadDateTime = new DateTimeOffset(ReadLongThreadSafe(ref _lastReadDateTimeTicks), TimeSpan.Zero);
//Are we in a new second? SetDateValues(now);
if (now - lastReadDateTime >= TimeSpan.FromSeconds(1) || now.Second != lastReadDateTime.Second)
{
//Yep, Update DateValues
SetDateValues(now);
WriteLongThreadSafe(ref _lastReadDateTimeTicks, now.Ticks);
}
if (_hadRequestsSinceLastTimerTick) if (_hadRequestsSinceLastTimerTick)
{ {
// We served requests since the last tick, reset the flag and return as we're still active // We served requests since the last tick, reset the flag and return as we're still active
_hadRequestsSinceLastTimerTick = false; _hadRequestsSinceLastTimerTick = false;
WriteLongThreadSafe(ref _lastRequestSeenTicks, now.Ticks); Interlocked.Exchange(ref _lastRequestSeenTicks, now.Ticks);
return; return;
} }
// No requests since the last timer tick, we need to check if we're beyond the idle threshold // No requests since the last timer tick, we need to check if we're beyond the idle threshold
if ((now.Ticks - ReadLongThreadSafe(ref _lastRequestSeenTicks)) >= _timeWithoutRequestsUntilIdle.Ticks) if ((now.Ticks - Interlocked.Read(ref _lastRequestSeenTicks)) >= _timeWithoutRequestsUntilIdle.Ticks)
{ {
// No requests since idle threshold so stop the timer if it's still running // No requests since idle threshold so stop the timer if it's still running
StopTimer(); StopTimer();
@ -184,7 +171,6 @@ namespace Microsoft.AspNet.Server.Kestrel.Http
} }
} }
/// <summary> /// <summary>
/// Sets date values from a provided ticks value /// Sets date values from a provided ticks value
/// </summary> /// </summary>
@ -196,24 +182,5 @@ namespace Microsoft.AspNet.Server.Kestrel.Http
Encoding.ASCII.GetBytes(_dateValue, 0, _dateValue.Length, !_activeDateBytes ? _dateBytes0 : _dateBytes1, "\r\nDate: ".Length); Encoding.ASCII.GetBytes(_dateValue, 0, _dateValue.Length, !_activeDateBytes ? _dateBytes0 : _dateBytes1, "\r\nDate: ".Length);
_activeDateBytes = !_activeDateBytes; _activeDateBytes = !_activeDateBytes;
} }
private void WriteLongThreadSafe(ref long location, long value)
{
Interlocked.Exchange(ref location, value);
}
private long ReadLongThreadSafe(ref long location)
{
if (_is64BitSystem)
{
return location;
}
else
{
return Interlocked.Read(ref location);
}
}
} }
} }