限流算法笔记
我们为什么需要限流? 防止恶意攻击、保障系统稳定、应对秒杀等高并发场景 有哪些常见的限流算法? 固定窗口,滑动窗口,漏桶,令牌桶 固定窗口,缺点:比如 限流阀值为5个请求,单位时间窗口是1s,如果我们在单位时间内的前0.8-1s和1-1.2s,分别并发5个请求。虽然都没有超过阀值,但是如果算0.8-1.2s内的,则并发数高达10,已经超过单位时间1s不超过5阀值的定义了。 当滑动窗口的格子周期划分的越多,那么滑动窗口的滚动就越平滑,限流的统计就会越精确。 1234567891011121314151617181920212223242526272829303132-- 滑动窗口限流脚本(Redis 7 兼容:所有新变量均使用 local,避免写全局表导致 readonly 错误)local key = KEYS[1]local window = tonumber(ARGV[1]) -- 秒local limit = tonumber(ARGV[2])local now = tonumber(ARGV[3]) -- 毫秒if not window or...
Untitled
Q: 当前实现会对业务异常(如库存不足)抛出RuntimeException,导致消息重复投递。 1234567891011121314151617181920212223createVoucherOrder 中boolean success = seckillVoucherService.update() .setSql("stock = stock - 1") .eq("voucher_id", voucherOrder.getVoucherId()).gt("stock",0) .update();if (!success) { *log*.error("库存不足!"); //return; throw new...
20250908debug
今天用黑马点评加rocketmq,想测试一下模拟大量用户下单 项目代码见CuSO41108/hmdp_init 遇到了经典的分布式事务数据不一致的问题: 我用ai写的测试用例,因为上次生成5000个token用jmeter压测失败了(暂未解决) 第一次测试:redis中stock为1000,数据库也为1000,THREAD_COUNT = 5000。 结果: 数据库最后剩下9,redis:stock剩下0 (这个时候忘记看voucher_order和seckill_voucher了) 起初是问了ai: MQ重复消费 的可能性存在。必须保证handleVoucherOrder 和 createVoucherOrder 是 幂等的 已经有幂等校验: 123456789Long count = query().eq("user_id", userId).eq("voucher_id", voucherOrder.getVoucherId()).count();if (count > 0) { ...
待办事项工具
.quadrant-container { display: grid; grid-template-columns: 1fr 1fr; grid-template-rows: 1fr 1fr; gap: 10px; margin: 20px 0; } .quadrant { border: 2px solid #333; padding: 15px; min-height: 200px; border-radius: 10px; color: white; } #q1 { background-color: #ff4d4d; } #q2 { background-color: orange; } #q3 { background-color: #4da6ff; } #q4 { background-color: #66cc66; } body { /* Butterfly 主题的 body 可能被包裹,改用局部背景 */ background:...
Hello World
Welcome to Hexo! This is your very first post. Check documentation for more info. If you get any problems when using Hexo, you can find the answer in troubleshooting or you can ask me on GitHub. Quick StartCreate a new post1$ hexo new "My New Post" More info: Writing Run server1$ hexo server More info: Server Generate static files1$ hexo generate More info: Generating Deploy to remote sites1$ hexo deploy More info: Deployment
测试文章
这是一个测试Hello Hexo!
CS61Bnote
CS61B NOTE2.Lists2.1引入1234567b的变化会影响a吗? Walrus a = new Walrus(1000, 8.3);Walrus b;b = a;b.weight = 5;System.out.println(a);System.out.println(b); 会。它是一个指向 引用类型123456789public static class Walrus { public int weight; public double tuskSize; public Walrus(int w, double ts) { weight = w; tuskSize = ts; }} 参数传递函数传递参数时,实际上只是在复制位。 数组实例化1x = new int[]{0, 1, 2, 95, 4}; 2.2...