限流的一些思考(2)

一:RateLimiter

RateLimiter是guava提供的基于令牌桶算法的实现类,可以非常简单的完成限流特技,并且根据系统的实际情况来调整生成token的速率。

rateLimiter.acquire()该方法会阻塞线程,直到令牌桶中能取到令牌为止才继续向下执行,并返回等待的时间。

二:DelayQueue

是一个无界的BlockingQueue,用于放置实现了Delayed接口的对象,其中的对象只能在其到期时才能从队列中取走。这种队列是有序的,即队头对象的延迟到期时间最长。注意:不能将null元素放置到这种队列中。

Delayed,一种混合风格的接口,用来标记那些应该在给定延迟时间之后执行的对象。此接口的实现必须定义一个 compareTo 方法,该方法提供与此接口的 getDelay 方法一致的排序。

三:几个例子解析

有很多任务,但希望每秒不超过N个

  1. import com.google.common.util.concurrent.RateLimiter;  
  2.   
  3. import java.util.ArrayList;  
  4. import java.util.List;  
  5. import java.util.concurrent.ExecutorService;  
  6. import java.util.concurrent.Executors;  
  7.   
  8. /** 
  9.  * Created by wuwf on 17/7/11. 
  10.  * 有很多个任务,但希望每秒不超过X个,可用此类 
  11.  */  
  12. public class Demo1 {  
  13.   
  14.     public static void main(String[] args) {  
  15.         //0.5代表一秒最多多少个  
  16.         RateLimiter rateLimiter = RateLimiter.create(0.5);  
  17.         List<Runnable> tasks = new ArrayList<Runnable>();  
  18.         for (int i = 0; i < 10; i++) {  
  19.             tasks.add(new UserRequest(i));  
  20.         }  
  21.         ExecutorService threadPool = Executors.newCachedThreadPool();  
  22.         for (Runnable runnable : tasks) {  
  23.             System.out.println("等待时间:" + rateLimiter.acquire());  
  24.             threadPool.execute(runnable);  
  25.         }  
  26.     }  
  27.   
  28.     private static class UserRequest implements Runnable {  
  29.         private int id;  
  30.   
  31.         public UserRequest(int id) {  
  32.             this.id = id;  
  33.         }  
  34.   
  35.         public void run() {  
  36.             System.out.println(id);  
  37.         }  
  38.     }  
  39.   
2. 抢购场景限流

  1. import com.google.common.util.concurrent.RateLimiter;  
  2. import com.tianyalei.model.GoodInfo;  
  3. import com.tianyalei.service.GoodInfoService;  
  4. import org.springframework.web.bind.annotation.RequestMapping;  
  5. import org.springframework.web.bind.annotation.RestController;  
  6.   
  7. import javax.annotation.Resource;  
  8.   
  9. /** 
  10.  * Created by wuwf on 17/7/11. 
  11.  */  
  12. @RestController  
  13. public class IndexController {  
  14.     @Resource(name = "db")  
  15.     private GoodInfoService goodInfoService;  
  16.   
  17.     RateLimiter rateLimiter = RateLimiter.create(10);  
  18.   
  19.     @RequestMapping("/miaosha")  
  20.     public Object miaosha(int count, String code) {  
  21.         System.out.println("等待时间" + rateLimiter.acquire());  
  22.         if (goodInfoService.update(code, count) > 0) {  
  23.             return "购买成功";  
  24.         }  
  25.         return "购买失败";  
  26.     }  
  27.   
  28.   
  29.   
  30.     @RequestMapping("/add")  
  31.     public Object add() {  
  32.         for (int i = 0; i < 100; i++) {  
  33.             GoodInfo goodInfo = new GoodInfo();  
  34.             goodInfo.setCode("iphone" + i);  
  35.             goodInfo.setAmount(100);  
  36.             goodInfoService.add(goodInfo);  
  37.         }  
  38.   
  39.         return "添加成功";  
  40.     }  
3.抢购场景降级
上面的例子虽然限制了单位时间内对DB的操作,但是对用户是不友好的,因为他需要等待,不能迅速的得到响应。当你有1万个并发请求,一秒只能处理10个,那剩余的用户都会陷入漫长的等待。所以我们需要对应用降级,一旦判断出某些请求是得不到令牌的,就迅速返回失败,避免无谓的等待。
由于RateLimiter是属于单位时间内生成多少个令牌的方式,譬如0.1秒生成1个,那抢购就要看运气了,你刚好是在刚生成1个时进来了,那么你就能抢到,在这0.1秒内其他的请求就算白瞎了,只能寄希望于下一个0.1秒,而从用户体验上来说,不能让他在那一直阻塞等待,所以就需要迅速判断,该用户在某段时间内,还有没有机会得到令牌,这里就需要使用tryAcquire(long timeout, TimeUnit unit)方法,指定一个超时时间,一旦判断出在timeout时间内还无法取得令牌,就返回false。 注意,这里并不是真正的等待了timeout时间,而是被判断为即便过了timeout时间,也无法取得令牌。这个是不需要等待的
  1. /** 
  2.      * tryAcquire(long timeout, TimeUnit unit) 
  3.      * 从RateLimiter 获取许可如果该许可可以在不超过timeout的时间内获取得到的话, 
  4.      * 或者如果无法在timeout 过期之前获取得到许可的话,那么立即返回false(无需等待) 
  5.      */  
  6.     @RequestMapping("/buy")  
  7.     public Object miao(int count, String code) {  
  8.         //判断能否在1秒内得到令牌,如果不能则立即返回false,不会阻塞程序  
  9.         if (!rateLimiter.tryAcquire(1000, TimeUnit.MILLISECONDS)) {  
  10.             System.out.println("短期无法获取令牌,真不幸,排队也瞎排");  
  11.             return "失败";  
  12.         }  
  13.         if (goodInfoService.update(code, count) > 0) {  
  14.             System.out.println("购买成功");  
  15.             return "成功";  
  16.         }  
  17.         System.out.println("数据不足,失败");  
  18.         return "失败";  
  19.     }  
4. 多考生考试

该场景来自于http://ideasforjava.iteye.com/blog/657384,模拟一个考试的日子,考试时间为120分钟,30分钟后才可交卷,当时间到了,或学生都交完卷了考试结束。

这个场景中几个点需要注意:

  1. 考试时间为120分钟,30分钟后才可交卷,初始化考生完成试卷时间最小应为30分钟
  2. 对于能够在120分钟内交卷的考生,如何实现这些考生交卷
  3. 对于120分钟内没有完成考试的考生,在120分钟考试时间到后需要让他们强制交卷
  4. 在所有的考生都交完卷后,需要将控制线程关闭

实现思想:用DelayQueue存储考生(Student类),每一个考生都有自己的名字和完成试卷的时间,Teacher线程对DelayQueue进行监控,收取完成试卷小于120分钟的学生的试卷。当考试时间120分钟到时,先关闭Teacher线程,然后强制DelayQueue中还存在的考生交卷。每一个考生交卷都会进行一次countDownLatch.countDown(),当countDownLatch.await()不再阻塞说明所有考生都交完卷了,而后结束考试。

package com.my.base.concurrent.delayQueue;

import java.util.Iterator;
import java.util.Random;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.DelayQueue;
import java.util.concurrent.Delayed;
import java.util.concurrent.TimeUnit;

/**
 *this project is created for my partactice.
 *In the  project I will write the mybatis by myself
 *
 *2014-1-10  下午9:43:48
 *@author 孙振超   mychaoyue2011@163.com
 */

public class Exam {

    /**
     *
     *2014-1-10 下午9:43:48 by 孙振超
     *
     *@param args
     *void
     * @throws InterruptedException 
     */
    public static void main(String[] args) throws InterruptedException {
        // TODO Auto-generated method stub
        int studentNumber = 20;
        CountDownLatch countDownLatch = new CountDownLatch(studentNumber+1);
        DelayQueue< Student> students = new DelayQueue<Student>();
        Random random = new Random();
        for (int i = 0; i < studentNumber; i++) {
            students.put(new Student("student"+(i+1), 30+random.nextInt(120),countDownLatch));
        }
        Thread teacherThread =new Thread(new Teacher(students)); 
        students.put(new EndExam(students, 120,countDownLatch,teacherThread));
        teacherThread.start();
        countDownLatch.await();
        System.out.println(" 考试时间到,全部交卷!");  
    }

}

class Student implements Runnable,Delayed{

    private String name;
    private long workTime;
    private long submitTime;
    private boolean isForce = false;
    private CountDownLatch countDownLatch;
    
    public Student(){}
    
    public Student(String name,long workTime,CountDownLatch countDownLatch){
        this.name = name;
        this.workTime = workTime;
        this.submitTime = TimeUnit.NANOSECONDS.convert(workTime, TimeUnit.NANOSECONDS)+System.nanoTime();
        this.countDownLatch = countDownLatch;
    }
    
    @Override
    public int compareTo(Delayed o) {
        // TODO Auto-generated method stub
        if(o == null || ! (o instanceof Student)) return 1;
        if(o == this) return 0; 
        Student s = (Student)o;
        if (this.workTime > s.workTime) {
            return 1;
        }else if (this.workTime == s.workTime) {
            return 0;
        }else {
            return -1;
        }
    }

    @Override
    public long getDelay(TimeUnit unit) {
        // TODO Auto-generated method stub
        return unit.convert(submitTime - System.nanoTime(),  TimeUnit.NANOSECONDS);
    }

    @Override
    public void run() {
        // TODO Auto-generated method stub
        if (isForce) {
            System.out.println(name + " 交卷, 希望用时" + workTime + "分钟"+" ,实际用时 120分钟" );
        }else {
            System.out.println(name + " 交卷, 希望用时" + workTime + "分钟"+" ,实际用时 "+workTime +" 分钟");  
        }
        countDownLatch.countDown();
    }

    public boolean isForce() {
        return isForce;
    }

    public void setForce(boolean isForce) {
        this.isForce = isForce;
    }
    
}

class EndExam extends Student{

    private DelayQueue<Student> students;
    private CountDownLatch countDownLatch;
    private Thread teacherThread;
    
    public EndExam(DelayQueue<Student> students, long workTime, CountDownLatch countDownLatch,Thread teacherThread) {
        super("强制收卷", workTime,countDownLatch);
        this.students = students;
        this.countDownLatch = countDownLatch;
        this.teacherThread = teacherThread;
    }
    
    
    
    @Override
    public void run() {
        // TODO Auto-generated method stub
        
        teacherThread.interrupt();
        Student tmpStudent;
        for (Iterator<Student> iterator2 = students.iterator(); iterator2.hasNext();) {
            tmpStudent = iterator2.next();
            tmpStudent.setForce(true);
            tmpStudent.run();
        }
        countDownLatch.countDown();
    }
    
}

class Teacher implements Runnable{

    private DelayQueue<Student> students;
    public Teacher(DelayQueue<Student> students){
        this.students = students;
    }
    
    @Override
    public void run() {
        // TODO Auto-generated method stub
        try {
            System.out.println(" test start");
            while(!Thread.interrupted()){
                students.take().run();
            }
        } catch (Exception e) {
            // TODO: handle exception
            e.printStackTrace();
        }
    }
    
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值