-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement rateLimit configuration (#84)
Very similar to throttle implementation in #66 The test timings need to account for GCRA buckets https://www.inngest.com/docs/guides/rate-limiting#how-rate-limiting-works
- Loading branch information
1 parent
befe29b
commit 2e01336
Showing
5 changed files
with
104 additions
and
0 deletions.
There are no files selected for viewing
28 changes: 28 additions & 0 deletions
28
...boot-demo/src/main/java/com/inngest/springbootdemo/testfunctions/RateLimitedFunction.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package com.inngest.springbootdemo.testfunctions; | ||
|
||
import com.inngest.FunctionContext; | ||
import com.inngest.InngestFunction; | ||
import com.inngest.InngestFunctionConfigBuilder; | ||
import com.inngest.Step; | ||
import org.jetbrains.annotations.NotNull; | ||
|
||
import java.time.Duration; | ||
|
||
public class RateLimitedFunction extends InngestFunction { | ||
|
||
@NotNull | ||
@Override | ||
public InngestFunctionConfigBuilder config(InngestFunctionConfigBuilder builder) { | ||
return builder | ||
.id("RateLimitedFunction") | ||
.name("RateLimited Function") | ||
.triggerEvent("test/rateLimit") | ||
.rateLimit(2, Duration.ofSeconds(6)); | ||
} | ||
|
||
@Override | ||
public Integer execute(FunctionContext ctx, Step step) { | ||
return step.run("result", () -> 42, Integer.class); | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
46 changes: 46 additions & 0 deletions
46
...oot-demo/src/test/java/com/inngest/springbootdemo/RateLimitedFunctionIntegrationTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package com.inngest.springbootdemo; | ||
|
||
import com.inngest.Inngest; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.parallel.Execution; | ||
import org.junit.jupiter.api.parallel.ExecutionMode; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
|
||
@IntegrationTest | ||
@Execution(ExecutionMode.CONCURRENT) | ||
class RateLimitedFunctionIntegrationTest { | ||
@Autowired | ||
private DevServerComponent devServer; | ||
|
||
@Autowired | ||
private Inngest client; | ||
|
||
@Test | ||
void testFunctionIsRateLimited() throws Exception { | ||
String event1 = InngestFunctionTestHelpers.sendEvent(client, "test/rateLimit").getIds()[0]; | ||
// Rate limit test function is limited to 2 over 6 seconds. Based on the simplistic description of GCRA in | ||
// https://www.inngest.com/docs/guides/rate-limiting#how-rate-limiting-works | ||
// we need to sleep at least 3 seconds here for the second event not to get rate limited | ||
Thread.sleep(3500); | ||
String event2 = InngestFunctionTestHelpers.sendEvent(client, "test/rateLimit").getIds()[0]; | ||
Thread.sleep(1000); | ||
String event3 = InngestFunctionTestHelpers.sendEvent(client, "test/rateLimit").getIds()[0]; | ||
|
||
// Sleep at least 6 seconds for the rate limit bucket to be completely cleared | ||
Thread.sleep(6000); | ||
|
||
// Rate limit should only allow the first 2 events to run | ||
assertEquals("Completed", devServer.runsByEvent(event1).first().getStatus()); | ||
assertEquals("Completed", devServer.runsByEvent(event2).first().getStatus()); | ||
assertEquals(0, devServer.runsByEvent(event3).data.length); | ||
|
||
// new event after the rate limit period will run, but the previously skipped event will stay skipped | ||
String event4 = InngestFunctionTestHelpers.sendEvent(client, "test/rateLimit").getIds()[0]; | ||
Thread.sleep(4000); | ||
|
||
assertEquals(0, devServer.runsByEvent(event3).data.length); | ||
assertEquals("Completed", devServer.runsByEvent(event4).first().getStatus()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters