-
Notifications
You must be signed in to change notification settings - Fork 0
Make hw8 #9
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
yuuusha
wants to merge
1
commit into
hw7
Choose a base branch
from
hw8
base: hw7
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Make hw8 #9
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
18 changes: 18 additions & 0 deletions
18
bot/src/main/java/edu/java/bot/configuration/KafkaConfiguration.java
This file contains hidden or 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,18 @@ | ||
| package edu.java.bot.configuration; | ||
|
|
||
| import org.apache.kafka.clients.admin.NewTopic; | ||
| import org.springframework.context.annotation.Bean; | ||
| import org.springframework.context.annotation.Configuration; | ||
| import org.springframework.kafka.config.TopicBuilder; | ||
|
|
||
| @Configuration | ||
| public class KafkaConfiguration { | ||
|
|
||
| @Bean | ||
| public NewTopic topic(ApplicationConfig config) { | ||
| return TopicBuilder.name(config.kafkaConfigurationInfo().topicName() + "_dlq") | ||
| .partitions(1) | ||
| .replicas(1) | ||
| .build(); | ||
| } | ||
| } |
20 changes: 20 additions & 0 deletions
20
bot/src/main/java/edu/java/bot/service/KafkaUpdatesListener.java
This file contains hidden or 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,20 @@ | ||
| package edu.java.bot.service; | ||
|
|
||
| import edu.java.bot.dto.request.LinkUpdate; | ||
| import lombok.RequiredArgsConstructor; | ||
| import org.springframework.kafka.annotation.KafkaListener; | ||
| import org.springframework.kafka.annotation.RetryableTopic; | ||
| import org.springframework.kafka.retrytopic.DltStrategy; | ||
| import org.springframework.stereotype.Service; | ||
|
|
||
| @Service | ||
| @RequiredArgsConstructor | ||
| public class KafkaUpdatesListener { | ||
| private final LinkUpdatesSenderService senderService; | ||
|
|
||
| @KafkaListener(topics = "${app.kafka-configuration-info.topic-name}", groupId = "bot") | ||
| @RetryableTopic(attempts = "1", dltStrategy = DltStrategy.FAIL_ON_ERROR, dltTopicSuffix = "_dlq") | ||
| public void listenUpdates(LinkUpdate linkUpdate) { | ||
| senderService.sendLinkUpdate(linkUpdate); | ||
| } | ||
| } |
20 changes: 20 additions & 0 deletions
20
bot/src/main/java/edu/java/bot/service/RateLimiterService.java
This file contains hidden or 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,20 @@ | ||
| package edu.java.bot.service; | ||
|
|
||
| import java.util.List; | ||
| import org.springframework.beans.factory.annotation.Value; | ||
| import org.springframework.stereotype.Service; | ||
|
|
||
| @Service | ||
| public class RateLimiterService { | ||
|
|
||
| private final List<String> whitelist; | ||
|
|
||
| public RateLimiterService(@Value("${rate-limiter.whitelist}") List<String> whitelist) { | ||
| this.whitelist = whitelist; | ||
| } | ||
|
|
||
| public boolean isSkipped(String ip) { | ||
| return whitelist.contains(ip); | ||
| } | ||
|
|
||
| } |
This file contains hidden or 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,18 @@ | ||
| package edu.java.bot.util; | ||
|
|
||
| import java.net.MalformedURLException; | ||
| import java.net.URI; | ||
| import java.net.URL; | ||
|
|
||
| public final class URLCreator { | ||
| private URLCreator() { | ||
| } | ||
|
|
||
| public static URL createURL(String link) { | ||
| try { | ||
| return URI.create(link).toURL(); | ||
| } catch (MalformedURLException e) { | ||
| throw new RuntimeException(e); | ||
| } | ||
| } | ||
| } |
This file contains hidden or 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
24 changes: 24 additions & 0 deletions
24
bot/src/test/java/edu/java/bot/kafka/KafkaIntegrationEnvironment.java
This file contains hidden or 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,24 @@ | ||
| package edu.java.bot.kafka; | ||
|
|
||
| import org.springframework.test.context.DynamicPropertyRegistry; | ||
| import org.springframework.test.context.DynamicPropertySource; | ||
| import org.testcontainers.containers.KafkaContainer; | ||
| import org.testcontainers.junit.jupiter.Testcontainers; | ||
| import org.testcontainers.utility.DockerImageName; | ||
|
|
||
| @Testcontainers | ||
| public class KafkaIntegrationEnvironment { | ||
| public static KafkaContainer KAFKA; | ||
|
|
||
| static { | ||
| KAFKA = new KafkaContainer(DockerImageName.parse("confluentinc/cp-kafka:7.6.0")); | ||
| KAFKA.start(); | ||
| } | ||
|
|
||
| @DynamicPropertySource | ||
| static void kafkaProperties(DynamicPropertyRegistry registry) { | ||
| registry.add("spring.kafka.bootstrap-servers", KAFKA::getBootstrapServers); | ||
| registry.add("spring.kafka.consumer.bootstrap-servers", KAFKA::getBootstrapServers); | ||
| registry.add("spring.kafka.producer.bootstrap-servers", KAFKA::getBootstrapServers); | ||
| } | ||
| } |
81 changes: 81 additions & 0 deletions
81
bot/src/test/java/edu/java/bot/kafka/KafkaUpdatesListenerTest.java
This file contains hidden or 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,81 @@ | ||
| package edu.java.bot.kafka; | ||
|
|
||
|
|
||
| import edu.java.bot.configuration.ApplicationConfig; | ||
| import edu.java.bot.dto.request.LinkUpdate; | ||
| import edu.java.bot.service.LinkUpdatesSenderService; | ||
| import edu.java.bot.util.URLCreator; | ||
| import org.apache.kafka.clients.consumer.KafkaConsumer; | ||
| import org.assertj.core.api.Assertions; | ||
| import org.junit.jupiter.api.Test; | ||
| import org.mockito.Mockito; | ||
| import org.springframework.beans.factory.annotation.Autowired; | ||
| import org.springframework.boot.autoconfigure.kafka.KafkaProperties; | ||
| import org.springframework.boot.test.context.SpringBootTest; | ||
| import org.springframework.boot.test.mock.mockito.MockBean; | ||
| import org.springframework.kafka.core.KafkaTemplate; | ||
|
|
||
| import java.time.Duration; | ||
| import java.util.List; | ||
| import java.util.Map; | ||
|
|
||
| import static org.awaitility.Awaitility.await; | ||
|
|
||
| @SpringBootTest | ||
| public class KafkaUpdatesListenerTest extends KafkaIntegrationEnvironment { | ||
|
|
||
| @MockBean | ||
| private LinkUpdatesSenderService linkUpdatesSenderService; | ||
|
|
||
| @Autowired | ||
| private KafkaTemplate<String, LinkUpdate> kafkaTemplate; | ||
|
|
||
| @Autowired | ||
| private ApplicationConfig config; | ||
|
|
||
| @Autowired | ||
| private KafkaProperties kafkaProperties; | ||
|
|
||
| @Test | ||
| public void listenUpdateCorrectTest() { | ||
| LinkUpdate linkUpdate = new LinkUpdate( | ||
| 1L, | ||
| URLCreator.createURL("https://github.com"), | ||
| "github", | ||
| List.of(1L), | ||
| Map.of() | ||
| ); | ||
| kafkaTemplate.send("updates", linkUpdate); | ||
| await() | ||
| .pollInterval(Duration.ofMillis(100)) | ||
| .atMost(Duration.ofSeconds(5)) | ||
| .untilAsserted(() -> Mockito.verify(linkUpdatesSenderService, Mockito.times(1)) | ||
| .sendLinkUpdate(linkUpdate)); | ||
| } | ||
|
|
||
| @Test | ||
| public void listenUpdateInCorrectTest() { | ||
| LinkUpdate linkUpdate = new LinkUpdate( | ||
| 1L, | ||
| URLCreator.createURL("https://github.com"), | ||
| "github", | ||
| List.of(1L), | ||
| Map.of() | ||
| ); | ||
| Mockito.doThrow(RuntimeException.class).when(linkUpdatesSenderService).sendLinkUpdate(linkUpdate); | ||
| KafkaConsumer<String, LinkUpdate> dlqKafkaConsumer = new KafkaConsumer<>( | ||
| kafkaProperties.buildConsumerProperties(null) | ||
| ); | ||
| dlqKafkaConsumer.subscribe(List.of(config.kafkaConfigurationInfo().topicName() + "_dlq")); | ||
| kafkaTemplate.send(config.kafkaConfigurationInfo().topicName(), linkUpdate); | ||
| await() | ||
| .pollInterval(Duration.ofMillis(100)) | ||
| .atMost(Duration.ofSeconds(10)) | ||
| .untilAsserted(() -> { | ||
| var values = dlqKafkaConsumer.poll(Duration.ofMillis(100)); | ||
| Assertions.assertThat(values).hasSize(1); | ||
| Assertions.assertThat(values.iterator().next().value()).isEqualTo(linkUpdate); | ||
| Mockito.verify(linkUpdatesSenderService).sendLinkUpdate(linkUpdate); | ||
| }); | ||
| } | ||
| } |
This file contains hidden or 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 was deleted.
Oops, something went wrong.
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. доработай, пожалуйста, по дз7, потом сделай git-rebase
Owner
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Сделаю rebase после ответа по комментарию в дз 7 |
This file contains hidden or 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 hidden or 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 |
|---|---|---|
| @@ -1,10 +1,22 @@ | ||
| package edu.java; | ||
|
|
||
| import java.time.Duration; | ||
| import java.util.List; | ||
| import org.jetbrains.annotations.NotNull; | ||
| import org.springframework.boot.context.properties.ConfigurationProperties; | ||
| import org.springframework.validation.annotation.Validated; | ||
|
|
||
| @Validated | ||
| @ConfigurationProperties(prefix = "retry-query", ignoreUnknownFields = false) | ||
| public record RetryQueryConfiguration(List<RetryElement> retries) { | ||
| public record RetryElement( | ||
| @NotNull String target, | ||
| @NotNull String type, | ||
| int maxAttempts, | ||
| double factor, | ||
| Duration minDelay, | ||
| Duration maxDelay, | ||
| List<Integer> codes | ||
| ) { | ||
| } | ||
| } |
6 changes: 3 additions & 3 deletions
6
retry/src/main/java/edu/java/builders/ExponentialRetryBuilder.java
This file contains hidden or 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 hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
сомнительная польза
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Что вы имеете ввиду? Довольно удобно было использовать, поэтому я оставил
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Имею в виду, что здесь отдельный класс с единственным методом, представляющим собой 1 строку.)
Ради выноса обработки исключения?
Можешь оставить.