From d22c2eb30d1c97f1ce95e2ea6550fb97eff7807a Mon Sep 17 00:00:00 2001 From: vissutagunawan <vglim3653@gmail.com> Date: Thu, 27 Mar 2025 10:04:51 +0700 Subject: [PATCH] Added Reflection 1 on README.md --- README.md | 24 +++++++++++++++++------- 1 file changed, 17 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index 7e0fc90..6cbd861 100644 --- a/README.md +++ b/README.md @@ -59,14 +59,14 @@ You can install Postman via this website: https://www.postman.com/downloads/ - Open another new terminal, edit `ROCKET_PORT` in `.env` to `8003`, then execute `cargo run`. ## Mandatory Checklists (Subscriber) -- [ ] Clone https://gitlab.com/ichlaffterlalu/bambangshop-receiver to a new repository. +- [x] Clone https://gitlab.com/ichlaffterlalu/bambangshop-receiver to a new repository. - **STAGE 1: Implement models and repositories** - - [ ] Commit: `Create Notification model struct.` - - [ ] Commit: `Create SubscriberRequest model struct.` - - [ ] Commit: `Create Notification database and Notification repository struct skeleton.` - - [ ] Commit: `Implement add function in Notification repository.` - - [ ] Commit: `Implement list_all_as_string function in Notification repository.` - - [ ] Write answers of your learning module's "Reflection Subscriber-1" questions in this README. + - [x] Commit: `Create Notification model struct.` + - [x] Commit: `Create SubscriberRequest model struct.` + - [x] Commit: `Create Notification database and Notification repository struct skeleton.` + - [x] Commit: `Implement add function in Notification repository.` + - [x] Commit: `Implement list_all_as_string function in Notification repository.` + - [x] Write answers of your learning module's "Reflection Subscriber-1" questions in this README. - **STAGE 3: Implement services and controllers** - [ ] Commit: `Create Notification service struct skeleton.` - [ ] Commit: `Implement subscribe function in Notification service.` @@ -86,4 +86,14 @@ This is the place for you to write reflections: #### Reflection Subscriber-1 +> 1. In this tutorial, we used RwLock<> to synchronise the use of Vec of Notifications. Explain why it is necessary for this case, and explain why we do not use Mutex<> instead? + +In this tutorial, using RwLock<> to synchronize access to the Vec of Notifications is necessary because of our specific access pattern. Our application performs many read operations on the Notifications Vec, with relatively few write operations. RwLock<> is superior to Mutex<> in this scenario because it allows multiple reader threads to access the data simultaneously, while only restricting access during write operations. +This creates a significant efficiency advantage over Mutex<>, which would block all threads during any access operation, whether reading or writing. Since our application primarily reads from the Notifications collection, RwLock<> provides the necessary thread safety while maximizing concurrent access and throughput. + +> 2. In this tutorial, we used lazy_static external library to define Vec and DashMap as a “static” variable. Compared to Java where we can mutate the content of a static variable via a static function, why did not Rust allow us to do so? + +Rust doesn't allow direct mutation of static variables (unlike Java where we can modify statics through static functions) because of Rust's strict thread safety guarantees. In Rust's memory safety model, allowing unrestricted mutation of static variables would create potential thread safety issues, as multiple threads could simultaneously access and modify the same static variable without synchronization. +This is why we need the lazy_static library to define Vec and DashMap as static variables - it provides the necessary thread-safe wrapper around these static collections. Unlike Java, which leaves thread safety as the developer's responsibility, Rust's design philosophy prioritizes preventing data races and other concurrency issues at the language level, requiring explicit synchronization mechanisms for shared mutable state. + #### Reflection Subscriber-2 -- GitLab