Skip to content

Instantly share code, notes, and snippets.

View jayhuang75's full-sized avatar

jayhuang75 jayhuang75

View GitHub Profile
@jayhuang75
jayhuang75 / nextjs-rust-twitter-streaming-websocket-save-trigger-twitter-stream.rs
Created May 28, 2022 16:38
nextjs-rust-twitter-streaming-websocket-save-trigger-twitter-stream.rs
client.sender = Some(client_sender);
clients.write().await.insert(id.clone(), client);
log::info!("[ws] client_connect : {} connected", id);
let locked = clients.write().await;
if let Some(v) = locked.get(&id) {
if let Some(sender) = &v.sender {
log::info!("[ws] client_msg : topic {:?}", v.topic);
twitter_stream(v.topic.clone(), sender.clone()).await;
@jayhuang75
jayhuang75 / nextjs-rust-twitter-streaming-websocket-split.rs
Last active May 28, 2022 16:36
nextjs-rust-twitter-streaming-websocket-split.rs
let (client_ws_sender, mut _client_ws_rcv) = ws.split();
let (client_sender, client_rcv) = mpsc::unbounded_channel();
let client_rcv = UnboundedReceiverStream::new(client_rcv);
tokio::task::spawn(client_rcv.forward(client_ws_sender).map(|result| {
if let Err(e) = result {
log::error!("Failed toerror sending websocket msg: {}", e);
}
}));
@jayhuang75
jayhuang75 / rust-nextjs-twitter-streaming-registeration.rs
Last active May 28, 2022 16:17
rust-nextjs-twitter-streaming-registeration.rs
let register = warp::path("register");
let register_routes = register
.and(warp::post())
.and(warp::body::json())
.and(with_clients(client_store.clone()))
.and_then(app::handlers::register_handler)
.or(register
.and(warp::delete())
.and(warp::path::param())
.and(with_clients(client_store.clone()))
@jayhuang75
jayhuang75 / nextjs-rust-twitter-websocket-model.rs
Last active May 28, 2022 03:06
nextjs-rust-twitter-websocket-model.rs
use crate::ClientStore;
use uuid::Uuid;
use warp::ws::Message;
use tokio::sync::{mpsc};
use serde::{Deserialize, Serialize};
#[derive(Debug, Clone)]
pub struct Client {
pub user_id: Uuid,
pub topic: String,
@jayhuang75
jayhuang75 / rust-velocity-limit-unbuffered-bench.sh
Created April 30, 2022 13:25
rust-velocity-limit-unbuffered-bench
Benchmark 1: ./rust-velocity-limit
Time (mean ± σ): 13.0 ms ± 1.4 ms [User: 4.5 ms, System: 7.0 ms]
Range (min … max): 11.1 ms … 16.7 ms 155 runs
Benchmark 2: ./go-velocity-limit
Time (mean ± σ): 9.9 ms ± 0.8 ms [User: 11.7 ms, System: 6.3 ms]
Range (min … max): 8.9 ms … 13.3 ms 214 runs
Summary
'./go-velocity-limit' ran
@jayhuang75
jayhuang75 / rust-velocity-limit-buffered-bench.sh
Created April 30, 2022 13:24
rust-velocity-limit-buffered-bench
Benchmark 1: ./rust-velocity-limit
Time (mean ± σ): 7.3 ms ± 0.6 ms [User: 4.2 ms, System: 1.6 ms]
Range (min … max): 5.4 ms … 11.8 ms 234 runs
Benchmark 2: ./go-velocity-limit
Time (mean ± σ): 9.6 ms ± 0.5 ms [User: 11.5 ms, System: 6.1 ms]
Range (min … max): 8.8 ms … 11.6 ms 216 runs
Summary
'./rust-velocity-limit' ran
@jayhuang75
jayhuang75 / rust-velocity-limit-unbufferd-run.sh
Created April 30, 2022 13:21
rust-velocity-limit-unbufferd-run
❯ cargo run -r
Finished release [optimized] target(s) in 0.17s
Running `target/release/rust-velocity-limit`
duration total execution time: 11.30037ms
@jayhuang75
jayhuang75 / rust-velocity-limit-file-output.rs
Created April 30, 2022 13:17
rust-velocity-limit-file-output
{"id":"31808","customer_id":"511","accept":true}
{"id":"558","customer_id":"256","accept":false}
@jayhuang75
jayhuang75 / rust-velocity-limit-file-example.rs
Created April 30, 2022 13:16
rust-velocity-limit-file-example
{"id":"1234","customer_id":"528","load_amount":"$3318.47","time":"2000-01-01T00:00:00Z"}
{"id":"5678","customer_id":"154","load_amount":"$1413.18","time":"2000-01-01T01:01:22Z"}
@jayhuang75
jayhuang75 / rust-velocity-limit-bufwriter.rs
Created April 28, 2022 21:05
rust-velocity-limit-bufwriter
let f = File::create(path).expect("Unable to create file");
+let mut buf = BufWriter::new(f);
for line in &self.output{
let serialized = serde_json::to_string(&line).unwrap();
writeln!(&mut buf, "{}", serialized).unwrap();
}