Skip to content

Commit

Permalink
Cleaning things up
Browse files Browse the repository at this point in the history
  • Loading branch information
alexeyzimarev committed Nov 24, 2023
1 parent bd59c90 commit 0be1656
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 24 deletions.
11 changes: 0 additions & 11 deletions src/Core/src/Eventuous.Shared/Tools/Ensure.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,17 +37,6 @@ public static T NotNull<T>(T? value, [CallerArgumentExpression("value")] string?
public static string NotEmptyString(string? value, [CallerArgumentExpression("value")] string? name = default)
=> !string.IsNullOrWhiteSpace(value) ? value : throw new ArgumentNullException(name);

// /// <summary>
// /// Throws a <see cref="DomainException"/> with a given message if the condition is not met
// /// </summary>
// /// <param name="condition">Condition to check</param>
// /// <param name="errorMessage">Message for the exception</param>
// /// <exception cref="DomainException"></exception>
// [DebuggerHidden]
// public static void IsTrue(bool condition, Func<string> errorMessage) {
// if (!condition) throw new DomainException(errorMessage());
// }

/// <summary>
/// Throws a custom exception if the condition is not met
/// </summary>
Expand Down
14 changes: 12 additions & 2 deletions src/Core/src/Eventuous.Shared/Tools/TaskExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,38 @@
// Licensed under the Apache License, Version 2.0.

using System.Runtime.CompilerServices;
// ReSharper disable PossibleMultipleEnumeration

namespace Eventuous.Tools;

static class TaskExtensions {
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static ConfiguredTaskAwaitable NoContext(this Task task) => task.ConfigureAwait(false);

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static ConfiguredTaskAwaitable<T> NoContext<T>(this Task<T> task) => task.ConfigureAwait(false);

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static ConfiguredValueTaskAwaitable NoContext(this ValueTask task) => task.ConfigureAwait(false);

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static ConfiguredValueTaskAwaitable<T> NoContext<T>(this ValueTask<T> task) => task.ConfigureAwait(false);

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static ConfiguredCancelableAsyncEnumerable<T> NoContext<T>(this IAsyncEnumerable<T> source, CancellationToken cancellationToken)
=> source.WithCancellation(cancellationToken).ConfigureAwait(false);

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Task WhenAll(this IEnumerable<Task> tasks) => Task.WhenAll(tasks);

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static async Task WhenAll(this IEnumerable<ValueTask> tasks) {
var toAwait = tasks.Where(valueTask => !valueTask.IsCompletedSuccessfully).Select(valueTask => valueTask.AsTask()).ToList();
var toAwait = tasks.Where(valueTask => !valueTask.IsCompletedSuccessfully).Select(valueTask => valueTask.AsTask());

if (toAwait.Count > 0) await Task.WhenAll(toAwait).NoContext();
if (toAwait.Any()) await Task.WhenAll(toAwait).NoContext();
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static async Task<IReadOnlyCollection<T>> WhenAll<T>(this IEnumerable<ValueTask<T>> tasks) {
var results = new List<T>();
var toAwait = new List<Task<T>>();
Expand All @@ -43,6 +52,7 @@ public static async Task<IReadOnlyCollection<T>> WhenAll<T>(this IEnumerable<Val
return results;
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static ConfiguredTaskAwaitable NoThrow(this Task task) {
#if NET8_0_OR_GREATER
return task.ConfigureAwait(ConfigureAwaitOptions.SuppressThrowing);
Expand Down
9 changes: 1 addition & 8 deletions src/Core/src/Eventuous.Shared/Tools/TaskRunner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,7 @@ public TaskRunner Start() {

return this;

async Task Run() {
try {
await taskFactory(_stopSource.Token);
} catch (OperationCanceledException) {
// ignore
}
}
async Task Run() => await taskFactory(_stopSource.Token).NoThrow();
}

public async ValueTask Stop(CancellationToken cancellationToken) {
Expand All @@ -31,7 +25,6 @@ public async ValueTask Stop(CancellationToken cancellationToken) {
#else
_stopSource.Cancel();
#endif
// if (_runner != null) await _runner.NoContext();
} finally {
var state = new TaskCompletionSource<object>();
var registration = cancellationToken.Register((s => (((TaskCompletionSource<object>)s!)!).SetCanceled(cancellationToken)), state);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,13 @@ namespace Eventuous.Subscriptions.Filters.Partitioning;
using Context;

public static class Partitioner {
/// <summary>
/// Partition key hash calculator function
/// </summary>
public delegate uint GetPartitionHash(string partitionKey);

/// <summary>
/// Function to get a partition key from a message context
/// </summary>
public delegate string GetPartitionKey(IMessageConsumeContext context);
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
// Copyright (C) Ubiquitous AS. All rights reserved
// Licensed under the Apache License, Version 2.0.

using System.Runtime.CompilerServices;

namespace Eventuous.Subscriptions.Filters;

using Context;
Expand Down Expand Up @@ -31,7 +29,6 @@ public PartitioningFilter(
.ToArray();
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
protected override ValueTask Send(AsyncConsumeContext context, LinkedListNode<IConsumeFilter>? next) {
var partitionKey = _partitioner(context);
var hash = _getHash(partitionKey);
Expand Down

0 comments on commit 0be1656

Please sign in to comment.