Should Polly Policies be singletons?

烈酒焚心 提交于 2019-12-05 08:36:24

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.

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

  • Share the same breaker policy instance across call sites when you want those call sites to break in common - for instance they have a common downstream dependency.
  • Don't share a breaker instance across call sites when you want those call sites to have independent circuit state and break independently.

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).

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!