1- use sqlx:: { postgres:: PgPoolOptions , types:: BigDecimal , Pool , Postgres } ;
1+ use db:: { orchestrator:: DbOrchestartor , retry:: RetryConfig } ;
2+ use sqlx:: types:: BigDecimal ;
23
34#[ derive( Clone , Debug ) ]
45pub struct Db {
5- pool : Pool < Postgres > ,
6+ orchestartor : DbOrchestartor ,
67}
78
89#[ derive( Debug , Clone ) ]
@@ -11,14 +12,19 @@ pub enum DbError {
1112}
1213
1314impl Db {
14- pub async fn try_new ( connection_url : & str ) -> Result < Self , DbError > {
15- let pool = PgPoolOptions :: new ( )
16- . max_connections ( 5 )
17- . connect ( connection_url)
18- . await
19- . map_err ( |e| DbError :: ConnectError ( e. to_string ( ) ) ) ?;
15+ pub async fn try_new ( connection_urls : & [ & str ] ) -> Result < Self , DbError > {
16+ let orchestartor = DbOrchestartor :: try_new (
17+ connection_urls,
18+ RetryConfig {
19+ factor : 0.0 ,
20+ max_delay_seconds : 0 ,
21+ max_times : 0 ,
22+ min_delay_millis : 0 ,
23+ } ,
24+ )
25+ . map_err ( |e| DbError :: ConnectError ( e. to_string ( ) ) ) ?;
2026
21- Ok ( Self { pool } )
27+ Ok ( Self { orchestartor } )
2228 }
2329
2430 pub async fn insert_payment_event (
@@ -29,18 +35,23 @@ impl Db {
2935 valid_until : & BigDecimal ,
3036 tx_hash : & str ,
3137 ) -> Result < ( ) , sqlx:: Error > {
32- sqlx:: query (
33- "INSERT INTO payment_events (address, started_at, amount, valid_until, tx_hash)
34- VALUES ($1, $2, $3, $4, $5)
35- ON CONFLICT (tx_hash) DO NOTHING" ,
36- )
37- . bind ( address. to_lowercase ( ) )
38- . bind ( started_at)
39- . bind ( amount)
40- . bind ( valid_until)
41- . bind ( tx_hash)
42- . execute ( & self . pool )
43- . await
44- . map ( |_| ( ) )
38+ self . orchestartor
39+ . write ( async |pool| {
40+ sqlx:: query (
41+ "INSERT INTO payment_events (address, started_at, amount, valid_until, tx_hash)
42+ VALUES ($1, $2, $3, $4, $5)
43+ ON CONFLICT (tx_hash) DO NOTHING" ,
44+ )
45+ . bind ( address. to_lowercase ( ) )
46+ . bind ( started_at)
47+ . bind ( amount)
48+ . bind ( valid_until)
49+ . bind ( tx_hash)
50+ . execute ( & pool)
51+ . await ?;
52+
53+ Ok ( ( ) )
54+ } )
55+ . await
4556 }
4657}
0 commit comments