forked from iluwatar/java-design-patterns
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feature: Rate Limiting pattern (iluwatar#2973)
Заготовка архитектуры
- Loading branch information
Каспшицкий Алексей
committed
Jul 4, 2024
1 parent
6b67c0e
commit 00b4aac
Showing
8 changed files
with
210 additions
and
7 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
/* | ||
* This project is licensed under the MIT license. Module model-view-viewmodel is using ZK framework licensed under LGPL (see lgpl-3.0.txt). | ||
* | ||
* The MIT License | ||
* Copyright © 2014-2022 Ilkka Seppälä | ||
* | ||
* Permission is hereby granted, free of charge, to any person obtaining a copy | ||
* of this software and associated documentation files (the "Software"), to deal | ||
* in the Software without restriction, including without limitation the rights | ||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
* copies of the Software, and to permit persons to whom the Software is | ||
* furnished to do so, subject to the following conditions: | ||
* | ||
* The above copyright notice and this permission notice shall be included in | ||
* all copies or substantial portions of the Software. | ||
* | ||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
* THE SOFTWARE. | ||
*/ | ||
package com.iluwatar; | ||
|
||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.boot.SpringApplication; | ||
|
||
/** | ||
* App class. | ||
*/ | ||
@Slf4j | ||
public class App { | ||
|
||
/** | ||
* Program entry point. | ||
*/ | ||
public static void main(String[] args) { | ||
SpringApplication app = new SpringApplication(App.class); | ||
System.out.println("Hello world!"); | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
17 changes: 17 additions & 0 deletions
17
rate-limiting/src/main/java/com/iluwatar/aspect/RateLimited.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,17 @@ | ||
package com.iluwatar.aspect; | ||
|
||
import java.util.concurrent.TimeUnit; | ||
|
||
/** | ||
* AOP annotation. | ||
*/ | ||
public @interface RateLimited { | ||
/** | ||
* invoke method vialotation for one ip address. | ||
*/ | ||
int count() default 10; | ||
/** | ||
* time unit for reset vialotation. | ||
*/ | ||
TimeUnit timeUnit() default TimeUnit.SECONDS; | ||
} |
68 changes: 68 additions & 0 deletions
68
rate-limiting/src/main/java/com/iluwatar/aspect/RateLimiterAspect.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,68 @@ | ||
package com.iluwatar.aspect; | ||
|
||
import lombok.extern.slf4j.Slf4j; | ||
import org.aspectj.lang.JoinPoint; | ||
import org.aspectj.lang.annotation.AfterReturning; | ||
import org.aspectj.lang.annotation.AfterThrowing; | ||
import org.aspectj.lang.annotation.Aspect; | ||
import org.aspectj.lang.annotation.Before; | ||
import org.aspectj.lang.annotation.Pointcut; | ||
import org.springframework.stereotype.Component; | ||
|
||
/** | ||
* Aspect for rate limiting functionality using Spring AOP. | ||
*/ | ||
@Slf4j | ||
@Aspect | ||
@Component | ||
public class RateLimiterAspect { | ||
|
||
/** | ||
* Pointcut to intercept methods annotated with @RateLimited. | ||
*/ | ||
@Pointcut("@annotation(com.iluwatar.aspect.RateLimited)") | ||
public void callMethodWithAnnotation() { } | ||
|
||
/** | ||
* Advice executed before methods annotated with @RateLimited are invoked. | ||
*/ | ||
@Before( | ||
value = "callMethodWithAnnotation()", argNames = "jp") | ||
public void endpointBeforeInvoke(JoinPoint jp) { | ||
String methodName = jp.getSignature().getName(); | ||
LOGGER.info("Before executing method: {}", methodName); | ||
} | ||
|
||
/** | ||
* Advice executed after methods annotated with @RateLimited successfully return. | ||
* | ||
* @param jp JoinPoint that intercepted the method call. | ||
* @param returningValue Value returned by the intercepted method. | ||
* @return The same returningValue as passed. | ||
*/ | ||
@AfterReturning( | ||
value = "callMethodWithAnnotation()", | ||
argNames = "jp,returningValue", | ||
returning = "returningValue") | ||
public Object endpointAfterReturning(JoinPoint jp, Object returningValue) { | ||
String methodName = jp.getSignature().getName(); | ||
LOGGER.info("Before executing method: {}", methodName); | ||
return returningValue; | ||
} | ||
|
||
/** | ||
* Advice executed after methods annotated with @RateLimited throw an exception. | ||
* | ||
* @param jp JoinPoint that intercepted the method call. | ||
* @param e Exception thrown by the intercepted method. | ||
*/ | ||
@AfterThrowing( | ||
pointcut = "callMethodWithAnnotation()", | ||
argNames = "jp,e", | ||
throwing = "e") | ||
public void endpointAfterThrowing(JoinPoint jp, Exception e) { | ||
String methodName = jp.getSignature().getName(); | ||
LOGGER.info("Before executing method: {}", methodName); | ||
} | ||
|
||
} |
26 changes: 26 additions & 0 deletions
26
rate-limiting/src/main/java/com/iluwatar/controller/RateLimitedController.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,26 @@ | ||
package com.iluwatar.controller; | ||
|
||
import com.iluwatar.aspect.RateLimited; | ||
import com.iluwatar.model.DtoClass; | ||
import java.util.concurrent.TimeUnit; | ||
import org.springframework.web.bind.annotation.GetMapping; | ||
import org.springframework.web.bind.annotation.RestController; | ||
|
||
/** | ||
* Rest API controller for example rate limited. | ||
*/ | ||
@RestController | ||
public class RateLimitedController { | ||
|
||
/** | ||
* Simple GET method. | ||
* | ||
* @return Simple DTO object. | ||
*/ | ||
@RateLimited(count = 15, timeUnit = TimeUnit.MINUTES) | ||
@GetMapping("test-request") | ||
public DtoClass getRequest() { | ||
return new DtoClass(); | ||
} | ||
|
||
} |
15 changes: 15 additions & 0 deletions
15
rate-limiting/src/main/java/com/iluwatar/model/DtoClass.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,15 @@ | ||
package com.iluwatar.model; | ||
|
||
import lombok.Data; | ||
|
||
/** | ||
* Example dto for controller. | ||
*/ | ||
@Data | ||
public class DtoClass { | ||
|
||
private Long id = 1L; | ||
|
||
private String name = "dto for example"; | ||
|
||
} |
11 changes: 11 additions & 0 deletions
11
rate-limiting/src/main/java/com/iluwatar/pattern/RateLimiter.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,11 @@ | ||
package com.iluwatar.pattern; | ||
|
||
import org.springframework.stereotype.Component; | ||
|
||
/** | ||
* Realization Rate Limiting. | ||
*/ | ||
@Component | ||
public class RateLimiter { | ||
|
||
} |