private IFlowController CreateFlowController(FlowControlStrategy strategy) { if (strategy == null) { throw new ArgumentNullException("FlowControllerFactory.CreateFlowController.strategy"); } IFlowController controller = null; switch (strategy.StrategyType) { case FlowControlStrategyType.TPS: controller = new TPSFlowController(strategy); break; case FlowControlStrategyType.Delay: controller = new DelayFlowController(strategy); break; case FlowControlStrategyType.Sum: controller = new SumFlowController(strategy); break; default: break; } return(controller); }
private long GetTokenBucketRefillInterval(FlowControlStrategy strategy) { long refillInterval = 0; switch (strategy.TimeSpan) { case FlowControlTimespan.Second: refillInterval = 1; break; case FlowControlTimespan.Minute: refillInterval = 60; break; case FlowControlTimespan.Hour: refillInterval = 60 * 60; break; case FlowControlTimespan.Day: refillInterval = 24 * 60 * 60; break; } return(refillInterval); }
public void SumFlowControlTest() { var strategy = new FlowControlStrategy() { ID = "SumFC", Name = "服务总访问量限流", Creator = "Teld", LastModifier = "Teld", CreateTime = DateTime.Now, LastModifyTime = DateTime.Now, StrategyType = FlowControlStrategyType.Sum, IntThreshold = 100, TimeSpan = FlowControlTimespan.Second, IsRefusedRequest = true }; FlowControlService.FlowControl(strategy, 1); try { for (int i = 0; i < 300; i++) { FlowControlService.FlowControl(strategy, 1); } } catch (Exception e) { Assert.AreEqual(e.Message, "触发流控!"); } }
public void DelayFlowControlWaitTest() { var strategy = new FlowControlStrategy() { ID = "DelayFC", Name = "服务总访问量限流", Creator = "Teld", LastModifier = "Teld", CreateTime = DateTime.Now, LastModifyTime = DateTime.Now, StrategyType = FlowControlStrategyType.Delay, IntThreshold = 100, TimeSpan = FlowControlTimespan.Second, IsRefusedRequest = false }; FlowControlService.FlowControl(strategy, 1); var stopWatch = new Stopwatch(); stopWatch.Start(); for (int i = 0; i < 100; i++) { FlowControlService.FlowControl(strategy, 1); } stopWatch.Stop(); Assert.IsTrue(stopWatch.ElapsedMilliseconds > 100 * 100); }
public IFlowController GetOrCreateFlowController(FlowControlStrategy strategy) { if (strategy == null) { throw new ArgumentNullException("FlowControllerFactory.GetOrCreateFlowController.strategy"); } if (!fcControllers.ContainsKey(strategy.ID)) { lock (syncObj) { if (!fcControllers.ContainsKey(strategy.ID)) { var fcController = CreateFlowController(strategy); if (fcController != null) { fcControllers.Add(strategy.ID, fcController); } } } } if (fcControllers.ContainsKey(strategy.ID)) { var controller = fcControllers[strategy.ID]; return(controller); } return(null); }
public void TPSFlowControlTest() { var strategy = new FlowControlStrategy() { ID = "TPSFC", Name = "服务TPS限流", Creator = "User", LastModifier = "User", CreateTime = DateTime.Now, LastModifyTime = DateTime.Now, StrategyType = FlowControlStrategyType.TPS, IntThreshold = 100, IsRefusedRequest = true }; FlowControlService.FlowControl(strategy, 1); try { for (int i = 0; i < 300; i++) { FlowControlService.FlowControl(strategy, 1); } } catch (Exception e) { Assert.AreEqual(e.Message, "触发流控!"); } }
public SumFlowController(FlowControlStrategy strategy) { FlowControlStrategy = strategy; var refillInterval = GetTokenBucketRefillInterval(strategy); InnerThrottleStrategy = new FixedTokenBucket(strategy.IntThreshold, refillInterval, 1000); }
/// <summary> /// 等待可用 /// </summary> /// <param name="strategy">流控策略</param> /// <param name="controller">流控器</param> /// <param name="waitTimespan">等待时间</param> /// <param name="count">请求次数</param> private static void WaitForAvailable(FlowControlStrategy strategy, IFlowController controller, TimeSpan waitTimespan, int count) { var timespan = waitTimespan; if (strategy.StrategyType == FlowControlStrategyType.Delay) { Thread.Sleep(timespan); return; } while (controller.ShouldThrottle(count, out timespan)) { Thread.Sleep(timespan); } }
/// <summary> /// 流控 /// </summary> /// <param name="strategy">流控策略</param> /// <param name="count">请求次数</param> public static void FlowControl(FlowControlStrategy strategy, int count = 1) { var controller = FlowControllerFactory.GetInstance().GetOrCreateFlowController(strategy); TimeSpan waitTimespan = TimeSpan.Zero; var result = controller.ShouldThrottle(count, out waitTimespan); if (result) { if (strategy.IsRefusedRequest == false && waitTimespan != TimeSpan.Zero) { WaitForAvailable(strategy, controller, waitTimespan, count); } else if (strategy.IsRefusedRequest) { throw new Exception("触发流控!"); } } }
public void TPSFlowControlWaitTest() { var strategy = new FlowControlStrategy() { ID = "TPSFC", Name = "服务TPS限流", Creator = "Teld", LastModifier = "Teld", CreateTime = DateTime.Now, LastModifyTime = DateTime.Now, StrategyType = FlowControlStrategyType.TPS, IntThreshold = 100, IsRefusedRequest = false }; FlowControlService.FlowControl(strategy, 1); for (int i = 0; i < 300; i++) { FlowControlService.FlowControl(strategy, 1); } }
public void SumFlowControlWaitTest() { var strategy = new FlowControlStrategy() { ID = "SumFC", Name = "服务总访问量限流", Creator = "Teld", LastModifier = "Teld", CreateTime = DateTime.Now, LastModifyTime = DateTime.Now, StrategyType = FlowControlStrategyType.Sum, IntThreshold = 100, TimeSpan = FlowControlTimespan.Second, IsRefusedRequest = false }; FlowControlService.FlowControl(strategy, 1); for (int i = 0; i < 300; i++) { FlowControlService.FlowControl(strategy, 1); } }
public DelayFlowController(FlowControlStrategy strategy) { FlowControlStrategy = strategy; }
public TPSFlowController(FlowControlStrategy strategy) { FlowControlStrategy = strategy; InnerThrottleStrategy = new FixedTokenBucket(strategy.IntThreshold, 1, 1000); }