Polly策略应该是单例吗? [英] Should Polly Policies be singletons?

查看:166
本文介绍了Polly策略应该是单例吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个查询IGetHamburgers,该查询调用了外部API. 我已经在我的DI容器中将IGetHamburgers的实现注册为Singleton.我将Polly用作断路器,如果两个请求失败,电路将打开.

I have a query, IGetHamburgers, that calls an external API. I've registered the implementation of IGetHamburgers in my DI container as a Singleton. Im using Polly as a Circuitbreaker, if two requests fails the circuit will open.

我的目标是,对Hamburger api的所有调用都应通过同一断路器,如果GetHamburgers失败,则所有其他调用也应失败.

My goal is that all calls to the Hamburger api should go through the same circuitbreaker, if GetHamburgers fails, then all other calls should fail as well.

我应该如何使用我的政策?我应该在以下字段中注册我的政策吗?

How should I use my Policy? Should I register my Policy as a field like this:

private Policy _policy;

private Policy Policy
{ 
    get 
    {
        if(this_policy != null)
        {
            return this_policy;
        }

        this._policy = Policy
            .Handle<Exception>()
            .CircuitBreaker(2, TimeSpan.FromMinutes(1));

        return this._policy;
    } 
}

public object Execute(.......)
{
    return Policy.Execute(() => this.hamburgerQuery.GetHamburgers());
}

OR

public object Execute(.......)
{
    var breaker = Policy
            .Handle<Exception>()
            .CircuitBreaker(2, TimeSpan.FromMinutes(1));
    return breaker.Execute(() => this.hamburgerQuery.GetHamburgers());
}

我猜第一个选项是正确的方法,因为从那时开始,Policy对象将始终是相同的,并且可以跟踪异常计数和类似的东西. 我的问题是,第二个选项也会起作用吗?我在Pollys Github上找到了很多示例/示例,但是我找不到在其中将Polly与DI和类似内容一起使用的真实世界"示例吗?

I guess that the first option is the correct way since then the Policy object will always be the same and can keep track of the exception count and stuff like that. My question is, will option number two work as well? I've found a lot of samples/examples on Pollys Github but I can't find any "real world" examples where Polly is used together with DI and stuff like that?

推荐答案

我猜第一个选项是正确的方法,因为从那时开始,Policy对象将始终是相同的,并且可以跟踪异常计数和类似的东西.

I guess that the first option is the correct way since then the Policy object will always be the same and can keep track of the exception count and stuff like that.

正确. Polly Wiki 这里.简而言之:

Correct. This is described in the Polly wiki here. In brief:

  • 当您希望这些呼叫站点共同中断时(例如,它们具有共同的下游依赖性),请在呼叫站点之间共享相同的中断策略实例.
  • 当您希望这些呼叫站点具有独立的电路状态并独立中断时,请不要在这些呼叫站点之间共享断路器实例.

请参见此stackoverflow答案,以更广泛的讨论将策略与使用策略分开配置,并通过DI将其注入使用站点,以及在Polly政策的整个范围(截至2017年6月)中重复使用同一实例(例如单例)与使用单独的实例的效果.

See this stackoverflow answer for a more extensive discussion of configuring policies separately from their usage, injecting them to usage sites by DI, and the effects of re-using the same instance (for example a singleton) versus using separate instances, across the full range (at June 2017) of Polly policies.

第二个选项也会起作用吗?

will option number two work as well?

否(出于相反的原因:每个调用都会创建一个单独的实例,因此不会与其他调用共享电路统计信息/状态).

No (for the converse reason: each call creates a separate instance, so won't share circuit statistics/states with other calls).

这篇关于Polly策略应该是单例吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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