PageRenderTime 52ms CodeModel.GetById 22ms RepoModel.GetById 1ms app.codeStats 0ms

/src/SqlServer/TransientDbConnection/RetryPolicyConfigurationSettings.cs

http://hashfoosqlserver.codeplex.com
C# | 172 lines | 87 code | 18 blank | 67 comment | 3 complexity | 7126accbed6f3c7d40538f54717c6a89 MD5 | raw file
  1. //=======================================================================================
  2. // Microsoft Windows Server AppFabric Customer Advisory Team (CAT) Best Practices Series
  3. //
  4. // This sample is supplemental to the technical guidance published on the community
  5. // blog at http://blogs.msdn.com/appfabriccat/.
  6. //
  7. //=======================================================================================
  8. // Copyright © 2010 Microsoft Corporation. All rights reserved.
  9. //
  10. // THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER
  11. // EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES OF
  12. // MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. YOU BEAR THE RISK OF USING IT.
  13. //=======================================================================================
  14. using System;
  15. namespace HashFoo.SqlServer.TransientDbConnection
  16. {
  17. #region Using references
  18. #endregion
  19. /// <summary>
  20. /// Implements a configuration section containing retry policy settings.
  21. /// </summary>
  22. [Serializable]
  23. public sealed class RetryPolicyConfigurationSettings : ConfigurationSection
  24. {
  25. #region Private members
  26. private const string DefaultPolicyProperty = "defaultPolicy";
  27. private const string DefaultSqlConnectionPolicyProperty = "defaultSqlConnectionPolicy";
  28. private const string DefaultSqlCommandPolicyProperty = "defaultSqlCommandPolicy";
  29. private const string DefaultStoragePolicyProperty = "defaultStoragePolicy";
  30. private const string DefaultCommunicationPolicyProperty = "defaultCommunicationPolicy";
  31. #endregion
  32. #region Public members
  33. /// <summary>
  34. /// The name of the configuration section represented by this type.
  35. /// </summary>
  36. public const string SectionName = "RetryPolicyConfiguration";
  37. #endregion
  38. #region Constructor
  39. /// <summary>
  40. /// Initializes a new instance of a <see cref="RetryPolicyConfigurationSettings"/> object using default settings.
  41. /// </summary>
  42. public RetryPolicyConfigurationSettings()
  43. {
  44. }
  45. #endregion
  46. #region Public properties
  47. /// <summary>
  48. /// Gets or sets the name of the default general-purpose retry policy.
  49. /// </summary>
  50. [ConfigurationProperty(DefaultPolicyProperty, IsRequired = true)]
  51. public string DefaultPolicy
  52. {
  53. get { return (string)base[DefaultPolicyProperty]; }
  54. set { base[DefaultPolicyProperty] = value; }
  55. }
  56. /// <summary>
  57. /// Gets or sets the name of a retry policy dedicated to handling transient conditions with SQL connections.
  58. /// </summary>
  59. [ConfigurationProperty(DefaultSqlConnectionPolicyProperty, IsRequired = false)]
  60. public string DefaultSqlConnectionPolicy
  61. {
  62. get { return (string)base[DefaultSqlConnectionPolicyProperty]; }
  63. set { base[DefaultSqlConnectionPolicyProperty] = value; }
  64. }
  65. /// <summary>
  66. /// Gets or sets the name of a retry policy dedicated to handling transient conditions with SQL commands.
  67. /// </summary>
  68. [ConfigurationProperty(DefaultSqlCommandPolicyProperty, IsRequired = false)]
  69. public string DefaultSqlCommandPolicy
  70. {
  71. get { return (string)base[DefaultSqlCommandPolicyProperty]; }
  72. set { base[DefaultSqlCommandPolicyProperty] = value; }
  73. }
  74. /// <summary>
  75. /// Gets or sets the name of a retry policy dedicated to handling transient conditions in Windows Azure storage services.
  76. /// </summary>
  77. [ConfigurationProperty(DefaultStoragePolicyProperty, IsRequired = false)]
  78. public string DefaultStoragePolicy
  79. {
  80. get { return (string)base[DefaultStoragePolicyProperty]; }
  81. set { base[DefaultStoragePolicyProperty] = value; }
  82. }
  83. /// <summary>
  84. /// Gets or sets the name of a retry policy dedicated to handling transient conditions in the WCF communication infrastructure.
  85. /// </summary>
  86. [ConfigurationProperty(DefaultCommunicationPolicyProperty, IsRequired = false)]
  87. public string DefaultCommunicationPolicy
  88. {
  89. get { return (string)base[DefaultCommunicationPolicyProperty]; }
  90. set { base[DefaultCommunicationPolicyProperty] = value; }
  91. }
  92. /// <summary>
  93. /// Returns a collection of retry policy definitions represented by the <see cref="RetryPolicyInfo"/> object instances.
  94. /// </summary>
  95. [ConfigurationProperty("", Options = ConfigurationPropertyOptions.IsDefaultCollection)]
  96. [ConfigurationCollection(typeof(RetryPolicyInfo))]
  97. public RetryPolicyCollection Policies
  98. {
  99. get { return (RetryPolicyCollection)base[String.Empty]; }
  100. }
  101. #endregion
  102. #region Public methods
  103. /// <summary>
  104. /// Returns an instance of the <see cref="RetryPolicy"/> object initialized for a given policy name.
  105. /// </summary>
  106. /// <typeparam name="T">The type implementing the <see cref="ITransientErrorDetectionStrategy"/> interface which is responsible for detecting transient conditions.</typeparam>
  107. /// <param name="policyName">The name under which a retry policy definition is registered in the application configuration.</param>
  108. /// <returns>The retry policy initialized from the specified policy definition, or a null reference if no such policy definition was found.</returns>
  109. public RetryPolicy<T> GetRetryPolicy<T>(string policyName) where T : ITransientErrorDetectionStrategy, new()
  110. {
  111. RetryPolicy<T> retryPolicy = null;
  112. if (!String.IsNullOrEmpty(policyName) && Policies.Contains(policyName))
  113. {
  114. RetryPolicyInfo retryPolicyInfo = Policies.Get(policyName);
  115. return retryPolicyInfo != null ? retryPolicyInfo.CreatePolicy<T>() : null;
  116. }
  117. return retryPolicy;
  118. }
  119. /// <summary>
  120. /// Adds a new definition describing a retry policy based on fixed interval retry mode.
  121. /// </summary>
  122. /// <param name="policyName">The name under which a retry policy definition will be registered in the application configuration.</param>
  123. /// <param name="maxRetryCount">The maximum number of retry attempts.</</param>
  124. /// <param name="retryInterval">The interval in milliseconds between retry attempts.</param>
  125. public void AddFixedIntervalPolicy(string policyName, int maxRetryCount, int retryInterval)
  126. {
  127. Policies.Add(new RetryPolicyInfo() { Name = policyName, MaxRetryCount = maxRetryCount, RetryInterval = retryInterval });
  128. }
  129. /// <summary>
  130. /// Adds a new definition describing a retry policy based on incremental interval retry mode.
  131. /// </summary>
  132. /// <param name="policyName">The name under which a retry policy definition will be registered in the application configuration.</param>
  133. /// <param name="maxRetryCount">The maximum number of retry attempts.</param>
  134. /// <param name="retryInterval">The initial interval in milliseconds which will apply for the first retry.</param>
  135. /// <param name="retryIncrement">The incremental time value in milliseconds for calculating progressive delay between retry attempts.</param>
  136. public void AddIncrementalIntervalPolicy(string policyName, int maxRetryCount, int retryInterval, int retryIncrement)
  137. {
  138. Policies.Add(new RetryPolicyInfo() { Name = policyName, MaxRetryCount = maxRetryCount, RetryInterval = retryInterval, RetryIncrement = retryIncrement });
  139. }
  140. /// <summary>
  141. /// Adds a new definition describing a retry policy based on random exponential back-off retry mode.
  142. /// </summary>
  143. /// <param name="policyName">The name under which a retry policy definition will be registered in the application configuration.</param>
  144. /// <param name="maxRetryCount">The maximum number of retry attempts.</param>
  145. /// <param name="minBackoff">The minimum back-off time in milliseconds.</param>
  146. /// <param name="maxBackoff">The maximum back-off time in milliseconds.</param>
  147. /// <param name="deltaBackof">The delta value in milliseconds to calculate random exponential delay between retry attempts.</param>
  148. public void AddExponentialIntervalPolicy(string policyName, int maxRetryCount, int minBackoff, int maxBackoff, int deltaBackof)
  149. {
  150. Policies.Add(new RetryPolicyInfo() { Name = policyName, MaxRetryCount = maxRetryCount, MinBackoff = minBackoff, MaxBackoff = maxBackoff, DeltaBackoff = deltaBackof });
  151. }
  152. #endregion
  153. }
  154. }