13 releases

0.2.5 Sep 15, 2023
0.2.4 Sep 14, 2023
0.2.2 Aug 18, 2023
0.1.10 Aug 17, 2023

#10 in #reconnect

Download history 21/week @ 2024-05-06 27/week @ 2024-05-13 27/week @ 2024-05-20 21/week @ 2024-05-27 22/week @ 2024-06-03 27/week @ 2024-06-10 14/week @ 2024-06-17 33/week @ 2024-06-24 9/week @ 2024-07-08 31/week @ 2024-07-15 19/week @ 2024-07-22 38/week @ 2024-07-29 18/week @ 2024-08-05 14/week @ 2024-08-12 13/week @ 2024-08-19

83 downloads per month
Used in 7 crates (4 directly)

BUSL-1.1

12KB
266 lines

Base on tokio postgres

Keep one postgres connection, will auto reconnect when connect close

基于 tokio postgres , 保留一个 postgres 连接 , 连接关闭时会自动重新连接

→ tests/main.rs

use lazy_static::lazy_static;
use pgw::{Pg, Sql};
use tokio::time;

lazy_static! {
    // get postgres connection uri from environment ( without prefix )
    static ref PG: Pg = Pg::new_with_env("PG_URI");
    // prepared sql
    static ref SQL_NSPNAME: Sql = PG.sql("SELECT oid FROM pg_catalog.pg_namespace LIMIT 2");
}
use tokio_postgres::types::Oid;
//
// lazy_static! {
// pub static ref SQL_LI: pgw::Sql = PG.sql("SELECT task.id FROM bot.task,bot.civitai_img WHERE hash IS NOT NULL AND bot.task.rid=bot.civitai_img.id AND task.adult=0 AND cid=1 ORDER BY star DESC LIMIT 512");
// }
//
// pub async fn li() -> Result<Vec<u64>, pgw::Error> {
//   Ok(
//     PG.query(&*SQL_LI, &[])
//       .await?
//       .iter()
//       .map(|r| r.get::<_, u64>(0))
//       .collect(),
//   )
// }

#[tokio::test]
async fn main() -> anyhow::Result<()> {
  loginit::init();
  // dbg!(li().await?);
  for i in 0..99999 {
    println!("loop {i}");
    match PG.query(&*SQL_NSPNAME, &[]).await {
      Ok(li) => {
        for i in li {
          let oid: Oid = i.try_get(0).unwrap();
          dbg!(oid);
        }
      }
      Err(err) => {
        dbg!(err);
      }
    }
    match PG
      .query_one("SELECT oid FROM pg_catalog.pg_namespace LIMIT 1", &[])
      .await
    {
      Ok(i) => {
        let oid: Oid = i.try_get(0).unwrap();
        dbg!(oid);
      }
      Err(err) => {
        dbg!(err);
      }
    }
    time::sleep(std::time::Duration::from_secs(6)).await;
  }
  Ok(())
}

Dependencies

~7–16MB
~229K SLoC