Skip to content
This repository has been archived by the owner on Jul 24, 2018. It is now read-only.

Commit

Permalink
Merge pull request #1 from maverix/Timeout-Enhancement
Browse files Browse the repository at this point in the history
Added Disposable SetTimeoutFor to IDocumentStore
  • Loading branch information
Andrew Harry committed Sep 26, 2013
2 parents 468afd0 + 9773ef9 commit 9f1d83d
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 4 deletions.
17 changes: 15 additions & 2 deletions Raven.Client.Lightweight/Connection/HttpJsonRequestFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,10 @@ public HttpJsonRequest CreateHttpJsonRequest(CreateHttpJsonRequestParams createH
request.CachedRequestDetails = cachedRequestDetails.CachedRequest;
request.SkipServerCheck = cachedRequestDetails.SkipServerCheck;
}

if (SessionTimeout != null && SessionTimeout.HasValue && SessionTimeout.Value.TotalSeconds > 1)
request.Timeout = SessionTimeout.Value;

ConfigureRequest(createHttpJsonRequestParams.Owner, new WebRequestEventArgs {Request = request.webRequest});
return request;
}
Expand Down Expand Up @@ -172,6 +176,14 @@ public TimeSpan? AggressiveCacheDuration
set { aggressiveCacheDuration.Value = value; }
}

///<summary>
/// Session timeout - Thread Local
///</summary>
public TimeSpan? SessionTimeout {
get { return sessionTimeout.Value; }
set { sessionTimeout.Value = value; }
}

/// <summary>
/// Disable the HTTP caching
/// </summary>
Expand All @@ -191,7 +203,7 @@ public bool DisableHttpCaching
public bool EnableBasicAuthenticationOverUnsecuredHttpEvenThoughPasswordsWouldBeSentOverTheWireInClearTextToBeStolenByHackers { get; set; }

private readonly ThreadLocal<TimeSpan?> aggressiveCacheDuration = new ThreadLocal<TimeSpan?>(() => null);

private readonly ThreadLocal<TimeSpan?> sessionTimeout = new ThreadLocal<TimeSpan?>(() => null);
private readonly ThreadLocal<bool> disableHttpCaching = new ThreadLocal<bool>(() => false);

private volatile bool disposed;
Expand Down Expand Up @@ -243,10 +255,11 @@ public void Dispose()
{
if (disposed)
return;
disposed = true;
disposed = true;
cache.Dispose();
aggressiveCacheDuration.Dispose();
disableHttpCaching.Dispose();
sessionTimeout.Dispose();
}

internal void UpdateCacheTime(HttpJsonRequest httpJsonRequest)
Expand Down
29 changes: 27 additions & 2 deletions Raven.Client.Lightweight/Document/DocumentStore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -762,6 +762,31 @@ public override IDisposable AggressivelyCacheFor(TimeSpan cacheDuration)
#endif
}

/// <summary>
/// Setup the WebRequest timeout for the session
/// </summary>
/// <param name="timeout">Specify the timeout duration</param>
/// <remarks>
/// Sets the timeout for the JsonRequest. Scoped to the Current Thread.
/// </remarks>
public override IDisposable SetTimeoutFor(TimeSpan timeout) {
AssertInitialized();
#if !SILVERLIGHT && !NETFX_CORE
if (timeout.TotalSeconds < 1)
throw new ArgumentException("timeout must be longer than a single second");

var old = jsonRequestFactory.SessionTimeout;
jsonRequestFactory.SessionTimeout = timeout;

return new DisposableAction(() => {
jsonRequestFactory.SessionTimeout = old;
});
#else
// TODO: with silverlight, we don't currently support session timeout
return new DisposableAction(() => { });
#endif
}

private IAsyncDocumentSession OpenAsyncSessionInternal(string dbName, IAsyncDatabaseCommands asyncDatabaseCommands)
{
AssertInitialized();
Expand All @@ -776,7 +801,7 @@ private IAsyncDocumentSession OpenAsyncSessionInternal(string dbName, IAsyncData

var session = new AsyncDocumentSession(dbName, this, asyncDatabaseCommands, listeners, sessionId)
{
DatabaseName = dbName ?? DefaultDatabase
DatabaseName = dbName ?? DefaultDatabase
};
AfterSessionCreated(session);
return session;
Expand Down Expand Up @@ -810,7 +835,7 @@ public override IAsyncDocumentSession OpenAsyncSession(string databaseName)

public IAsyncDocumentSession OpenAsyncSession(OpenSessionOptions options)
{
return OpenAsyncSessionInternal(options.Database, SetupCommandsAsync(AsyncDatabaseCommands, options.Database, options.Credentials, options));
return OpenAsyncSessionInternal(options.Database, SetupCommandsAsync(AsyncDatabaseCommands, options.Database, options.Credentials, options));
}

/// <summary>
Expand Down
2 changes: 2 additions & 0 deletions Raven.Client.Lightweight/DocumentStoreBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ protected DocumentStoreBase()

public abstract IDisposable DisableAggressiveCaching();

public abstract IDisposable SetTimeoutFor(TimeSpan timeout);

#if !SILVERLIGHT && !NETFX_CORE
/// <summary>
/// Gets the shared operations headers.
Expand Down
9 changes: 9 additions & 0 deletions Raven.Client.Lightweight/IDocumentStore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,15 @@ public interface IDocumentStore : IDisposalNotification
/// </remarks>
IDisposable DisableAggressiveCaching();

/// <summary>
/// Setup the WebRequest timeout for the session
/// </summary>
/// <param name="timeout">Specify the timeout duration</param>
/// <remarks>
/// Sets the timeout for the JsonRequest. Scoped to the Current Thread.
/// </remarks>
IDisposable SetTimeoutFor(TimeSpan timeout);

/// <summary>
/// Gets the shared operations headers.
/// </summary>
Expand Down
18 changes: 18 additions & 0 deletions Raven.Client.Lightweight/Shard/ShardedDocumentStore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,24 @@ public override IDisposable DisableAggressiveCaching()
});
}

/// <summary>
/// Setup the WebRequest timeout for the session
/// </summary>
/// <param name="timeout">Specify the timeout duration</param>
/// <remarks>
/// Sets the timeout for the JsonRequest. Scoped to the Current Thread.
/// </remarks>
public override IDisposable SetTimeoutFor(TimeSpan timeout) {
var disposables =
ShardStrategy.Shards.Select(shard => shard.Value.SetTimeoutFor(timeout)).ToList();

return new DisposableAction(() => {
foreach (var disposable in disposables) {
disposable.Dispose();
}
});
}

#if !SILVERLIGHT && !NETFX_CORE

/// <summary>
Expand Down

0 comments on commit 9f1d83d

Please sign in to comment.