漏斗算法详解
? ? ? ? 漏斗是生活中一個非常常見的容器,他自身的形狀決定了其擁有的性質,上面寬、下邊窄,上面進水快,而窄的那頭出水較慢。比如生活中,我們需要向開水瓶里灌水,用一個漏斗會非常的方便。
? ? ? ? 我們用熱水壺通過漏斗往開水瓶里倒水,水會沿著漏斗順流到開水瓶里,如果這個時候你不小心手抖了,一下倒入很多,你會發現漏斗里的水不能一下子進到開水瓶里而是滿了出來。
? ? ? ? 這是一個簡單的數學問題,可以用以下表達式說明:
? ? ? ?if? ?單位時間內的進水速率> 單位時間內出水速率 :
? ? ? ? ? ? print("漏斗里的水會一直增加!... 直到溢出")
? ? ? else?
? ? ? ? ? ?print("漏斗永遠裝不滿...水一直會順著流出。")
? ? ? ? 如果我們能控制進水和出水的速率,那么可以借助漏斗實現單位時間內流量的限制,即限流。
? ? ? ? 實現思路:
? ? ? ? 1.? 定義一個Funnel 靜態內部類,相當于漏斗,可以初始化容量和漏斗大口的出水速率。
? ? ? ? 2.? 定義進水方法watering(int capacity),參數為每次出水的量,默認為1。
? ? ? ? 3.? 定義出水方法runningWater(), 由于每次流出水需要統計量,那么就需要記錄每次入水的時間戳,以供計算容量,出水的同時剩余容量leftCapcity需要加上出水量 this.leftCapacity += deltaCapacity。
? ? ? ? ? 單位時間內的出水量(deltaCapacity)?=? 速率(waterRate) * (當前出水的時間戳ts- 上一次出水的時間戳ts)
? ? ? ? 4. 如果出水的量超過了漏斗的最大容量,那么直接將leftCapacity置為最大容量,也就是說此刻漏斗里相當于沒有水。
? ? ? ? 如果漏斗的剩余容量> 一次性進水量,那么表示水可以從漏斗中通行,否則不能通行直接return。
package leetcode100;import java.math.BigDecimal; import java.util.Map; import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.atomic.AtomicInteger; import java.util.concurrent.locks.ReentrantLock; import java.util.function.Function;/*** 漏斗算法* Funnel algorithm*/ public class FunnelProblem {/*** record a logic key and funnel*/private static Map<String, Funnel> funnelMap = new ConcurrentHashMap<>();static class Funnel {private ReentrantLock lock = new ReentrantLock();// 水流速度private BigDecimal waterRate;// 漏斗容量private Integer capacity;// 漏斗剩余容量private Integer leftCapacity;// 上一次訪問時間戳private Long lastAccess;public Funnel(BigDecimal waterRate, Integer capacity) {this.waterRate = waterRate;this.capacity = capacity;this.leftCapacity = capacity;this.lastAccess = System.currentTimeMillis();}/*** water out, space add*/public void runningWater() {long nowTime = System.currentTimeMillis();long deltaTime = nowTime - lastAccess;Integer deltaCapacity = waterRate.multiply(new BigDecimal(deltaTime)).intValue();// the deltaCapacity means how much water are in .// if the interval are too long, it will be over Max Integerif (deltaCapacity < 0) {this.leftCapacity = capacity;this.lastAccess = nowTime;return;}if (deltaCapacity < 1) {return;}this.leftCapacity += deltaCapacity;this.lastAccess = nowTime;if (this.leftCapacity > this.capacity) {this.leftCapacity = this.capacity;}}/*** watering.*/public boolean watering(Integer waterCapacity) {runningWater();if (leftCapacity > waterCapacity) {this.leftCapacity -= waterCapacity;return true;}return false;}}public boolean allow(String userId, String key, Integer initCapacity, BigDecimal leakRate) {String userKey = userId + key;Funnel funnel = funnelMap.computeIfAbsent(userKey, s -> new Funnel(leakRate, initCapacity));return funnel.watering(1);}private static AtomicInteger countAllow = new AtomicInteger(0);private static AtomicInteger countDisAllow = new AtomicInteger(0);public static void main(String[] args) {FunnelProblem funnelProblem = new FunnelProblem();for (int i = 0; i < 1000; i++) {new Thread(() -> {Boolean allow = funnelProblem.allow("zhangsan", "browser", 500, BigDecimal.valueOf(1));if (allow) {System.out.println("you can browser!");countAllow.addAndGet(1);} else {System.out.println("pls wait !");countDisAllow.addAndGet(1);}}).start();}try {Thread.sleep(1000);} catch (InterruptedException e) {e.printStackTrace();}System.out.println("可以瀏覽 " + countAllow.get() + " 人次," + "等待了 " + countDisAllow.get() + " 人次");}}模擬1000流量同時進入到漏斗,查看打印結果:
? ? ? ? 1000人中,可以瀏覽 546 人次,等待了 454 人次,我們可以通過設置initCapacity 和 leakRate的值來初始化每個漏斗的大小和進水的速率。
總結
- 上一篇: echarts3 实现中国地图
- 下一篇: Magento热销产品Luxe_Best