Skip to content

Commit 8b6235e

Browse files
committed
bench
1 parent ba34a9e commit 8b6235e

File tree

6 files changed

+24
-18
lines changed

6 files changed

+24
-18
lines changed

src/Cassandra/Connections/Connection.cs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -761,15 +761,15 @@ private static bool InvokeReadCallbacks(MemoryStream stream, ICollection<Func<Me
761761
return false;
762762
}
763763
//Invoke all callbacks using the default TaskScheduler
764-
Task.Run(async () =>
764+
Task.Factory.StartNew(async () =>
765765
{
766766
stream.Position = 0;
767767
foreach (var cb in operationCallbacks)
768768
{
769769
await cb(stream, timestamp).ConfigureAwait(false);
770770
}
771771
stream.Dispose();
772-
}, CancellationToken.None);
772+
}, CancellationToken.None, TaskCreationOptions.None, TaskScheduler.Default);
773773
return true;
774774
}
775775

@@ -803,7 +803,9 @@ public OperationState Send(IRequest request, Func<IRequestError, Response, Task>
803803
if (_isClosed)
804804
{
805805
// Avoid calling back before returning
806-
Task.Run(() => callback(RequestError.CreateClientError(new SocketException((int)SocketError.NotConnected), true), null), CancellationToken.None);
806+
Task.Factory.StartNew(
807+
() => callback(RequestError.CreateClientError(new SocketException((int)SocketError.NotConnected), true), null),
808+
CancellationToken.None, TaskCreationOptions.None, TaskScheduler.Default);
807809
return null;
808810
}
809811

@@ -827,7 +829,9 @@ public OperationState Send(IRequest request, Func<IRequestError, Response, Task>
827829
catch (Exception ex)
828830
{
829831
// Avoid calling back before returning
830-
Task.Run(() => callback(RequestError.CreateClientError(ex, true), null), CancellationToken.None);
832+
Task.Factory.StartNew(
833+
() => callback(RequestError.CreateClientError(ex, true), null),
834+
CancellationToken.None, TaskCreationOptions.None, TaskScheduler.Default);
831835
return null;
832836
}
833837
}

src/Cassandra/Connections/HostConnectionPool.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -404,7 +404,7 @@ internal void OnConnectionClosing(IConnection c = null)
404404
return;
405405
}
406406
// We are using an IO thread
407-
Task.Run(async () =>
407+
Task.Factory.StartNew(async () =>
408408
{
409409
// Use a lock for avoiding concurrent calls to SetNewConnectionTimeout()
410410
await _allConnectionClosedEventLock.WaitAsync().ConfigureAwait(false);
@@ -427,7 +427,7 @@ internal void OnConnectionClosing(IConnection c = null)
427427
{
428428
_allConnectionClosedEventLock.Release();
429429
}
430-
}).Forget();
430+
}, CancellationToken.None, TaskCreationOptions.None, TaskScheduler.Default);
431431
}
432432

433433
private void OnDistanceChanged(HostDistance previousDistance, HostDistance distance)

src/Cassandra/OperationState.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ public void InvokeCallback(IRequestError error, long timestamp)
155155
}
156156
//Invoke the callback in a new thread in the thread pool
157157
//This way we don't let the user block on a thread used by the Connection
158-
Task.Run(() => callback(error, null, timestamp), CancellationToken.None);
158+
Task.Factory.StartNew(() => callback(error, null, timestamp), CancellationToken.None, TaskCreationOptions.None, TaskScheduler.Default);
159159
}
160160

161161
/// <summary>
@@ -175,7 +175,7 @@ public bool MarkAsTimedOut(OperationTimedOutException ex, Func<Task> onReceive,
175175
Thread.MemoryBarrier();
176176

177177
_timeoutCallbackSet = true;
178-
Task.Run(() => callback(RequestError.CreateClientError(ex, false), null, timestamp), CancellationToken.None);
178+
Task.Factory.StartNew(() => callback(RequestError.CreateClientError(ex, false), null, timestamp), CancellationToken.None, TaskCreationOptions.None, TaskScheduler.Default);
179179
return true;
180180
}
181181

