Skip to content

Commit 00b4aac

Browse files
author
Каспшицкий Алексей
committed
feature: Rate Limiting pattern (iluwatar#2973)
Заготовка архитектуры
1 parent 6b67c0e commit 00b4aac

File tree

8 files changed

+210
-7
lines changed

8 files changed

+210
-7
lines changed

Diff for: rate-limiting/pom.xml

+30
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,22 @@
3434
</parent>
3535
<artifactId>rate-limiting</artifactId>
3636
<dependencies>
37+
<dependency>
38+
<groupId>org.springframework</groupId>
39+
<artifactId>spring-webmvc</artifactId>
40+
</dependency>
41+
<dependency>
42+
<groupId>org.springframework.boot</groupId>
43+
<artifactId>spring-boot-starter-web</artifactId>
44+
</dependency>
45+
<dependency>
46+
<groupId>org.springframework</groupId>
47+
<artifactId>spring-context</artifactId>
48+
</dependency>
49+
<dependency>
50+
<groupId>org.springframework.boot</groupId>
51+
<artifactId>spring-boot-starter-thymeleaf</artifactId>
52+
</dependency>
3753
<dependency>
3854
<groupId>org.junit.jupiter</groupId>
3955
<artifactId>junit-jupiter-engine</artifactId>
@@ -44,6 +60,20 @@
4460
<artifactId>mockito-core</artifactId>
4561
<scope>test</scope>
4662
</dependency>
63+
<dependency>
64+
<groupId>org.springframework.boot</groupId>
65+
<artifactId>spring-boot-starter-test</artifactId>
66+
<scope>test</scope>
67+
</dependency>
68+
<dependency>
69+
<groupId>org.springframework</groupId>
70+
<artifactId>spring-test</artifactId>
71+
<scope>test</scope>
72+
</dependency>
73+
<dependency>
74+
<groupId>org.springframework.boot</groupId>
75+
<artifactId>spring-boot-starter-aop</artifactId>
76+
</dependency>
4777
</dependencies>
4878
<build>
4979
<plugins>

Diff for: rate-limiting/src/main/java/com/iluwatar/App.java

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/*
2+
* This project is licensed under the MIT license. Module model-view-viewmodel is using ZK framework licensed under LGPL (see lgpl-3.0.txt).
3+
*
4+
* The MIT License
5+
* Copyright © 2014-2022 Ilkka Seppälä
6+
*
7+
* Permission is hereby granted, free of charge, to any person obtaining a copy
8+
* of this software and associated documentation files (the "Software"), to deal
9+
* in the Software without restriction, including without limitation the rights
10+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11+
* copies of the Software, and to permit persons to whom the Software is
12+
* furnished to do so, subject to the following conditions:
13+
*
14+
* The above copyright notice and this permission notice shall be included in
15+
* all copies or substantial portions of the Software.
16+
*
17+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23+
* THE SOFTWARE.
24+
*/
25+
package com.iluwatar;
26+
27+
import lombok.extern.slf4j.Slf4j;
28+
import org.springframework.boot.SpringApplication;
29+
30+
/**
31+
* App class.
32+
*/
33+
@Slf4j
34+
public class App {
35+
36+
/**
37+
* Program entry point.
38+
*/
39+
public static void main(String[] args) {
40+
SpringApplication app = new SpringApplication(App.class);
41+
System.out.println("Hello world!");
42+
}
43+
}

Diff for: rate-limiting/src/main/java/com/iluwatar/Main.java

-7
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package com.iluwatar.aspect;
2+
3+
import java.util.concurrent.TimeUnit;
4+
5+
/**
6+
* AOP annotation.
7+
*/
8+
public @interface RateLimited {
9+
/**
10+
* invoke method vialotation for one ip address.
11+
*/
12+
int count() default 10;
13+
/**
14+
* time unit for reset vialotation.
15+
*/
16+
TimeUnit timeUnit() default TimeUnit.SECONDS;
17+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
package com.iluwatar.aspect;
2+
3+
import lombok.extern.slf4j.Slf4j;
4+
import org.aspectj.lang.JoinPoint;
5+
import org.aspectj.lang.annotation.AfterReturning;
6+
import org.aspectj.lang.annotation.AfterThrowing;
7+
import org.aspectj.lang.annotation.Aspect;
8+
import org.aspectj.lang.annotation.Before;
9+
import org.aspectj.lang.annotation.Pointcut;
10+
import org.springframework.stereotype.Component;
11+
12+
/**
13+
* Aspect for rate limiting functionality using Spring AOP.
14+
*/
15+
@Slf4j
16+
@Aspect
17+
@Component
18+
public class RateLimiterAspect {
19+
20+
/**
21+
* Pointcut to intercept methods annotated with @RateLimited.
22+
*/
23+
@Pointcut("@annotation(com.iluwatar.aspect.RateLimited)")
24+
public void callMethodWithAnnotation() { }
25+
26+
/**
27+
* Advice executed before methods annotated with @RateLimited are invoked.
28+
*/
29+
@Before(
30+
value = "callMethodWithAnnotation()", argNames = "jp")
31+
public void endpointBeforeInvoke(JoinPoint jp) {
32+
String methodName = jp.getSignature().getName();
33+
LOGGER.info("Before executing method: {}", methodName);
34+
}
35+
36+
/**
37+
* Advice executed after methods annotated with @RateLimited successfully return.
38+
*
39+
* @param jp JoinPoint that intercepted the method call.
40+
* @param returningValue Value returned by the intercepted method.
41+
* @return The same returningValue as passed.
42+
*/
43+
@AfterReturning(
44+
value = "callMethodWithAnnotation()",
45+
argNames = "jp,returningValue",
46+
returning = "returningValue")
47+
public Object endpointAfterReturning(JoinPoint jp, Object returningValue) {
48+
String methodName = jp.getSignature().getName();
49+
LOGGER.info("Before executing method: {}", methodName);
50+
return returningValue;
51+
}
52+
53+
/**
54+
* Advice executed after methods annotated with @RateLimited throw an exception.
55+
*
56+
* @param jp JoinPoint that intercepted the method call.
57+
* @param e Exception thrown by the intercepted method.
58+
*/
59+
@AfterThrowing(
60+
pointcut = "callMethodWithAnnotation()",
61+
argNames = "jp,e",
62+
throwing = "e")
63+
public void endpointAfterThrowing(JoinPoint jp, Exception e) {
64+
String methodName = jp.getSignature().getName();
65+
LOGGER.info("Before executing method: {}", methodName);
66+
}
67+
68+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package com.iluwatar.controller;
2+
3+
import com.iluwatar.aspect.RateLimited;
4+
import com.iluwatar.model.DtoClass;
5+
import java.util.concurrent.TimeUnit;
6+
import org.springframework.web.bind.annotation.GetMapping;
7+
import org.springframework.web.bind.annotation.RestController;
8+
9+
/**
10+
* Rest API controller for example rate limited.
11+
*/
12+
@RestController
13+
public class RateLimitedController {
14+
15+
/**
16+
* Simple GET method.
17+
*
18+
* @return Simple DTO object.
19+
*/
20+
@RateLimited(count = 15, timeUnit = TimeUnit.MINUTES)
21+
@GetMapping("test-request")
22+
public DtoClass getRequest() {
23+
return new DtoClass();
24+
}
25+
26+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package com.iluwatar.model;
2+
3+
import lombok.Data;
4+
5+
/**
6+
* Example dto for controller.
7+
*/
8+
@Data
9+
public class DtoClass {
10+
11+
private Long id = 1L;
12+
13+
private String name = "dto for example";
14+
15+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package com.iluwatar.pattern;
2+
3+
import org.springframework.stereotype.Component;
4+
5+
/**
6+
* Realization Rate Limiting.
7+
*/
8+
@Component
9+
public class RateLimiter {
10+
11+
}

0 commit comments

Comments
 (0)