|
@@ -1,7 +1,6 @@
|
|
#![allow(clippy::uninit_vec)]
|
|
#![allow(clippy::uninit_vec)]
|
|
-use std::{borrow::Cow, cell::RefCell, fmt::Write as FmtWrite, rc::Rc};
|
|
|
|
|
|
+use std::{borrow::Cow, cell::RefCell, fmt::Write as FmtWrite};
|
|
|
|
|
|
-use futures::{Future, FutureExt};
|
|
|
|
use nanorand::{Rng, WyRand};
|
|
use nanorand::{Rng, WyRand};
|
|
use ntex::util::{BufMut, Bytes, BytesMut};
|
|
use ntex::util::{BufMut, Bytes, BytesMut};
|
|
use smallvec::SmallVec;
|
|
use smallvec::SmallVec;
|
|
@@ -30,7 +29,7 @@ pub struct PgConnection {
|
|
world: Statement,
|
|
world: Statement,
|
|
rng: WyRand,
|
|
rng: WyRand,
|
|
updates: Vec<Statement>,
|
|
updates: Vec<Statement>,
|
|
- buf: Rc<RefCell<BytesMut>>,
|
|
|
|
|
|
+ buf: RefCell<BytesMut>,
|
|
}
|
|
}
|
|
|
|
|
|
impl PgConnection {
|
|
impl PgConnection {
|
|
@@ -38,7 +37,9 @@ impl PgConnection {
|
|
let (cl, conn) = connect(db_url)
|
|
let (cl, conn) = connect(db_url)
|
|
.await
|
|
.await
|
|
.expect("can not connect to postgresql");
|
|
.expect("can not connect to postgresql");
|
|
- ntex::rt::spawn(conn.map(|_| ()));
|
|
|
|
|
|
+ ntex::rt::spawn(async move {
|
|
|
|
+ let _ = conn.await;
|
|
|
|
+ });
|
|
|
|
|
|
let fortune = cl.prepare("SELECT * FROM fortune").await.unwrap();
|
|
let fortune = cl.prepare("SELECT * FROM fortune").await.unwrap();
|
|
let mut updates = Vec::new();
|
|
let mut updates = Vec::new();
|
|
@@ -67,32 +68,28 @@ impl PgConnection {
|
|
world,
|
|
world,
|
|
updates,
|
|
updates,
|
|
rng: WyRand::new(),
|
|
rng: WyRand::new(),
|
|
- buf: Rc::new(RefCell::new(BytesMut::with_capacity(65535))),
|
|
|
|
|
|
+ buf: RefCell::new(BytesMut::with_capacity(65535)),
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
impl PgConnection {
|
|
impl PgConnection {
|
|
- pub fn get_world(&self) -> impl Future<Output = Bytes> {
|
|
|
|
- let buf = self.buf.clone();
|
|
|
|
|
|
+ pub async fn get_world(&self) -> Bytes {
|
|
let random_id = (self.rng.clone().generate::<u32>() % 10_000 + 1) as i32;
|
|
let random_id = (self.rng.clone().generate::<u32>() % 10_000 + 1) as i32;
|
|
- self.cl
|
|
|
|
- .query_one(&self.world, &[&random_id])
|
|
|
|
- .map(move |row| {
|
|
|
|
- let row = row.unwrap();
|
|
|
|
- let mut body = buf.borrow_mut();
|
|
|
|
- utils::reserve(&mut body);
|
|
|
|
- World {
|
|
|
|
- id: row.get(0),
|
|
|
|
- randomnumber: row.get(1),
|
|
|
|
- }
|
|
|
|
- .to_bytes_mut(&mut *body);
|
|
|
|
- body.split().freeze()
|
|
|
|
- })
|
|
|
|
|
|
+
|
|
|
|
+ let row = self.cl.query_one(&self.world, &[&random_id]).await.unwrap();
|
|
|
|
+
|
|
|
|
+ let mut body = self.buf.borrow_mut();
|
|
|
|
+ utils::reserve(&mut body);
|
|
|
|
+ World {
|
|
|
|
+ id: row.get(0),
|
|
|
|
+ randomnumber: row.get(1),
|
|
|
|
+ }
|
|
|
|
+ .to_bytes_mut(&mut *body);
|
|
|
|
+ body.split().freeze()
|
|
}
|
|
}
|
|
|
|
|
|
- pub fn get_worlds(&self, num: usize) -> impl Future<Output = Bytes> {
|
|
|
|
- let buf = self.buf.clone();
|
|
|
|
|
|
+ pub async fn get_worlds(&self, num: usize) -> Bytes {
|
|
let mut rng = self.rng.clone();
|
|
let mut rng = self.rng.clone();
|
|
let mut queries = SmallVec::<[_; 32]>::new();
|
|
let mut queries = SmallVec::<[_; 32]>::new();
|
|
(0..num).for_each(|_| {
|
|
(0..num).for_each(|_| {
|
|
@@ -100,32 +97,28 @@ impl PgConnection {
|
|
queries.push(self.cl.query_one(&self.world, &[&w_id]));
|
|
queries.push(self.cl.query_one(&self.world, &[&w_id]));
|
|
});
|
|
});
|
|
|
|
|
|
- async move {
|
|
|
|
- let mut worlds = SmallVec::<[_; 32]>::new();
|
|
|
|
- for fut in queries {
|
|
|
|
- let row = fut.await.unwrap();
|
|
|
|
- worlds.push(World {
|
|
|
|
- id: row.get(0),
|
|
|
|
- randomnumber: row.get(1),
|
|
|
|
- })
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- let mut body = buf.borrow_mut();
|
|
|
|
- utils::reserve(&mut body);
|
|
|
|
-
|
|
|
|
- body.put_u8(b'[');
|
|
|
|
- worlds.iter().for_each(|w| {
|
|
|
|
- w.to_bytes_mut(&mut *body);
|
|
|
|
- body.put_u8(b',');
|
|
|
|
- });
|
|
|
|
- let idx = body.len() - 1;
|
|
|
|
- body[idx] = b']';
|
|
|
|
- body.split().freeze()
|
|
|
|
|
|
+ let mut worlds = SmallVec::<[_; 32]>::new();
|
|
|
|
+ for fut in queries {
|
|
|
|
+ let row = fut.await.unwrap();
|
|
|
|
+ worlds.push(World {
|
|
|
|
+ id: row.get(0),
|
|
|
|
+ randomnumber: row.get(1),
|
|
|
|
+ })
|
|
}
|
|
}
|
|
|
|
+
|
|
|
|
+ let mut body = self.buf.borrow_mut();
|
|
|
|
+ utils::reserve(&mut body);
|
|
|
|
+ body.put_u8(b'[');
|
|
|
|
+ worlds.iter().for_each(|w| {
|
|
|
|
+ w.to_bytes_mut(&mut *body);
|
|
|
|
+ body.put_u8(b',');
|
|
|
|
+ });
|
|
|
|
+ let idx = body.len() - 1;
|
|
|
|
+ body[idx] = b']';
|
|
|
|
+ body.split().freeze()
|
|
}
|
|
}
|
|
|
|
|
|
- pub fn update(&self, num: usize) -> impl Future<Output = Bytes> {
|
|
|
|
- let buf = self.buf.clone();
|
|
|
|
|
|
+ pub async fn update(&self, num: usize) -> Bytes {
|
|
let mut rng = self.rng.clone();
|
|
let mut rng = self.rng.clone();
|
|
let mut queries = SmallVec::<[_; 32]>::new();
|
|
let mut queries = SmallVec::<[_; 32]>::new();
|
|
(0..num).for_each(|_| {
|
|
(0..num).for_each(|_| {
|
|
@@ -133,78 +126,60 @@ impl PgConnection {
|
|
queries.push(self.cl.query_one(&self.world, &[&w_id]));
|
|
queries.push(self.cl.query_one(&self.world, &[&w_id]));
|
|
});
|
|
});
|
|
|
|
|
|
- let cl = self.cl.clone();
|
|
|
|
- let st = self.updates[num - 1].clone();
|
|
|
|
- let base = num * 2;
|
|
|
|
- async move {
|
|
|
|
- let mut worlds = SmallVec::<[_; 32]>::new();
|
|
|
|
- let mut params_data: Vec<i32> = Vec::with_capacity(num * 3);
|
|
|
|
- unsafe {
|
|
|
|
- params_data.set_len(num * 3);
|
|
|
|
- }
|
|
|
|
- for (idx, fut) in queries.into_iter().enumerate() {
|
|
|
|
- let q = fut.await.unwrap();
|
|
|
|
- let id = (rng.generate::<u32>() % 10_000 + 1) as i32;
|
|
|
|
- let wid = q.get(0);
|
|
|
|
- let randomnumber = id;
|
|
|
|
-
|
|
|
|
- params_data[idx * 2] = wid;
|
|
|
|
- params_data[idx * 2 + 1] = randomnumber;
|
|
|
|
- params_data[base + idx] = wid;
|
|
|
|
- worlds.push(World {
|
|
|
|
- id: wid,
|
|
|
|
- randomnumber,
|
|
|
|
- });
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- let mut params: Vec<&dyn ToSql> = Vec::with_capacity(num as usize * 3);
|
|
|
|
- for i in params_data.iter() {
|
|
|
|
- params.push(i);
|
|
|
|
- }
|
|
|
|
- let _ = cl
|
|
|
|
- .query(&st, ¶ms)
|
|
|
|
- .await;
|
|
|
|
-
|
|
|
|
- let mut body = buf.borrow_mut();
|
|
|
|
- utils::reserve(&mut body);
|
|
|
|
- body.put_u8(b'[');
|
|
|
|
- worlds.iter().for_each(|w| {
|
|
|
|
- w.to_bytes_mut(&mut *body);
|
|
|
|
- body.put_u8(b',');
|
|
|
|
|
|
+ let mut worlds = SmallVec::<[_; 32]>::new();
|
|
|
|
+ for fut in queries.into_iter() {
|
|
|
|
+ let row = fut.await.unwrap();
|
|
|
|
+ worlds.push(World {
|
|
|
|
+ id: row.get(0),
|
|
|
|
+ randomnumber: (rng.generate::<u32>() % 10_000 + 1) as i32,
|
|
});
|
|
});
|
|
- let idx = body.len() - 1;
|
|
|
|
- body[idx] = b']';
|
|
|
|
- body.split().freeze()
|
|
|
|
}
|
|
}
|
|
|
|
+
|
|
|
|
+ let mut params: Vec<&dyn ToSql> = Vec::with_capacity(num * 3);
|
|
|
|
+ for w in &worlds {
|
|
|
|
+ params.push(&w.id);
|
|
|
|
+ params.push(&w.randomnumber);
|
|
|
|
+ }
|
|
|
|
+ for w in &worlds {
|
|
|
|
+ params.push(&w.id);
|
|
|
|
+ }
|
|
|
|
+ let _ = self.cl.query(&self.updates[num - 1], ¶ms).await;
|
|
|
|
+
|
|
|
|
+ let mut body = self.buf.borrow_mut();
|
|
|
|
+ utils::reserve(&mut body);
|
|
|
|
+ body.put_u8(b'[');
|
|
|
|
+ worlds.iter().for_each(|w| {
|
|
|
|
+ w.to_bytes_mut(&mut *body);
|
|
|
|
+ body.put_u8(b',');
|
|
|
|
+ });
|
|
|
|
+ let idx = body.len() - 1;
|
|
|
|
+ body[idx] = b']';
|
|
|
|
+ body.split().freeze()
|
|
}
|
|
}
|
|
|
|
|
|
- pub fn tell_fortune(&self) -> impl Future<Output = Bytes> {
|
|
|
|
- let buf = self.buf.clone();
|
|
|
|
|
|
+ pub async fn tell_fortune(&self) -> Bytes {
|
|
let fut = self.cl.query_raw(&self.fortune, &[]);
|
|
let fut = self.cl.query_raw(&self.fortune, &[]);
|
|
|
|
|
|
- async move {
|
|
|
|
- let rows = fut.await.unwrap();
|
|
|
|
- let mut fortunes: SmallVec<[_; 32]> = smallvec::smallvec![Fortune {
|
|
|
|
- id: 0,
|
|
|
|
- message: Cow::Borrowed("Additional fortune added at request time."),
|
|
|
|
- }];
|
|
|
|
-
|
|
|
|
- for row in rows {
|
|
|
|
- fortunes.push(Fortune {
|
|
|
|
- id: row.get(0),
|
|
|
|
- message: Cow::Owned(row.get(1)),
|
|
|
|
- });
|
|
|
|
- }
|
|
|
|
|
|
+ let rows = fut.await.unwrap();
|
|
|
|
+ let mut fortunes: SmallVec<[_; 32]> = smallvec::smallvec![Fortune {
|
|
|
|
+ id: 0,
|
|
|
|
+ message: Cow::Borrowed("Additional fortune added at request time."),
|
|
|
|
+ }];
|
|
|
|
|
|
- fortunes.sort_by(|it, next| it.message.cmp(&next.message));
|
|
|
|
|
|
+ for row in rows {
|
|
|
|
+ fortunes.push(Fortune {
|
|
|
|
+ id: row.get(0),
|
|
|
|
+ message: Cow::Owned(row.get(1)),
|
|
|
|
+ });
|
|
|
|
+ }
|
|
|
|
|
|
- let mut body = std::mem::replace(&mut *buf.borrow_mut(), BytesMut::new());
|
|
|
|
- utils::reserve(&mut body);
|
|
|
|
- ywrite_html!(body, "{{> fortune }}");
|
|
|
|
|
|
+ fortunes.sort_by(|it, next| it.message.cmp(&next.message));
|
|
|
|
|
|
- let result = body.split().freeze();
|
|
|
|
- let _ = std::mem::replace(&mut *buf.borrow_mut(), body);
|
|
|
|
- result
|
|
|
|
- }
|
|
|
|
|
|
+ let mut body = std::mem::replace(&mut *self.buf.borrow_mut(), BytesMut::new());
|
|
|
|
+ utils::reserve(&mut body);
|
|
|
|
+ ywrite_html!(body, "{{> fortune }}");
|
|
|
|
+ let result = body.split().freeze();
|
|
|
|
+ let _ = std::mem::replace(&mut *self.buf.borrow_mut(), body);
|
|
|
|
+ result
|
|
}
|
|
}
|
|
}
|
|
}
|