Skip to content

Commit

Permalink
V1.1 update (#2)
Browse files Browse the repository at this point in the history
V1.1 update
  • Loading branch information
Jinhuafei authored Jul 12, 2017
1 parent e8037c6 commit 1630641
Show file tree
Hide file tree
Showing 25 changed files with 1,597 additions and 522 deletions.
16 changes: 16 additions & 0 deletions src/SessionStateModule/AppSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,12 @@ private static void LoadSettings(NameValueCollection appSettings)
{
_requestQueueLimitPerSession = DefaultRequestQueueLimitPerSession;
}

//
// AllowConcurrentRequests
//
string allowConcurrentRequestPerSession = appSettings["aspnet:AllowConcurrentRequestsPerSession"];
bool.TryParse(allowConcurrentRequestPerSession, out _allowConcurrentRequestsPerSession);
}

private static void EnsureSettingsLoaded()
Expand Down Expand Up @@ -62,5 +68,15 @@ public static int RequestQueueLimitPerSession
return _requestQueueLimitPerSession;
}
}

private static bool _allowConcurrentRequestsPerSession = false;
public static bool AllowConcurrentRequestsPerSession
{
get
{
EnsureSettingsLoaded();
return _allowConcurrentRequestsPerSession;
}
}
}
}
52 changes: 3 additions & 49 deletions src/SessionStateModule/InProcSessionStateStoreAsync.cs
Original file line number Diff line number Diff line change
Expand Up @@ -161,36 +161,7 @@ public override Task RemoveItemAsync(
throw new ArgumentException(SR.Session_id_too_long);
}

var lockCookie = (int)lockId;
var state = (InProcSessionState)s_store.Get(id);

if(state != null)
{
bool lockTaken = false;

try
{
state.SpinLock.Enter(ref lockTaken);

// Only remove the item if we are the owner
if(!state.Locked || state.LockCookie != lockCookie)
{
return Task.CompletedTask;
}

// prevent overwriting when we drop the lock
state.LockCookie = 0;
}
finally
{
if(lockTaken)
{
state.SpinLock.Exit();
}
}

s_store.Remove(id);
}
s_store.Remove(id);

return Task.CompletedTask;
}
Expand Down Expand Up @@ -253,11 +224,6 @@ public override Task SetAndReleaseItemExclusiveAsync(
try
{
currentState.SpinLock.Enter(ref lockTaken);
// Only set the state if we are the owner
if(!currentState.Locked || currentState.LockCookie != lockCookie)
{
return Task.CompletedTask;
}

// we can change the state in place if the timeout hasn't changed
if(currentState.Timeout == item.Timeout)
Expand All @@ -278,14 +244,7 @@ Please note that the item itself should not expire between now and
updated its expiry time.
*/
currentState.Flags |= (int)SessionStateItemFlags.IgnoreCacheItemRemoved;

/* By setting _lockCookie to 0, we prevent an overwriting by ReleaseExclusive
when we drop the lock.
The scenario can happen if another request is polling and trying to prempt
the lock we have on the item.
*/
lockCookieForInsert = lockCookie;
currentState.LockCookie = 0;
}
}
finally
Expand Down Expand Up @@ -346,12 +305,7 @@ private Task<GetItemResult> DoGetAsync(HttpContextBase context, string id, bool
SessionStateActions actionFlags;

var item = DoGet(context, id, exclusive, out locked, out lockAge, out lockId, out actionFlags);
GetItemResult result = null;

if (item != null)
{
result = new GetItemResult(item, locked, lockAge, lockId, actionFlags);
}
GetItemResult result = new GetItemResult(item, locked, lockAge, lockId, actionFlags);

return Task.FromResult<GetItemResult>(result);
}
Expand Down Expand Up @@ -472,7 +426,7 @@ private void InsertToCache(string key, InProcSessionState value)
RemovedCallback = _callback,
Priority = CacheItemPriority.NotRemovable
};
s_store.Add(key, value, cachePolicy);
s_store.Set(key, value, cachePolicy);
}

