org.redisson.api.RRateLimiter Java Examples

The following examples show how to use org.redisson.api.RRateLimiter. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: RateLimiterTest.java    From blog with BSD 2-Clause "Simplified" License 6 votes vote down vote up
public static void main(String[] args) {
	Config config = new Config();
	config.useSingleServer().setAddress("redis://localhost:6379");
	RedissonClient client = Redisson.create(config);

	RRateLimiter rateLimiter = client.getRateLimiter("rate_limiter");
	rateLimiter.trySetRate(RateType.OVERALL, 1, 5, RateIntervalUnit.SECONDS);

	ExecutorService executorService = Executors.newFixedThreadPool(10);
	for (int i = 0; i < 10; i++) {
		executorService.submit(() -> {
			try {
				rateLimiter.acquire();
				System.out.println("时间:" + System.currentTimeMillis() + ",线程" + Thread.currentThread().getId()
						+ "进入数据区:" + System.currentTimeMillis());
			} catch (Exception e) {
				e.printStackTrace();
			}
		});
	}
}
 
Example #2
Source File: RateLimiterExamples.java    From redisson-examples with Apache License 2.0 6 votes vote down vote up
public static void main(String[] args) throws InterruptedException {
    // connects to 127.0.0.1:6379 by default
    RedissonClient redisson = Redisson.create();

    RRateLimiter limiter = redisson.getRateLimiter("myLimiter");
    // one permit per 2 seconds
    limiter.trySetRate(RateType.OVERALL, 1, 2, RateIntervalUnit.SECONDS);
    
    CountDownLatch latch = new CountDownLatch(2);
    limiter.acquire(1);
    latch.countDown();

    Thread t = new Thread(() -> {
        limiter.acquire(1);
        
        latch.countDown();
    });
    t.start();
    t.join();
    
    latch.await();
    
    redisson.shutdown();
}
 
Example #3
Source File: RedissonRateLimiterTest.java    From redisson with Apache License 2.0 6 votes vote down vote up
@Test
public void test() throws InterruptedException {
    RRateLimiter rr = redisson.getRateLimiter("test");
    assertThat(rr.trySetRate(RateType.OVERALL, 10, 1, RateIntervalUnit.SECONDS)).isTrue();
    assertThat(rr.trySetRate(RateType.OVERALL, 20, 1, RateIntervalUnit.SECONDS)).isFalse();
    
    for (int j = 0; j < 3; j++) {
        for (int i = 0; i < 10; i++) {
            assertThat(rr.tryAcquire()).isTrue();
        }
        for (int i = 0; i < 10; i++) {
            assertThat(rr.tryAcquire()).isFalse();
        }
        Thread.sleep(1050);
    }
}
 
Example #4
Source File: RedissonRateLimiterTest.java    From redisson with Apache License 2.0 5 votes vote down vote up
@Test
public void testRateConfig() {
    RRateLimiter rr = redisson.getRateLimiter("acquire");
    assertThat(rr.trySetRate(RateType.OVERALL, 1, 5, RateIntervalUnit.SECONDS)).isTrue();
    
    assertThat(rr.getConfig().getRate()).isEqualTo(1);
    assertThat(rr.getConfig().getRateInterval()).isEqualTo(5000);
    assertThat(rr.getConfig().getRateType()).isEqualTo(RateType.OVERALL);
}
 
Example #5
Source File: RedissonRateLimiterTest.java    From redisson with Apache License 2.0 5 votes vote down vote up
@Test
public void testPermitsExceeding() throws InterruptedException {
    RRateLimiter limiter = redisson.getRateLimiter("myLimiter");
    limiter.trySetRate(RateType.PER_CLIENT, 1, 1, RateIntervalUnit.SECONDS);
    
    Assertions.assertThatThrownBy(() -> limiter.tryAcquire(20)).hasMessageContaining("Requested permits amount could not exceed defined rate");
    assertThat(limiter.tryAcquire()).isTrue();
}
 
