Skip to content

Commit

Permalink
Remove usage of the palaver crate in an example (#597)
Browse files Browse the repository at this point in the history
* Remove usage of the `palaver` crate in an example

* fmt
  • Loading branch information
dvdplm authored Dec 9, 2021
1 parent d3b4706 commit b41acab
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 3 deletions.
1 change: 0 additions & 1 deletion examples/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ jsonrpsee = { path = "../jsonrpsee", features = ["full"] }
tracing = "0.1"
tracing-subscriber = "0.2"
tokio = { version = "1.8", features = ["full"] }
palaver = "0.2"

[[example]]
name = "http"
Expand Down
20 changes: 18 additions & 2 deletions examples/multi_middleware.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ use jsonrpsee::{
ws_server::{RpcModule, WsServerBuilder},
};
use std::net::SocketAddr;
use std::process::Command;
use std::time::Instant;

/// Example middleware to measure call execution time.
Expand Down Expand Up @@ -63,17 +64,32 @@ impl middleware::Middleware for Timings {
#[derive(Clone)]
struct ThreadWatcher;

impl ThreadWatcher {
// Count the number of threads visible to this process. Counts the lines of `ps -eL` and equivalent minus one (the header).
// Cribbed from the `palaver` crate.
fn count_threads() -> usize {
let out = if cfg!(any(target_os = "linux", target_os = "android")) {
Command::new("ps").arg("-eL").output().expect("failed to execute process")
} else if cfg!(any(target_os = "macos", target_os = "ios")) {
Command::new("ps").arg("-eM").output().expect("failed to execute process")
} else {
unimplemented!()
};
out.stdout.split(|&x| x == b'\n').skip(1).filter(|x| !x.is_empty()).count()
}
}

impl middleware::Middleware for ThreadWatcher {
type Instant = isize;

fn on_request(&self) -> Self::Instant {
let threads = palaver::process::count_threads();
let threads = Self::count_threads();
println!("[ThreadWatcher] Threads running on the machine at the start of a call: {}", threads);
threads as isize
}

fn on_response(&self, started_at: Self::Instant) {
let current_nr_threads = palaver::process::count_threads() as isize;
let current_nr_threads = Self::count_threads() as isize;
println!("[ThreadWatcher] Request started {} threads", current_nr_threads - started_at);
}
}
Expand Down

0 comments on commit b41acab

Please sign in to comment.