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 13/week @ 2024-01-08 7/week @ 2024-01-15 9/week @ 2024-02-05 17/week @ 2024-02-12 45/week @ 2024-02-19 33/week @ 2024-02-26 28/week @ 2024-03-04 36/week @ 2024-03-11 28/week @ 2024-03-18 35/week @ 2024-03-25 52/week @ 2024-04-01 18/week @ 2024-04-08 23/week @ 2024-04-15 27/week @ 2024-04-22

127 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

~8–18MB
~310K SLoC