Example #6
Source File: RedissonRateLimiterTest.java    From redisson with Apache License 2.0 5 votes vote down vote up
@Test
public void testZeroTimeout() throws InterruptedException {
    RRateLimiter limiter = redisson.getRateLimiter("myLimiter");
    limiter.trySetRate(RateType.OVERALL, 5, 1, RateIntervalUnit.SECONDS);
    assertThat(limiter.availablePermits()).isEqualTo(5);

    assertThat(limiter.tryAcquire(1, 0, TimeUnit.SECONDS)).isTrue();
    assertThat(limiter.tryAcquire(1, 0, TimeUnit.SECONDS)).isTrue();
    assertThat(limiter.availablePermits()).isEqualTo(3);
    assertThat(limiter.tryAcquire(1, 0, TimeUnit.SECONDS)).isTrue();
    assertThat(limiter.tryAcquire(1, 0, TimeUnit.SECONDS)).isTrue();
    assertThat(limiter.availablePermits()).isEqualTo(1);
    assertThat(limiter.tryAcquire(1, 0, TimeUnit.SECONDS)).isTrue();

    assertThat(limiter.availablePermits()).isEqualTo(0);

    assertThat(limiter.tryAcquire(1, 0, TimeUnit.SECONDS)).isFalse();
    assertThat(limiter.tryAcquire(1, 0, TimeUnit.SECONDS)).isFalse();
    assertThat(limiter.tryAcquire(1, 0, TimeUnit.SECONDS)).isFalse();
    assertThat(limiter.tryAcquire(1, 0, TimeUnit.SECONDS)).isFalse();
    assertThat(limiter.tryAcquire(1, 0, TimeUnit.SECONDS)).isFalse();
    
    Thread.sleep(1000);
    
    assertThat(limiter.tryAcquire(1, 0, TimeUnit.SECONDS)).isTrue();
    assertThat(limiter.tryAcquire(1, 0, TimeUnit.SECONDS)).isTrue();
    assertThat(limiter.tryAcquire(1, 0, TimeUnit.SECONDS)).isTrue();
    assertThat(limiter.tryAcquire(1, 0, TimeUnit.SECONDS)).isTrue();
    assertThat(limiter.tryAcquire(1, 0, TimeUnit.SECONDS)).isTrue();
    
    assertThat(limiter.tryAcquire(1, 0, TimeUnit.SECONDS)).isFalse();
    assertThat(limiter.tryAcquire(1, 0, TimeUnit.SECONDS)).isFalse();
    assertThat(limiter.tryAcquire(1, 0, TimeUnit.SECONDS)).isFalse();
    assertThat(limiter.tryAcquire(1, 0, TimeUnit.SECONDS)).isFalse();
    assertThat(limiter.tryAcquire(1, 0, TimeUnit.SECONDS)).isFalse();
}
 
Example #7
Source File: RedissonRateLimiterTest.java    From redisson with Apache License 2.0 5 votes vote down vote up
@Test(timeout = 1500)
public void testTryAcquire() {
    RRateLimiter rr = redisson.getRateLimiter("acquire");
    assertThat(rr.trySetRate(RateType.OVERALL, 1, 5, RateIntervalUnit.SECONDS)).isTrue();

    assertThat(rr.tryAcquire(1, 1, TimeUnit.SECONDS)).isTrue();
    assertThat(rr.tryAcquire(1, 1, TimeUnit.SECONDS)).isFalse();
    assertThat(rr.tryAcquire()).isFalse();
}
 
Example #8
Source File: RedissonRateLimiterTest.java    From redisson with Apache License 2.0 5 votes vote down vote up
@Test
public void testAcquire() {
    RRateLimiter rr = redisson.getRateLimiter("acquire");
    assertThat(rr.trySetRate(RateType.OVERALL, 1, 5, RateIntervalUnit.SECONDS)).isTrue();
    for (int i = 0; i < 10; i++) {
        rr.acquire(1);
    }
    assertThat(rr.tryAcquire()).isFalse();
}
 
Example #9
Source File: TracingRedissonClient.java    From java-redis-client with Apache License 2.0 4 votes vote down vote up
@Override
public RRateLimiter getRateLimiter(String name) {
  return redissonClient.getRateLimiter(name);
}
 
Example #10
Source File: RedissonRateLimiterTest.java    From redisson with Apache License 2.0 4 votes vote down vote up
@Test
public void testConcurrency() throws InterruptedException {
    RRateLimiter rr = redisson.getRateLimiter("test");
    assertThat(rr.trySetRate(RateType.OVERALL, 10, 1, RateIntervalUnit.SECONDS)).isTrue();
    assertThat(rr.trySetRate(RateType.OVERALL, 20, 1, RateIntervalUnit.SECONDS)).isFalse();
    
    Queue<Long> queue = new ConcurrentLinkedQueue<Long>();
    AtomicLong counter = new AtomicLong();
    ExecutorService pool = Executors.newFixedThreadPool(8);
    for (int i = 0; i < 8; i++) {
        pool.execute(new Runnable() {
            @Override
            public void run() {
                while (true) {
                    if (rr.tryAcquire()) {
                        if (counter.incrementAndGet() > 500) {
                            break;
                        }
                        queue.add(System.currentTimeMillis());
                    }
                    try {
                        Thread.sleep(ThreadLocalRandom.current().nextInt(10));
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    
                }
            }
        });
    }
    
    pool.shutdown();
    assertThat(pool.awaitTermination(1, TimeUnit.MINUTES)).isTrue();
    
    int count = 0;
    long start = 0;
    for (Long value : queue) {
        if (count % 10 == 0) {
            if (start > 0) {
                assertThat(value - start).isGreaterThan(990);
            }
            start = value;
        }
        count++;
    }
}