geekorm_core/backends/libsql/
mutex.rs

1use serde::{Serialize, de::DeserializeOwned};
2use std::sync::Arc;
3
4#[cfg(not(feature = "backends-tokio"))]
5use std::sync::Mutex;
6#[cfg(feature = "backends-tokio")]
7use tokio::sync::Mutex;
8
9use crate::{GeekConnection, QueryBuilderTrait, TableBuilder};
10
11const WAIT: std::time::Duration = std::time::Duration::from_nanos(100);
12const TIMEOUT: std::time::Duration = std::time::Duration::from_secs(5);
13
14impl<C> GeekConnection for Arc<Mutex<C>>
15where
16    // Self: Sized + Sync + Send + 'static,
17    C: GeekConnection<Connection = libsql::Connection>,
18{
19    type Connection = Arc<Mutex<libsql::Connection>>;
20
21    async fn create_table<T>(connection: &Self::Connection) -> Result<(), crate::Error>
22    where
23        T: TableBuilder + QueryBuilderTrait + Sized + Serialize + DeserializeOwned,
24    {
25        let start = std::time::Instant::now();
26        while start.elapsed() < TIMEOUT {
27            match connection.try_lock() {
28                Ok(conn) => return C::create_table::<T>(&conn).await,
29                Err(_) => {
30                    std::thread::sleep(WAIT);
31                }
32            }
33        }
34        Err(crate::Error::LibSQLError {
35            error: "Error getting write lock on connection".to_string(),
36            query: "N/A".to_string(),
37        })
38    }
39
40    async fn row_count(
41        connection: &Self::Connection,
42        query: crate::Query,
43    ) -> Result<i64, crate::Error> {
44        let start = std::time::Instant::now();
45        while start.elapsed() < TIMEOUT {
46            match connection.try_lock() {
47                Ok(conn) => return C::row_count(&conn, query).await,
48                Err(_) => {
49                    std::thread::sleep(WAIT);
50                }
51            }
52        }
53        Err(crate::Error::LibSQLError {
54            error: "Error getting write lock on connection in row_count".to_string(),
55            query: "N/A".to_string(),
56        })
57    }
58
59    async fn query<T>(
60        connection: &Self::Connection,
61        query: crate::Query,
62    ) -> Result<Vec<T>, crate::Error>
63    where
64        T: serde::de::DeserializeOwned,
65    {
66        let start = std::time::Instant::now();
67        while start.elapsed() < TIMEOUT {
68            match connection.try_lock() {
69                Ok(conn) => return C::query::<T>(&conn, query).await,
70                Err(_) => {
71                    std::thread::sleep(WAIT);
72                }
73            }
74        }
75
76        Err(crate::Error::LibSQLError {
77            error: "Error getting write lock on connection".to_string(),
78            query: "N/A".to_string(),
79        })
80    }
81
82    async fn query_first<T>(
83        connection: &Self::Connection,
84        query: crate::Query,
85    ) -> Result<T, crate::Error>
86    where
87        T: serde::de::DeserializeOwned,
88    {
89        let start = std::time::Instant::now();
90        while start.elapsed() < TIMEOUT {
91            match connection.try_lock() {
92                Ok(conn) => return C::query_first::<T>(&conn, query).await,
93                Err(_) => {
94                    std::thread::sleep(WAIT);
95                }
96            }
97        }
98        Err(crate::Error::LibSQLError {
99            error: "Error getting write lock on connection".to_string(),
100            query: "N/A".to_string(),
101        })
102    }
103
104    async fn execute(
105        connection: &Self::Connection,
106        query: crate::Query,
107    ) -> Result<(), crate::Error> {
108        let start = std::time::Instant::now();
109        while start.elapsed() < TIMEOUT {
110            match connection.try_lock() {
111                Ok(conn) => return C::execute(&conn, query).await,
112                Err(_) => {
113                    std::thread::sleep(WAIT);
114                }
115            }
116        }
117        Err(crate::Error::LibSQLError {
118            error: "Error getting write lock on connection in execute".to_string(),
119            query: "N/A".to_string(),
120        })
121    }
122}