private SessionStateStoreData CreateLegitStoreData(
Expand Down
30 changes: 12 additions & 18 deletions src/SessionStateModule/SessionStateModuleAsync.cs
Original file line number Diff line number Diff line change
Expand Up @@ -576,7 +576,11 @@ private async Task CompleteAcquireStateAsync(HttpContext context)
OnStart(EventArgs.Empty);
}

RegisterEnsureStateStoreItemLocked();
// lock free session doesn't need this
if(!AppSettings.AllowConcurrentRequestsPerSession)
{
RegisterEnsureStateStoreItemLocked();
}
}
finally
{
Expand Down Expand Up @@ -653,7 +657,7 @@ private async Task GetSessionStateItemAsync()

Debug.Assert(_rqId != null, "_rqId != null");

if (_rqReadonly)
if (_rqReadonly || AppSettings.AllowConcurrentRequestsPerSession)
{
GetItemResult result = await _store.GetItemAsync(_rqContext, _rqId, GetCancellationToken());
if (result != null)
Expand Down Expand Up @@ -701,7 +705,7 @@ private async Task GetSessionStateItemAsync()
QueueRef();
isQueued = true;
}
// TODO: The retry timeout should be configurable.

if (lockAge >= _rqExecutionTimeout)
{
/* Release the lock on the item, which is held by another thread*/
Expand Down Expand Up @@ -786,15 +790,7 @@ private static void LookUpRegForPollInterval()
}
}
}

// Called by OnReleaseState to get the session id.
private string ReleaseStateGetSessionId()
{
Debug.Assert(_rqId != null, "_rqId != null");
return _rqId;
}



// Release session state
private Task ReleaseStateAsync(HttpApplication application)
{
Expand Down Expand Up @@ -849,15 +845,15 @@ private async Task ReleaseStateAsyncImpl(HttpApplication application)
// we need to explicitly call Session_End.
if (_supportSessionExpiry)
{
_onEndTarget.RaiseSessionOnEnd(ReleaseStateGetSessionId(), _rqItem);
_onEndTarget.RaiseSessionOnEnd(_rqId, _rqItem);
}
}
else
{
Debug.Assert(_rqItem != null, "_rqItem cannot null if it's not a new session");

// Remove it from the store because the session is abandoned.
await _store.RemoveItemAsync(_rqContext, ReleaseStateGetSessionId(), _rqLockId, _rqItem, GetCancellationToken());
await _store.RemoveItemAsync(_rqContext, _rqId, _rqLockId, _rqItem, GetCancellationToken());
}
}
else if (!_rqReadonly ||
Expand All @@ -882,8 +878,7 @@ private async Task ReleaseStateAsyncImpl(HttpApplication application)
}

setItemCalled = true;
await
_store.SetAndReleaseItemExclusiveAsync(_rqContext, ReleaseStateGetSessionId(), _rqItem,
await _store.SetAndReleaseItemExclusiveAsync(_rqContext, _rqId, _rqItem,
_rqLockId, _rqSessionStateNotFound, GetCancellationToken());
}
else
Expand All @@ -892,8 +887,7 @@ private async Task ReleaseStateAsyncImpl(HttpApplication application)
if (!_rqSessionStateNotFound)
{
Debug.Assert(_rqItem != null, "_rqItem cannot null if it's not a new session");
await
_store.ReleaseItemExclusiveAsync(_rqContext, ReleaseStateGetSessionId(), _rqLockId,
await _store.ReleaseItemExclusiveAsync(_rqContext, _rqId, _rqLockId,
GetCancellationToken());
}
}
Expand Down
13 changes: 0 additions & 13 deletions src/SqlSessionStateProviderAsync/App.config

This file was deleted.

98 changes: 0 additions & 98 deletions src/SqlSessionStateProviderAsync/Entities/ModelHelper.cs

This file was deleted.

23 changes: 0 additions & 23 deletions src/SqlSessionStateProviderAsync/Entities/Session.cs

This file was deleted.

23 changes: 0 additions & 23 deletions src/SqlSessionStateProviderAsync/Entities/SessionContext.cs

This file was deleted.

Loading

0 comments on commit 1630641

Please sign in to comment.