瀏覽代碼

[may_minihttp] support db client thread id calc (#7921)

Xudong Huang 2 年之前
父節點
當前提交
9a2fe67fc5

+ 5 - 5
frameworks/Rust/may-minihttp/Cargo.toml

@@ -14,18 +14,18 @@ log = { version = "0.4", features = ["release_max_level_off"] }
 mimalloc = { version = "0.1", default-features = false }
 nanorand = { version = "0.7", default-features = false, features = ["std", "wyrand"] }
 
-buf-min = { version = "0.7", features = ["bytes"] }
 yarte = { version = "0.15", features = ["bytes-buf", "json"] }
+buf-min = { version = "0.7", features = ["bytes"] }
 
-may = { version = "0.3.25", default-features = false }
-may_postgres = { git = "https://github.com/Xudong-Huang/may_postgres.git" }
-may_minihttp = { git = "https://github.com/Xudong-Huang/may_minihttp.git" }
+may = { version = "0.3", default-features = false }
+may_minihttp = { git = "https://github.com/Xudong-Huang/may_minihttp.git", rev = "dc8a146", default-features = false }
+may_postgres = { git = "https://github.com/Xudong-Huang/may_postgres.git", rev = "74e639d", default-features = false }
 
 [profile.release]
 opt-level = 3
 codegen-units = 1
 panic = 'abort'
-lto = "thin"
+lto = 'thin'
 debug = false
 incremental = false
 overflow-checks = false

+ 1 - 1
frameworks/Rust/may-minihttp/may-minihttp.dockerfile

@@ -1,4 +1,4 @@
-FROM rust:1.66
+FROM rust:1.67
 
 RUN apt-get update -yqq && apt-get install -yqq cmake g++
 

+ 12 - 16
frameworks/Rust/may-minihttp/src/main.rs

@@ -3,7 +3,6 @@ static GLOBAL: mimalloc::MiMalloc = mimalloc::MiMalloc;
 
 use std::fmt::Write;
 use std::io;
-use std::sync::atomic::{AtomicUsize, Ordering};
 use std::sync::Arc;
 
 use bytes::BytesMut;
@@ -44,7 +43,6 @@ pub struct Fortune<'a> {
 }
 
 struct PgConnectionPool {
-    idx: AtomicUsize,
     clients: Vec<PgConnection>,
 }
 
@@ -53,18 +51,14 @@ impl PgConnectionPool {
         let clients = (0..size)
             .map(|_| std::thread::spawn(move || PgConnection::new(db_url)))
             .collect::<Vec<_>>();
-        let clients = clients.into_iter().map(|t| t.join().unwrap()).collect();
-
-        PgConnectionPool {
-            idx: AtomicUsize::new(0),
-            clients,
-        }
+        let mut clients: Vec<_> = clients.into_iter().map(|t| t.join().unwrap()).collect();
+        clients.sort_by(|a, b| (a.client.id() % size).cmp(&(b.client.id() % size)));
+        PgConnectionPool { clients }
     }
 
-    fn get_connection(&self) -> PgConnection {
-        let idx = self.idx.fetch_add(1, Ordering::Relaxed);
+    fn get_connection(&self, id: usize) -> PgConnection {
         let len = self.clients.len();
-        let connection = &self.clients[idx % len];
+        let connection = &self.clients[id % len];
         PgConnection {
             client: connection.client.clone(),
             statement: connection.statement.clone(),
@@ -186,7 +180,7 @@ impl PgConnection {
             }
         }
 
-        let mut params: Vec<&(dyn ToSql)> = Vec::with_capacity(num * 3);
+        let mut params: Vec<&(dyn ToSql + Sync)> = Vec::with_capacity(num * 3);
         for w in &worlds {
             params.push(&w.id);
             params.push(&w.randomnumber);
@@ -195,8 +189,10 @@ impl PgConnection {
             params.push(&w.id);
         }
 
-        self.client
-            .query_raw(&self.statement.updates[num - 1], params)?;
+        // use `query_one` to sync wait result
+        let _ = self
+            .client
+            .query_one(&self.statement.updates[num - 1], &params);
         Ok(worlds)
     }
 
@@ -279,8 +275,8 @@ struct HttpServer {
 impl HttpServiceFactory for HttpServer {
     type Service = Techempower;
 
-    fn new_service(&self) -> Self::Service {
-        let db = self.db_pool.get_connection();
+    fn new_service(&self, id: usize) -> Self::Service {
+        let db = self.db_pool.get_connection(id);
         let rng = WyRand::new();
         Techempower { db, rng }
     }