Skip to content

Commit 54173bc

Browse files
Programming Challenge redislabs-training#7
1 parent 3390941 commit 54173bc

2 files changed

Lines changed: 39 additions & 20 deletions

File tree

Lines changed: 39 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,46 @@
11
package com.redislabs.university.RU102J.dao;
22

3+
import com.redislabs.university.RU102J.core.KeyHelper;
4+
5+
import java.time.ZonedDateTime;
6+
import java.util.UUID;
7+
8+
import redis.clients.jedis.Jedis;
39
import redis.clients.jedis.JedisPool;
10+
import redis.clients.jedis.Response;
11+
import redis.clients.jedis.Transaction;
412

513
public class RateLimiterSlidingDaoRedisImpl implements RateLimiter {
614

7-
private final JedisPool jedisPool;
8-
private final long windowSizeMS;
9-
private final long maxHits;
10-
11-
public RateLimiterSlidingDaoRedisImpl(JedisPool pool, long windowSizeMS,
12-
long maxHits) {
13-
this.jedisPool = pool;
14-
this.windowSizeMS = windowSizeMS;
15-
this.maxHits = maxHits;
16-
}
17-
18-
// Challenge #7
19-
@Override
20-
public void hit(String name) throws RateLimitExceededException {
21-
// START CHALLENGE #7
22-
// END CHALLENGE #7
23-
}
15+
private final JedisPool jedisPool;
16+
private final long windowSizeMS;
17+
private final long maxHits;
18+
19+
public RateLimiterSlidingDaoRedisImpl( JedisPool pool, long windowSizeMS, long maxHits ) {
20+
this.jedisPool = pool;
21+
this.windowSizeMS = windowSizeMS;
22+
this.maxHits = maxHits;
23+
}
24+
25+
// Challenge #7
26+
@Override
27+
public void hit( String name ) throws RateLimitExceededException {
28+
// START CHALLENGE #7
29+
try ( Jedis jedis = jedisPool.getResource(); Transaction t = jedis.multi() ) {
30+
long currentMs = ZonedDateTime.now().toInstant().toEpochMilli();
31+
32+
// make the member unique in order not to replace the score of another one
33+
String member = String.format( "%s:%s", currentMs, UUID.randomUUID() );
34+
String key = KeyHelper.getKey( String.format( "limiter:%s:%s:maxHits", windowSizeMS, name ) );
35+
t.zadd( key, currentMs, member );
36+
t.zremrangeByScore( key, 0, currentMs - windowSizeMS ); // remove hits before the current sliding window
37+
Response<Long> hitCount = t.zcard( key );
38+
t.exec();
39+
40+
if ( hitCount.get() > maxHits ) {
41+
throw new RateLimitExceededException();
42+
}
43+
}
44+
// END CHALLENGE #7
45+
}
2446
}

src/test/java/com/redislabs/university/RU102J/dao/RateLimiterSlidingDaoRedisImplTest.java

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,6 @@ public void flush() {
4747
keyManager.deleteKeys(jedis);
4848
}
4949

50-
@Ignore
5150
@Test
5251
public void hit() {
5352
int exceptionCount = 0;
@@ -64,7 +63,6 @@ public void hit() {
6463
assertThat(exceptionCount, is(0));
6564
}
6665

67-
@Ignore
6866
@Test
6967
public void hitOutsideLimit() {
7068
int exceptionCount = 0;
@@ -81,7 +79,6 @@ public void hitOutsideLimit() {
8179
assertThat(exceptionCount, is(2));
8280
}
8381

84-
@Ignore
8582
@Test
8683
public void hitOutsideWindow() throws InterruptedException {
8784
int exceptionCount = 0;

0 commit comments

Comments
 (0)