Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Internal API] Protect against properties that throw exceptions. #62

Merged
merged 1 commit into from
Sep 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions src/Marille/Hub.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,8 @@ Task DeliverAtLeastOnceSync<T> (Channel<Message<T>> channel, IWorker<T> [] worke
var tasks = new Task [workersArray.Length];
for(var index = 0; index < workersArray.Length; index++) {
var worker = workersArray [index];
if (worker.UseBackgroundThread) {
_ = worker.TryGetUseBackgroundThread (out var useBackgroundThread);
if (useBackgroundThread) {
tasks [index] = Task.Run (async () => {
await worker.ConsumeAsync (item.Payload, token).ConfigureAwait (false);
}, token);
Expand All @@ -88,7 +89,8 @@ Task DeliverAtMostOnce<T> (Channel<Message<T>> channel, IWorker<T> [] workersArr
token = cts.Token;
}

var task = worker.UseBackgroundThread ?
_ = worker.TryGetUseBackgroundThread (out var useBackgroundThread);
var task = useBackgroundThread ?
Task.Run (async () => {
await worker.ConsumeAsync (item.Payload, token).ConfigureAwait (false);
}, token) :
Expand Down
17 changes: 17 additions & 0 deletions src/Marille/WorkerExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
namespace Marille;

internal static class WorkerExtensions {

// properties can throw (ie: NotImplementedException) so we need to catch them and use a default value. We should
// allow to log this later.
public static bool TryGetUseBackgroundThread<T> (this IWorker<T> worker, out bool useBackgroundThread) where T : struct
{
useBackgroundThread = false;
try {
useBackgroundThread = worker.UseBackgroundThread;
return true;
} catch {
return false;
}
}
}