@@ -204,14 +204,14 @@ public void Cancel()
204204
/// </summary>
205205
internal static void CallbackMultiple(IEnumerable<OperationState> ops, IRequestError error, long timestamp)
206206
{
207-
Task.Run(async () =>
207+
Task.Factory.StartNew(async () =>
208208
{
209209
foreach (var state in ops)
210210
{
211211
var callback = state.SetCompleted();
212212
await callback(error, null, timestamp).ConfigureAwait(false);
213213
}
214-
}, CancellationToken.None);
214+
}, CancellationToken.None, TaskCreationOptions.None, TaskScheduler.Default);
215215
}
216216
}
217217
}

src/Cassandra/ProtocolEvents/ProtocolEventDebouncer.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
using System;
1818
using System.Linq;
19+
using System.Threading;
1920
using System.Threading.Tasks;
2021
using System.Threading.Tasks.Dataflow;
2122

@@ -184,7 +185,7 @@ private void Process()
184185
_queue = null;
185186

186187
// not necessary to enqueue within the exclusive scheduler
187-
Task.Run(async () =>
188+
Task.Factory.StartNew(async () =>
188189
{
189190
var sent = false;
190191
try
@@ -203,7 +204,7 @@ private void Process()
203204
cb?.TrySetException(new DriverInternalError("Could not process events in the ProtocolEventDebouncer."));
204205
}
205206
}
206-
}).Forget();
207+
}, CancellationToken.None, TaskCreationOptions.None, TaskScheduler.Default);
207208
}
208209

209210
private static async Task ProcessQueue(EventQueue queue)

src/Cassandra/Requests/RequestExecution.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
using System.Linq;
2222
using System.Net;
2323
using System.Net.Sockets;
24+
using System.Threading;
2425
using System.Threading.Tasks;
2526
using Cassandra.Connections;
2627
using Cassandra.Observers.Abstractions;
@@ -573,11 +574,11 @@ bool SearchBoundStatement(Statement s) =>
573574
preparedKeyspace, _session.Keyspace));
574575

575576
var c = _connection;
576-
Task.Run(async () =>
577+
Task.Factory.StartNew(async () =>
577578
{
578579
await c.SetKeyspace(preparedKeyspace).ConfigureAwait(false);
579580
await SendAsync(request, nodeRequestInfo.Host, NewReprepareResponseHandler(ex)).ConfigureAwait(false);
580-
}).Forget();
581+
}, CancellationToken.None, TaskCreationOptions.None, TaskScheduler.Default).Forget();
581582
return;
582583
}
583584
await SendAsync(request, nodeRequestInfo.Host, NewReprepareResponseHandler(ex)).ConfigureAwait(false);

src/Cassandra/Requests/RequestHandler.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -253,7 +253,7 @@ private async Task<bool> SetCompletedAsync(Exception ex, RowSet result, Func<Tas
253253
if (action != null)
254254
{
255255
//Create a new Task using the default scheduler, invoke the action and set the result
256-
Task.Run(async () =>
256+
Task.Factory.StartNew(async () =>
257257
{
258258
try
259259
{
@@ -268,7 +268,7 @@ private async Task<bool> SetCompletedAsync(Exception ex, RowSet result, Func<Tas
268268

269269
await ClearNodeExecutionsAsync().ConfigureAwait(false);
270270
await _requestResultHandler.TrySetResultAsync(result, _sessionRequestInfo).ConfigureAwait(false);
271-
}, CancellationToken.None).Forget();
271+
}, CancellationToken.None, TaskCreationOptions.None, TaskScheduler.Default).Forget();
272272
return true;
273273
}
274274

@@ -506,7 +506,7 @@ private void ScheduleNext(Host currentHost)
506506
_nextExecutionTimeout = _session.Cluster.Configuration.Timer.NewTimeout(_ =>
507507
{
508508
// Start the speculative execution outside the IO thread
509-
Task.Run(() =>
509+
Task.Factory.StartNew(() =>
510510
{
511511
if (HasCompleted())
512512
{
@@ -516,7 +516,7 @@ private void ScheduleNext(Host currentHost)
516516
RequestHandler.Logger.Info("Starting new speculative execution after {0} ms. Last used host: {1}", delay, currentHost.Address);
517517
_requestObserver.OnSpeculativeExecution(currentHost, delay);
518518
return StartNewExecutionAsync();
519-
}, CancellationToken.None);
519+
}, CancellationToken.None, TaskCreationOptions.None, TaskScheduler.Default);
520520
}, null, delay);
521521
}
522522

0 commit comments

Comments
 (0)