触发并忘记发送非必要指标的异步方法的最佳实践 [英] Best practices to Fire and Forget an async method to send non essential metric

查看:24
本文介绍了触发并忘记发送非必要指标的异步方法的最佳实践的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

等待发送非必要指标对我来说毫无意义,因为它会在每次调用后端 dotnet 核心服务时增加延迟(等待服务器的响应),每次客户端调用可能会发生多次.尽管如此,在失败时记录错误仍然很重要(不过我不需要抛出它,因为指标的失败不应影响服务).

Waiting for a non-essential metrics to be sent makes no sense to me, as it adds latency (waiting for the server's response) on each call to the back-end dotnet core service, which might happen multiple times per client call. Still, logging an error on failure is important (I do not need to throw it though, as the metric's failure should not impact the service).

我找到了多种方法.这是以 FireAndForget 方式调用的方法:

I found multiple ways of doing so. Here is the method to be called in a FireAndForget manner:

public async Task FireAndForget()
{
    try{ await sendTheMetric(); } // Try catch on the await, to log exceptions
    catch(Exception e){ logger.debug(e) }
}

方法一:去掉await.

Method 1: Remove the await.

FireAndForget(); // No await in front of the call

方法 2:对我来说似乎类似于方法 1,因为我们不等待 Task.Factory.StartNew 调用.

Method 2: Seems similar to method 1 to me, as we do not await the Task.Factory.StartNew call.

Task.Factory.StartNew(async () => await MyAsyncTask());

方法 3:作为工作项在 ThreadPool 上入队.

Method 3: Enqueue on the ThreadPool as a workItem.

ThreadPool.QueueUserWorkItem(async o => await FireAndForget());

我很难找到应该将哪个用于 Fire 并忘记调用发送非必要指标.我的目标是在每次发送指标时不增加对我的服务的每次调用的延迟.当指标发送失败时记录错误很重要,但它不应该重新抛出.线程上下文对于任务的执行并不重要.任务应该总是,或者至少几乎总是,完成.

I have a hard time finding which one I should use for a Fire and forget call to send a non-essential metric. My goal is to not add latency to each calls to my service each time a metric is sent. Logging an error when the metrics fails to be sent is important, but it should never re-throw. The thread context is not important for the execution of the task. The task should always, or at least almost always, be completed.

哪个是需求的最佳实践?还是都一样?

Which one is the best practice for for requirements? Or are they all the same?

注意:我没有包含 async void,因为它看起来有风险(如果发生异常,它可能会崩溃,因为没有 Task 会包装它).

Note: I did not include async void, as it seems risky (if an exception occurs, it might crash, as no Task will wrap it).

推荐答案

希望这不会把水搅浑,但有第四个选项(从我读到的内容来看)正在成为更被接受的选项.

Hope this doesn't muddy the water but there is a 4th option that (from what i have read) is becoming a more accepted option.

从 C#7.0 开始,您可以选择对此类实例使用丢弃.

Starting with C#7.0 you have the option of using a discard for this sort of instance.

_ = FireAndForget();

这实质上表明不需要该任务,因此不需要返回值.似乎比方法 1 更值得推荐,因为这意味着您明确指出它是即发即弃"(这不那么模棱两可,看起来不像是编码错误).

This essentially indicates that the task is not needed and thus no return value is expected. Seems to be recommended over Method 1 as it means you are explicitly indicating that it is "fire and forget" (which is less ambiguous and does not look like a coding mistake).

这篇关于触发并忘记发送非必要指标的异步方法的最佳实践的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