Skip to content

Commit

Permalink
feature: Rate Limiting pattern (iluwatar#2973)
Browse files Browse the repository at this point in the history
Заготовка архитектуры
  • Loading branch information
Каспшицкий Алексей committed Jul 4, 2024
1 parent 6b67c0e commit 00b4aac
Show file tree
Hide file tree
Showing 8 changed files with 210 additions and 7 deletions.
30 changes: 30 additions & 0 deletions rate-limiting/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,22 @@
</parent>
<artifactId>rate-limiting</artifactId>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
Expand All @@ -44,6 +60,20 @@
<artifactId>mockito-core</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
Expand Down
43 changes: 43 additions & 0 deletions rate-limiting/src/main/java/com/iluwatar/App.java
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!");
}
}
7 changes: 0 additions & 7 deletions rate-limiting/src/main/java/com/iluwatar/Main.java

This file was deleted.

17 changes: 17 additions & 0 deletions rate-limiting/src/main/java/com/iluwatar/aspect/RateLimited.java
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;
}
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);
}

}
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 rate-limiting/src/main/java/com/iluwatar/model/DtoClass.java
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 rate-limiting/src/main/java/com/iluwatar/pattern/RateLimiter.java
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 {

}

0 comments on commit 00b4aac

Please sign in to comment.