Skip to content

Instantly share code, notes, and snippets.

@ackintosh
Created July 7, 2024 22:53
Show Gist options
  • Select an option

  • Save ackintosh/a5c9beec771f749422f390c9ad87499e to your computer and use it in GitHub Desktop.

Select an option

Save ackintosh/a5c9beec771f749422f390c9ad87499e to your computer and use it in GitHub Desktop.

Revisions

  1. ackintosh created this gist Jul 7, 2024.
    33 changes: 33 additions & 0 deletions monitor_mutex.rs
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,33 @@
    struct MonitorMutex {
    inner: Mutex<RateLimiter>,
    }

    impl MonitorMutex {
    fn new(limiter: RateLimiter) -> Self {
    MonitorMutex {
    inner: Mutex::new(limiter),
    }
    }

    fn lock(&self) -> MutexGuard<RateLimiter> {
    let start = Instant::now();
    let guard = self.inner.lock();
    let duration = start.elapsed().as_nanos() as u64;
    println!("MonitorMutex:{}", duration);
    guard
    }
    }

    impl Deref for MonitorMutex {
    type Target = Mutex<RateLimiter>;

    fn deref(&self) -> &Self::Target {
    &self.inner
    }
    }

    impl DerefMut for MonitorMutex {
    fn deref_mut(&mut self) -> &mut Self::Target {
    &mut self.inner
    }
    }