Skip to content

Commit 98b575f

Browse files
committed
default max connections set to 0, placed async outside of semaphore if max conn is 0
1 parent 1f38f35 commit 98b575f

File tree

2 files changed

+16
-9
lines changed

2 files changed

+16
-9
lines changed

Diff for: src/Nest/Domain/Connection/Connection.cs

+15-8
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@ public Connection(IConnectionSettings settings)
2525
throw new ArgumentNullException("settings");
2626

2727
this._ConnectionSettings = settings;
28-
this._ResourceLock = new Semaphore(settings.MaximumAsyncConnections, settings.MaximumAsyncConnections);
28+
var semaphore = Math.Max(1, settings.MaximumAsyncConnections);
29+
this._ResourceLock = new Semaphore(semaphore, semaphore);
2930
this._enableTrace = settings.TraceEnabled;
3031
}
3132

@@ -212,6 +213,9 @@ protected virtual ConnectionStatus DoSynchronousRequest(HttpWebRequest request,
212213
protected virtual Task<ConnectionStatus> DoAsyncRequest(HttpWebRequest request, string data = null)
213214
{
214215
var tcs = new TaskCompletionSource<ConnectionStatus>();
216+
if (this._ConnectionSettings.MaximumAsyncConnections <= 0)
217+
return this.CreateIterateTask(request, data, tcs);
218+
215219
var timeout = this._ConnectionSettings.Timeout;
216220
if (!this._ResourceLock.WaitOne(timeout))
217221
{
@@ -227,20 +231,23 @@ protected virtual Task<ConnectionStatus> DoAsyncRequest(HttpWebRequest request,
227231
}
228232
try
229233
{
230-
//return Task.Factory.StartNew(() =>
231-
//{
232-
233-
this.Iterate(this._AsyncSteps(request, tcs, data), tcs);
234-
return tcs.Task;
235-
236-
//}, TaskCreationOptions.LongRunning);
234+
return Task.Factory.StartNew(
235+
() => this.CreateIterateTask(request, data, tcs).Result
236+
, TaskCreationOptions.LongRunning
237+
);
237238
}
238239
finally
239240
{
240241
this._ResourceLock.Release();
241242
}
242243
}
243244

245+
private Task<ConnectionStatus> CreateIterateTask(HttpWebRequest request, string data, TaskCompletionSource<ConnectionStatus> tcs)
246+
{
247+
this.Iterate(this._AsyncSteps(request, tcs, data), tcs);
248+
return tcs.Task;
249+
}
250+
244251
private IEnumerable<Task> _AsyncSteps(HttpWebRequest request, TaskCompletionSource<ConnectionStatus> tcs, string data = null)
245252
{
246253
using (var tracer = new ConnectionStatusTracer(this._ConnectionSettings.TraceEnabled))

Diff for: src/Nest/Domain/Connection/ConnectionSettings.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ public ConnectionSettings(Uri uri)
4848
this.Host = uri.Host;
4949
this.Port = uri.Port;
5050

51-
this.MaximumAsyncConnections = 20;
51+
this.MaximumAsyncConnections = 0;
5252
this.DefaultTypeNameInferrer = this.LowerCaseAndPluralizeTypeNameInferrer;
5353
this.DefaultIndices = new FluentDictionary<Type, string>();
5454
this.DefaultTypeNames = new FluentDictionary<Type, string>();

0 commit comments

Comments
 (0)