Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,13 @@ local timestamp_key = KEYS[2]

local rate = tonumber(ARGV[1])
local capacity = tonumber(ARGV[2])
local now = tonumber(ARGV[3]) or redis.call('TIME')[1]

local now = tonumber(ARGV[3])
if not now then
local redis_time = redis.call('TIME')
now = redis_time[1] + (redis_time[2] / 1000000)
end

local requested = tonumber(ARGV[4])

local fill_time = capacity / rate
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,10 @@ static List<String> getArgs(long rate, long capacity, long now, long requested)
return Arrays.asList(rate + "", capacity + "", now + "", requested + "");
}

static List<String> getArgsWithRedisTime(long rate, long capacity, long requested) {
return Arrays.asList(rate + "", capacity + "", "", requested + "");
}

@Test
public void testNewAccess() {
long rate = 1;
Expand Down Expand Up @@ -164,6 +168,24 @@ public void testCapacityExceedsMaxInt() {
assertThat(result.get(1)).isEqualTo(REDIS_LUA_MAX_SAFE_INTEGER - 1);
}

@Test
public void testRedisTimeSupportsSubSecondRefill() throws InterruptedException {
long rate = 10;
long capacity = 10;
List<String> keys = getKeys("sub_second_refill");

List<String> args = getArgsWithRedisTime(rate, capacity, capacity);
List<Long> first = redisTemplate.execute(redisScript, keys, args).blockFirst();
assertThat(first.get(0)).isEqualTo(1);
assertThat(first.get(1)).isEqualTo(0);

Thread.sleep(250);

args = getArgsWithRedisTime(rate, capacity, 1);
List<Long> second = redisTemplate.execute(redisScript, keys, args).blockFirst();
assertThat(second.get(0)).isEqualTo(1);
}

@EnableAutoConfiguration
@SpringBootConfiguration
public static class TestConfig {
Expand Down