Skip to content

Instantly share code, notes, and snippets.

@onyxmaster
Last active June 9, 2023 14:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save onyxmaster/72f6608767a5336dc16b488ebc498db4 to your computer and use it in GitHub Desktop.
Save onyxmaster/72f6608767a5336dc16b488ebc498db4 to your computer and use it in GitHub Desktop.
// Filter only active items
record Item(DateTime ActiveUntil);
IReadOnlyCollection<Item> GetActiveItems(IEnumerable<Item> items) => items.Where(item => item.ActiveUntil > DateTime.Now).ToArray();
/* ******************************************************************** */
// Robustly perform an [idempotent] request until timeout is reached
void PerformRequestRobustly(TimeSpan timeout)
{
var retryUntil = DateTime.UtcNow + timeout;
while (true)
{
try
{
PerformRequest();
break;
}
catch (FailedRequestException) when (DateTime.UtcNow < retryUntil)
{
}
}
}
/* ******************************************************************** */
// This is a piece of library code. Is this acceptable?
async Task RobustlyCall(Func<Task> inner)
{
if (inner is null)
{
throw new ArgumentNullException(nameof(inner));
}
try
{
await inner();
}
catch
{
}
}
/* ******************************************************************** */
// Is this any better? Or worse?
Task RobustlyCall(Func<Task> inner)
{
if (inner is null)
{
throw new ArgumentNullException(nameof(inner));
}
try
{
return inner();
}
catch
{
return Task.CompletedTask;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment