mirror of
https://github.com/tiennm99/java-design-patterns.git
synced 2026-09-04 06:19:47 +00:00
fix: spotless formatting fixes
This commit is contained in:
@@ -67,6 +67,26 @@
|
|||||||
<groupId>org.apache.maven.plugins</groupId>
|
<groupId>org.apache.maven.plugins</groupId>
|
||||||
<artifactId>maven-compiler-plugin</artifactId>
|
<artifactId>maven-compiler-plugin</artifactId>
|
||||||
</plugin>
|
</plugin>
|
||||||
|
<plugin>
|
||||||
|
<groupId>com.diffplug.spotless</groupId>
|
||||||
|
<artifactId>spotless-maven-plugin</artifactId>
|
||||||
|
<version>2.44.4</version>
|
||||||
|
<executions>
|
||||||
|
<execution>
|
||||||
|
<goals>
|
||||||
|
<goal>check</goal>
|
||||||
|
<goal>apply</goal>
|
||||||
|
</goals>
|
||||||
|
</execution>
|
||||||
|
</executions>
|
||||||
|
<configuration>
|
||||||
|
<java>
|
||||||
|
<googleJavaFormat>
|
||||||
|
<version>1.17.0</version>
|
||||||
|
</googleJavaFormat>
|
||||||
|
</java>
|
||||||
|
</configuration>
|
||||||
|
</plugin>
|
||||||
</plugins>
|
</plugins>
|
||||||
</build>
|
</build>
|
||||||
|
|
||||||
|
|||||||
+3
-4
@@ -10,8 +10,7 @@ import org.springframework.cloud.openfeign.EnableFeignClients;
|
|||||||
@EnableFeignClients
|
@EnableFeignClients
|
||||||
public class ContextserviceApplication {
|
public class ContextserviceApplication {
|
||||||
|
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
SpringApplication.run(ContextserviceApplication.class, args);
|
SpringApplication.run(ContextserviceApplication.class, args);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
+3
-2
@@ -7,7 +7,6 @@ import org.springframework.boot.actuate.health.HealthIndicator;
|
|||||||
import org.springframework.scheduling.annotation.Scheduled;
|
import org.springframework.scheduling.annotation.Scheduled;
|
||||||
import org.springframework.stereotype.Component;
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
|
|
||||||
@Component("myCustomHealthCheck")
|
@Component("myCustomHealthCheck")
|
||||||
public class MyCustomHealthCheck implements HealthIndicator {
|
public class MyCustomHealthCheck implements HealthIndicator {
|
||||||
|
|
||||||
@@ -33,7 +32,9 @@ public class MyCustomHealthCheck implements HealthIndicator {
|
|||||||
public Health health() {
|
public Health health() {
|
||||||
if (isHealthy) {
|
if (isHealthy) {
|
||||||
log.info("Health check successful, service is UP");
|
log.info("Health check successful, service is UP");
|
||||||
return Health.up().withDetail("message", "Service is running and scheduled checks are OK").build();
|
return Health.up()
|
||||||
|
.withDetail("message", "Service is running and scheduled checks are OK")
|
||||||
|
.build();
|
||||||
} else {
|
} else {
|
||||||
log.warn("Health check failed, service is DOWN");
|
log.warn("Health check failed, service is DOWN");
|
||||||
return Health.down().withDetail("error", "Scheduled health checks failed").build();
|
return Health.down().withDetail("error", "Scheduled health checks failed").build();
|
||||||
|
|||||||
+3
-2
@@ -13,7 +13,8 @@ public class ContextController {
|
|||||||
private final String userRegion;
|
private final String userRegion;
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
public ContextController(GreetingServiceClient greetingServiceClient, @Value("${user.region}") String userRegion) {
|
public ContextController(
|
||||||
|
GreetingServiceClient greetingServiceClient, @Value("${user.region}") String userRegion) {
|
||||||
this.greetingServiceClient = greetingServiceClient;
|
this.greetingServiceClient = greetingServiceClient;
|
||||||
this.userRegion = userRegion;
|
this.userRegion = userRegion;
|
||||||
}
|
}
|
||||||
@@ -21,6 +22,6 @@ public class ContextController {
|
|||||||
@GetMapping("/context")
|
@GetMapping("/context")
|
||||||
public String getContext() {
|
public String getContext() {
|
||||||
String greeting = greetingServiceClient.getGreeting();
|
String greeting = greetingServiceClient.getGreeting();
|
||||||
return "The Greeting Service says: "+greeting+" from "+userRegion;
|
return "The Greeting Service says: " + greeting + " from " + userRegion;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+12
-11
@@ -7,7 +7,6 @@ import org.mockito.Mockito;
|
|||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.beans.factory.annotation.Value;
|
import org.springframework.beans.factory.annotation.Value;
|
||||||
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
|
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
|
||||||
|
|
||||||
import org.springframework.boot.test.context.SpringBootTest;
|
import org.springframework.boot.test.context.SpringBootTest;
|
||||||
import org.springframework.context.annotation.Import;
|
import org.springframework.context.annotation.Import;
|
||||||
import org.springframework.http.MediaType;
|
import org.springframework.http.MediaType;
|
||||||
@@ -21,29 +20,31 @@ import org.springframework.test.web.servlet.result.MockMvcResultMatchers;
|
|||||||
@Import(TestConfig.class)
|
@Import(TestConfig.class)
|
||||||
class ContextControllerTest {
|
class ContextControllerTest {
|
||||||
|
|
||||||
@Autowired
|
@Autowired private MockMvc mockMvc;
|
||||||
private MockMvc mockMvc;
|
|
||||||
|
|
||||||
@MockitoBean
|
@MockitoBean private GreetingServiceClient greetingServiceClient;
|
||||||
private GreetingServiceClient greetingServiceClient;
|
|
||||||
|
|
||||||
@Value("${user.region}")
|
@Value("${user.region}")
|
||||||
private String userRegion;
|
private String userRegion;
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void shouldReturnContextGreeting() throws Exception{
|
void shouldReturnContextGreeting() throws Exception {
|
||||||
Mockito.when(greetingServiceClient.getGreeting()).thenReturn("Mocked Hello");
|
Mockito.when(greetingServiceClient.getGreeting()).thenReturn("Mocked Hello");
|
||||||
|
|
||||||
mockMvc.perform(MockMvcRequestBuilders.get("/context")
|
mockMvc
|
||||||
.accept(MediaType.TEXT_PLAIN))
|
.perform(MockMvcRequestBuilders.get("/context").accept(MediaType.TEXT_PLAIN))
|
||||||
.andExpect(MockMvcResultMatchers.status().isOk())
|
.andExpect(MockMvcResultMatchers.status().isOk())
|
||||||
.andExpect(MockMvcResultMatchers.content().string("The Greeting Service says: Mocked Hello from Chennai, Tamil Nadu, India"));
|
.andExpect(
|
||||||
|
MockMvcResultMatchers.content()
|
||||||
|
.string("The Greeting Service says: Mocked Hello from Chennai, Tamil Nadu, India"));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void shouldReturnContextServiceHealthStatusUp() throws Exception {
|
void shouldReturnContextServiceHealthStatusUp() throws Exception {
|
||||||
mockMvc.perform(MockMvcRequestBuilders.get("/actuator/health"))
|
mockMvc
|
||||||
|
.perform(MockMvcRequestBuilders.get("/actuator/health"))
|
||||||
.andExpect(MockMvcResultMatchers.status().isOk())
|
.andExpect(MockMvcResultMatchers.status().isOk())
|
||||||
.andExpect(MockMvcResultMatchers.content().string(Matchers.containsString("\"status\":\"UP\"")));
|
.andExpect(
|
||||||
|
MockMvcResultMatchers.content().string(Matchers.containsString("\"status\":\"UP\"")));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+7
-6
@@ -6,12 +6,13 @@ import org.springframework.boot.test.context.SpringBootTest;
|
|||||||
@SpringBootTest
|
@SpringBootTest
|
||||||
class ContextserviceApplicationTests {
|
class ContextserviceApplicationTests {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void contextLoads() {
|
void contextLoads() {
|
||||||
// This is a basic integration test that checks if the Spring Application Context loads successfully.
|
// This is a basic integration test that checks if the Spring Application Context loads
|
||||||
|
// successfully.
|
||||||
// If the context loads without any exceptions, the test is considered passing.
|
// If the context loads without any exceptions, the test is considered passing.
|
||||||
// It is often left empty as the act of loading the context is the primary verification.
|
// It is often left empty as the act of loading the context is the primary verification.
|
||||||
// You can add specific assertions here if you want to verify the presence or state of certain beans.
|
// You can add specific assertions here if you want to verify the presence or state of certain
|
||||||
}
|
// beans.
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+6
-5
@@ -1,10 +1,11 @@
|
|||||||
package com.learning.contextservice;
|
package com.learning.contextservice;
|
||||||
|
|
||||||
|
import static org.junit.jupiter.api.Assertions.*;
|
||||||
|
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
import org.springframework.boot.actuate.health.Health;
|
import org.springframework.boot.actuate.health.Health;
|
||||||
import org.springframework.test.util.ReflectionTestUtils;
|
|
||||||
import org.springframework.boot.actuate.health.Status;
|
import org.springframework.boot.actuate.health.Status;
|
||||||
import static org.junit.jupiter.api.Assertions.*;
|
import org.springframework.test.util.ReflectionTestUtils;
|
||||||
|
|
||||||
class MyCustomHealthCheckTest {
|
class MyCustomHealthCheckTest {
|
||||||
|
|
||||||
@@ -16,7 +17,8 @@ class MyCustomHealthCheckTest {
|
|||||||
Health health = healthCheck.health();
|
Health health = healthCheck.health();
|
||||||
assertEquals(Status.UP, health.getStatus());
|
assertEquals(Status.UP, health.getStatus());
|
||||||
assertTrue(health.getDetails().containsKey("message"));
|
assertTrue(health.getDetails().containsKey("message"));
|
||||||
assertEquals("Service is running and scheduled checks are OK", health.getDetails().get("message"));
|
assertEquals(
|
||||||
|
"Service is running and scheduled checks are OK", health.getDetails().get("message"));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@@ -29,5 +31,4 @@ class MyCustomHealthCheckTest {
|
|||||||
assertTrue(health.getDetails().containsKey("error"));
|
assertTrue(health.getDetails().containsKey("error"));
|
||||||
assertEquals("Scheduled health checks failed", health.getDetails().get("error"));
|
assertEquals("Scheduled health checks failed", health.getDetails().get("error"));
|
||||||
}
|
}
|
||||||
|
}
|
||||||
}
|
|
||||||
|
|||||||
@@ -49,6 +49,26 @@
|
|||||||
<groupId>org.apache.maven.plugins</groupId>
|
<groupId>org.apache.maven.plugins</groupId>
|
||||||
<artifactId>maven-compiler-plugin</artifactId>
|
<artifactId>maven-compiler-plugin</artifactId>
|
||||||
</plugin>
|
</plugin>
|
||||||
|
<plugin>
|
||||||
|
<groupId>com.diffplug.spotless</groupId>
|
||||||
|
<artifactId>spotless-maven-plugin</artifactId>
|
||||||
|
<version>2.44.4</version>
|
||||||
|
<executions>
|
||||||
|
<execution>
|
||||||
|
<goals>
|
||||||
|
<goal>check</goal>
|
||||||
|
<goal>apply</goal>
|
||||||
|
</goals>
|
||||||
|
</execution>
|
||||||
|
</executions>
|
||||||
|
<configuration>
|
||||||
|
<java>
|
||||||
|
<googleJavaFormat>
|
||||||
|
<version>1.17.0</version>
|
||||||
|
</googleJavaFormat>
|
||||||
|
</java>
|
||||||
|
</configuration>
|
||||||
|
</plugin>
|
||||||
</plugins>
|
</plugins>
|
||||||
</build>
|
</build>
|
||||||
|
|
||||||
|
|||||||
+3
-4
@@ -8,8 +8,7 @@ import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
|
|||||||
@EnableEurekaServer
|
@EnableEurekaServer
|
||||||
public class EurekaserverApplication {
|
public class EurekaserverApplication {
|
||||||
|
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
SpringApplication.run(EurekaserverApplication.class, args);
|
SpringApplication.run(EurekaserverApplication.class, args);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
+7
-6
@@ -6,12 +6,13 @@ import org.springframework.boot.test.context.SpringBootTest;
|
|||||||
@SpringBootTest
|
@SpringBootTest
|
||||||
class EurekaserverApplicationTests {
|
class EurekaserverApplicationTests {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void contextLoads() {
|
void contextLoads() {
|
||||||
// This is a basic integration test that checks if the Spring Application Context loads successfully.
|
// This is a basic integration test that checks if the Spring Application Context loads
|
||||||
|
// successfully.
|
||||||
// If the context loads without any exceptions, the test is considered passing.
|
// If the context loads without any exceptions, the test is considered passing.
|
||||||
// It is often left empty as the act of loading the context is the primary verification.
|
// It is often left empty as the act of loading the context is the primary verification.
|
||||||
// You can add specific assertions here if you want to verify the presence or state of certain beans.
|
// You can add specific assertions here if you want to verify the presence or state of certain
|
||||||
}
|
// beans.
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -63,6 +63,26 @@
|
|||||||
<groupId>org.apache.maven.plugins</groupId>
|
<groupId>org.apache.maven.plugins</groupId>
|
||||||
<artifactId>maven-compiler-plugin</artifactId>
|
<artifactId>maven-compiler-plugin</artifactId>
|
||||||
</plugin>
|
</plugin>
|
||||||
|
<plugin>
|
||||||
|
<groupId>com.diffplug.spotless</groupId>
|
||||||
|
<artifactId>spotless-maven-plugin</artifactId>
|
||||||
|
<version>2.44.4</version>
|
||||||
|
<executions>
|
||||||
|
<execution>
|
||||||
|
<goals>
|
||||||
|
<goal>check</goal>
|
||||||
|
<goal>apply</goal>
|
||||||
|
</goals>
|
||||||
|
</execution>
|
||||||
|
</executions>
|
||||||
|
<configuration>
|
||||||
|
<java>
|
||||||
|
<googleJavaFormat>
|
||||||
|
<version>1.17.0</version>
|
||||||
|
</googleJavaFormat>
|
||||||
|
</java>
|
||||||
|
</configuration>
|
||||||
|
</plugin>
|
||||||
</plugins>
|
</plugins>
|
||||||
</build>
|
</build>
|
||||||
|
|
||||||
|
|||||||
+3
-4
@@ -10,8 +10,7 @@ import org.springframework.context.annotation.ComponentScan;
|
|||||||
@ComponentScan("com.learning.greetingservice.controller")
|
@ComponentScan("com.learning.greetingservice.controller")
|
||||||
public class GreetingserviceApplication {
|
public class GreetingserviceApplication {
|
||||||
|
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
SpringApplication.run(GreetingserviceApplication.class, args);
|
SpringApplication.run(GreetingserviceApplication.class, args);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
+3
-1
@@ -32,7 +32,9 @@ public class MyCustomHealthCheck implements HealthIndicator {
|
|||||||
public Health health() {
|
public Health health() {
|
||||||
if (isHealthy) {
|
if (isHealthy) {
|
||||||
log.info("Health check successful, service is UP");
|
log.info("Health check successful, service is UP");
|
||||||
return Health.up().withDetail("message", "Service is running and scheduled checks are OK").build();
|
return Health.up()
|
||||||
|
.withDetail("message", "Service is running and scheduled checks are OK")
|
||||||
|
.build();
|
||||||
} else {
|
} else {
|
||||||
log.warn("Health check failed, service is DOWN");
|
log.warn("Health check failed, service is DOWN");
|
||||||
return Health.down().withDetail("error", "Scheduled health checks failed").build();
|
return Health.down().withDetail("error", "Scheduled health checks failed").build();
|
||||||
|
|||||||
+7
-6
@@ -6,12 +6,13 @@ import org.springframework.boot.test.context.SpringBootTest;
|
|||||||
@SpringBootTest
|
@SpringBootTest
|
||||||
class GreetingserviceApplicationTests {
|
class GreetingserviceApplicationTests {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void contextLoads() {
|
void contextLoads() {
|
||||||
// This is a basic integration test that checks if the Spring Application Context loads successfully.
|
// This is a basic integration test that checks if the Spring Application Context loads
|
||||||
|
// successfully.
|
||||||
// If the context loads without any exceptions, the test is considered passing.
|
// If the context loads without any exceptions, the test is considered passing.
|
||||||
// It is often left empty as the act of loading the context is the primary verification.
|
// It is often left empty as the act of loading the context is the primary verification.
|
||||||
// You can add specific assertions here if you want to verify the presence or state of certain beans.
|
// You can add specific assertions here if you want to verify the presence or state of certain
|
||||||
}
|
// beans.
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+6
-5
@@ -1,10 +1,11 @@
|
|||||||
package com.learning.greetingservice;
|
package com.learning.greetingservice;
|
||||||
|
|
||||||
|
import static org.junit.jupiter.api.Assertions.*;
|
||||||
|
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
import org.springframework.boot.actuate.health.Health;
|
import org.springframework.boot.actuate.health.Health;
|
||||||
import org.springframework.test.util.ReflectionTestUtils;
|
|
||||||
import org.springframework.boot.actuate.health.Status;
|
import org.springframework.boot.actuate.health.Status;
|
||||||
import static org.junit.jupiter.api.Assertions.*;
|
import org.springframework.test.util.ReflectionTestUtils;
|
||||||
|
|
||||||
class MyCustomHealthCheckTest {
|
class MyCustomHealthCheckTest {
|
||||||
|
|
||||||
@@ -16,7 +17,7 @@ class MyCustomHealthCheckTest {
|
|||||||
Health health = healthCheck.health();
|
Health health = healthCheck.health();
|
||||||
assertEquals(Status.UP, health.getStatus());
|
assertEquals(Status.UP, health.getStatus());
|
||||||
assertTrue(health.getDetails().containsKey("message"));
|
assertTrue(health.getDetails().containsKey("message"));
|
||||||
assertEquals("Service is running and scheduled checks are OK", health.getDetails().get("message"));
|
assertEquals(
|
||||||
|
"Service is running and scheduled checks are OK", health.getDetails().get("message"));
|
||||||
}
|
}
|
||||||
|
}
|
||||||
}
|
|
||||||
|
|||||||
+10
-8
@@ -16,21 +16,23 @@ import org.springframework.test.web.servlet.result.MockMvcResultMatchers;
|
|||||||
@ActiveProfiles("test")
|
@ActiveProfiles("test")
|
||||||
class GreetingControllerTest {
|
class GreetingControllerTest {
|
||||||
|
|
||||||
@Autowired
|
@Autowired private MockMvc mockMvc;
|
||||||
private MockMvc mockMvc;
|
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void shouldReturnGreeting() throws Exception{
|
void shouldReturnGreeting() throws Exception {
|
||||||
mockMvc.perform(MockMvcRequestBuilders.get("/greeting")
|
mockMvc
|
||||||
.accept(MediaType.TEXT_PLAIN))
|
.perform(MockMvcRequestBuilders.get("/greeting").accept(MediaType.TEXT_PLAIN))
|
||||||
.andExpect(MockMvcResultMatchers.status().isOk())
|
.andExpect(MockMvcResultMatchers.status().isOk())
|
||||||
.andExpect(MockMvcResultMatchers.content().string("Hello"));
|
.andExpect(MockMvcResultMatchers.content().string("Hello"));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void shouldReturnHealthStatusUp() throws Exception{
|
void shouldReturnHealthStatusUp() throws Exception {
|
||||||
mockMvc.perform(MockMvcRequestBuilders.get("/actuator/health"))
|
mockMvc
|
||||||
|
.perform(MockMvcRequestBuilders.get("/actuator/health"))
|
||||||
.andExpect(MockMvcResultMatchers.status().isOk())
|
.andExpect(MockMvcResultMatchers.status().isOk())
|
||||||
.andExpect(MockMvcResultMatchers.content().string(org.hamcrest.Matchers.containsString("\"status\":\"UP\"")));
|
.andExpect(
|
||||||
|
MockMvcResultMatchers.content()
|
||||||
|
.string(org.hamcrest.Matchers.containsString("\"status\":\"UP\"")));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+8
-15
@@ -4,10 +4,8 @@ import java.util.concurrent.*;
|
|||||||
import java.util.concurrent.atomic.AtomicInteger;
|
import java.util.concurrent.atomic.AtomicInteger;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Token Bucket rate limiter implementation. Allows requests to proceed as long
|
* Token Bucket rate limiter implementation. Allows requests to proceed as long as there are tokens
|
||||||
* as there are tokens
|
* available in the bucket. Tokens are added at a fixed interval up to a defined capacity.
|
||||||
* available in the bucket. Tokens are added at a fixed interval up to a defined
|
|
||||||
* capacity.
|
|
||||||
*/
|
*/
|
||||||
public class TokenBucketRateLimiter implements RateLimiter {
|
public class TokenBucketRateLimiter implements RateLimiter {
|
||||||
private final int capacity;
|
private final int capacity;
|
||||||
@@ -22,10 +20,10 @@ public class TokenBucketRateLimiter implements RateLimiter {
|
|||||||
/**
|
/**
|
||||||
* Constructor with custom refill interval.
|
* Constructor with custom refill interval.
|
||||||
*
|
*
|
||||||
* @param capacity token bucket capacity
|
* @param capacity token bucket capacity
|
||||||
* @param refillRate token refill rate
|
* @param refillRate token refill rate
|
||||||
* @param refillInterval refill interval value
|
* @param refillInterval refill interval value
|
||||||
* @param timeUnit refill interval time unit
|
* @param timeUnit refill interval time unit
|
||||||
*/
|
*/
|
||||||
public TokenBucketRateLimiter(
|
public TokenBucketRateLimiter(
|
||||||
int capacity, int refillRate, long refillInterval, TimeUnit timeUnit) {
|
int capacity, int refillRate, long refillInterval, TimeUnit timeUnit) {
|
||||||
@@ -53,10 +51,7 @@ public class TokenBucketRateLimiter implements RateLimiter {
|
|||||||
buckets.forEach((k, b) -> b.refill(refillRate));
|
buckets.forEach((k, b) -> b.refill(refillRate));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/** Inner class that represents the bucket holding tokens for each service-operation. */
|
||||||
* Inner class that represents the bucket holding tokens for each
|
|
||||||
* service-operation.
|
|
||||||
*/
|
|
||||||
private static class TokenBucket {
|
private static class TokenBucket {
|
||||||
private final int capacity;
|
private final int capacity;
|
||||||
private final AtomicInteger tokens;
|
private final AtomicInteger tokens;
|
||||||
@@ -69,10 +64,8 @@ public class TokenBucketRateLimiter implements RateLimiter {
|
|||||||
boolean tryConsume() {
|
boolean tryConsume() {
|
||||||
while (true) {
|
while (true) {
|
||||||
int current = tokens.get();
|
int current = tokens.get();
|
||||||
if (current <= 0)
|
if (current <= 0) return false;
|
||||||
return false;
|
if (tokens.compareAndSet(current, current - 1)) return true;
|
||||||
if (tokens.compareAndSet(current, current - 1))
|
|
||||||
return true;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user