From a723bd825d63745d1debc070f0c6a252b05c535a Mon Sep 17 00:00:00 2001 From: Ferdinand57 <ferdinandbs77@gmail.com> Date: Fri, 28 Mar 2025 20:56:56 +0700 Subject: [PATCH] Reflection Subscriber 1 --- README.md | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index ed30bf7..73bd7fd 100644 --- a/README.md +++ b/README.md @@ -61,12 +61,12 @@ You can install Postman via this website: https://www.postman.com/downloads/ ## Mandatory Checklists (Subscriber) - [v] 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. + - [v] Commit: `Create Notification model struct.` + - [v] Commit: `Create SubscriberRequest model struct.` + - [v] Commit: `Create Notification database and Notification repository struct skeleton.` + - [v] Commit: `Implement add function in Notification repository.` + - [v] Commit: `Implement list_all_as_string function in Notification repository.` + - [v] 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,12 @@ 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? + +Imagine we have a whiteboard (representing the notification list), Mutex<> allow only one person to interact with the board at a time, regardless if they want to write or just want to read the whiteboard, meanwhile RwLock<> allow as many people to read the whiteboard at the same time, meanwhile if a person want to write, they have to wait for everyone to stop reading so the one writting can get exclusive access. Since we look at the notification list more often than we write to it, RwLock is usually faster because it lets all the readers peek simultaneously without blocking each other. We only pay the "wait" cost when someone actually needs to write. Mutex would make even readers wait for each other, slowing things down unnecessarily for reads. + +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 is like a very strict parents who really doesn't want us to mess things up, especially when we share toys/data with other people/threads, because changing shared static stuff can cause a data race a nasty bug where multiple threads try to read and write to the same spot at the exact same time without coordination, leading to totally unpredictable results and crashes, java on the other hand has more freedom, it lets us do it because it trust us to be careful, that's why rust makes us use tools like lazy static to set up complex shared data properly, to make sure it's done safely and doesn't cause data races, it's more work upfront but way safer. + #### Reflection Subscriber-2 -- GitLab