Skip to content

Conversation

@RurikV
Copy link

@RurikV RurikV commented May 3, 2025

Add asynchronous fetching of Pokémon types and details with error handling

Fetches Pokemon data asynchronously

Implements asynchronous fetching of Pokémon types and details to improve performance.

Adds error handling to gracefully manage potential API failures during data retrieval.

…ter confirming it's working properly, proceed to a multi-threaded approach.

Add access log test data and implement unit tests for solution metrics

- Created a new access log test file with extensive log entries for testing.
- Implemented a Clojure test suite to validate the functionality of the solution.
- Tests include calculations for total bytes, filtering by URL and referrer, and handling non-existent entries.
…dling

Fetches Pokemon data asynchronously

Implements asynchronous fetching of Pokémon types and details to improve performance.

Adds error handling to gracefully manage potential API failures during data retrieval.
(def ^:const log-dir "./logs")

;; Create a fixed thread pool for parallel processing
(def thread-pool (Executors/newFixedThreadPool
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Если ниже тут же используешь (.addShutdownHook задефайненный объект внутри замыкания (.shutdown thread-pool) и т.п. , прямо по месту, то лучше использовать defonce. Иначе при перезугрузке неймспейса в репл или при подключении в качестве зависимости в несколько других неймспейсов, весь код неймспейса может вычислиться дважды, что приведет к передефайну thread-pool, а старый может остаться висеть в системе, потому что на него висит его шатдаун хук.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Спасибо большое Андрей!
Поправил.
884bebc

(+ 14 (.. Runtime getRuntime availableProcessors))))

;; Add a shutdown hook to ensure the thread pool is properly shut down
(.addShutdownHook (Runtime/getRuntime)
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Если операция идемпотентная (следующие вызовы после первого ничего не изменяют), то ок. Иначе - при перезагрузке модуля может повеситься 2 хука, 2 листенера и т.п. Поэтому лучше такой прямой мутирующий код в теле неймспейса не писать, а выносить в функции типа init, которые вызывать единожды в точке входа (мэйн или его аналоги)

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Спасибо большое!
Исправил.
dd8fd61

This prevents re-initialization of the thread pool if the code is reloaded, ensuring resource consistency and avoiding potential issues with duplicate thread pools. It is particularly useful in development environments with hot-reloading.
Moved shutdown logic into a dedicated init-thread-pool function for clarity and reusability. This improves lifecycle control and avoids side effects at the namespace level.
Note: This function should be called explicitly once at application startup (e.g. -main) to avoid multiple shutdown hooks in case of module reload.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants