|
@@ -3,7 +3,6 @@ static GLOBAL: mimalloc::MiMalloc = mimalloc::MiMalloc;
|
|
|
|
|
|
use std::fmt::Write;
|
|
use std::fmt::Write;
|
|
use std::io;
|
|
use std::io;
|
|
-use std::sync::atomic::{AtomicUsize, Ordering};
|
|
|
|
use std::sync::Arc;
|
|
use std::sync::Arc;
|
|
|
|
|
|
use bytes::BytesMut;
|
|
use bytes::BytesMut;
|
|
@@ -44,7 +43,6 @@ pub struct Fortune<'a> {
|
|
}
|
|
}
|
|
|
|
|
|
struct PgConnectionPool {
|
|
struct PgConnectionPool {
|
|
- idx: AtomicUsize,
|
|
|
|
clients: Vec<PgConnection>,
|
|
clients: Vec<PgConnection>,
|
|
}
|
|
}
|
|
|
|
|
|
@@ -53,18 +51,14 @@ impl PgConnectionPool {
|
|
let clients = (0..size)
|
|
let clients = (0..size)
|
|
.map(|_| std::thread::spawn(move || PgConnection::new(db_url)))
|
|
.map(|_| std::thread::spawn(move || PgConnection::new(db_url)))
|
|
.collect::<Vec<_>>();
|
|
.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 len = self.clients.len();
|
|
- let connection = &self.clients[idx % len];
|
|
|
|
|
|
+ let connection = &self.clients[id % len];
|
|
PgConnection {
|
|
PgConnection {
|
|
client: connection.client.clone(),
|
|
client: connection.client.clone(),
|
|
statement: connection.statement.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 {
|
|
for w in &worlds {
|
|
params.push(&w.id);
|
|
params.push(&w.id);
|
|
params.push(&w.randomnumber);
|
|
params.push(&w.randomnumber);
|
|
@@ -195,8 +189,10 @@ impl PgConnection {
|
|
params.push(&w.id);
|
|
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], ¶ms);
|
|
Ok(worlds)
|
|
Ok(worlds)
|
|
}
|
|
}
|
|
|
|
|
|
@@ -279,8 +275,8 @@ struct HttpServer {
|
|
impl HttpServiceFactory for HttpServer {
|
|
impl HttpServiceFactory for HttpServer {
|
|
type Service = Techempower;
|
|
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();
|
|
let rng = WyRand::new();
|
|
Techempower { db, rng }
|
|
Techempower { db, rng }
|
|
}
|
|
}
|