public void Should_be_able_to_check_if_key_exists() { Policy policy = Policy.NoOp(); string key = Guid.NewGuid().ToString(); _registry.Add(key, policy); _registry.ContainsKey(key).Should().BeTrue(); string key2 = Guid.NewGuid().ToString(); _registry.ContainsKey(key2).Should().BeFalse(); }
/// <summary> /// Supplies the specified the policy registry, so we can use policies for Task Queues or in user-defined request handlers such as ExceptionHandler /// that provide quality of service concerns /// </summary> /// <param name="thePolicyRegistry">The policy registry.</param> /// <returns>INeedLogging.</returns> /// <exception cref="ConfigurationException">The policy registry is missing the CommandProcessor.RETRYPOLICY policy which is required</exception> /// <exception cref="ConfigurationException">The policy registry is missing the CommandProcessor.CIRCUITBREAKER policy which is required</exception> public INeedMessaging Policies(IPolicyRegistry <string> thePolicyRegistry) { if (!thePolicyRegistry.ContainsKey(CommandProcessor.RETRYPOLICY)) { throw new ConfigurationException("The policy registry is missing the CommandProcessor.RETRYPOLICY policy which is required"); } if (!thePolicyRegistry.ContainsKey(CommandProcessor.CIRCUITBREAKER)) { throw new ConfigurationException("The policy registry is missing the CommandProcessor.CIRCUITBREAKER policy which is required"); } _policyRegistry = thePolicyRegistry; return(this); }
/// <summary> /// Protect yourself against a missing policy by using NoOp /// </summary> /// <param name="input"></param> /// <returns></returns> public string UseRegistryWithNoOp(int input) { ISyncPolicy policy = _registry.ContainsKey("DefaultRetry") ? _registry.Get <ISyncPolicy>("DefaultRetry") : Policy.NoOp(); var response = policy.Execute(() => dbContext.First(r => r.Key == input)); return(response.Value); }
public void Register() { if (!registry.ContainsKey(Name)) { var policy = Policy.CacheAsync <CacheFormat>(cacheProvider, TtlStrategy, this, OnCacheGet, OnCacheMiss, OnCachePut, OnCacheGetError, OnCachePutError); registry.Add(Name, policy); } else { logger.LogError("CacheManagement {cacheName} has already been registered", Name); } }
public static TBuilder AddPolicies <TBuilder>(this TBuilder builder, IPolicyRegistry <string> policyRegistry) where TBuilder : IQueryProcessorExtensionBuilder { if (policyRegistry == null) { throw new ArgumentNullException(nameof(policyRegistry)); } if (!policyRegistry.ContainsKey(Constants.RetryPolicyName)) { throw new ConfigurationException($"The policy registry is missing the {Constants.RetryPolicyName} policy which is required"); } if (!policyRegistry.ContainsKey(Constants.CircuitBreakerPolicyName)) { throw new ConfigurationException($"The policy registry is missing the {Constants.CircuitBreakerPolicyName} policy which is required"); } builder.RegisterDecorator(typeof(RetryableQueryDecorator <,>)); builder.AddContextBagItem(Constants.ContextBagKey, policyRegistry); return(builder); }
public bool TryAdd <TPolicy>(string key, TPolicy policy) where TPolicy : ICircuitBreakerPolicy { var added = false; lock (_lock) { if (!Registry.ContainsKey(key)) { Registry.Add(key, policy); added = true; } } return(added); }