MCPcopy Create free account
hub / github.com/chatmail/core / get

Method get

src/sql/pool.rs:93–134  ·  view source on GitHub ↗

Retrieves a connection from the pool. Sets `query_only` pragma to the provided value to prevent accidental misuse of connection for writing when reading is intended. Only pass `query_only=false` if you want to use the connection for writing.

(self: Arc<Self>, query_only: bool)

Source from the content-addressed store, hash-verified

91 /// Only pass `query_only=false` if you want
92 /// to use the connection for writing.
93 pub async fn get(self: Arc<Self>, query_only: bool) -> Result<PooledConnection> {
94 if query_only {
95 let permit = self.semaphore.clone().acquire_owned().await?;
96 let conn = {
97 let mut connections = self.connections.lock();
98 connections
99 .pop()
100 .context("Got a permit when there are no connections in the pool")?
101 };
102 let conn = PooledConnection {
103 pool: Arc::downgrade(&self),
104 conn: Some(conn),
105 _permit: permit,
106 _write_mutex_guard: None,
107 };
108 conn.pragma_update(None, "query_only", "1")?;
109 Ok(conn)
110 } else {
111 // We get write guard first to avoid taking a permit
112 // and not using it, blocking a reader from getting a connection
113 // while being ourselves blocked by another wrtier.
114 let write_mutex_guard = Arc::clone(&self.write_mutex).lock_owned().await;
115
116 // We may still have to wait for a connection
117 // to be returned by some reader.
118 let permit = self.semaphore.clone().acquire_owned().await?;
119 let conn = {
120 let mut connections = self.connections.lock();
121 connections.pop().context(
122 "Got a permit and write lock when there are no connections in the pool",
123 )?
124 };
125 let conn = PooledConnection {
126 pool: Arc::downgrade(&self),
127 conn: Some(conn),
128 _permit: permit,
129 _write_mutex_guard: Some(write_mutex_guard),
130 };
131 conn.pragma_update(None, "query_only", "0")?;
132 Ok(conn)
133 }
134 }
135}
136
137/// Pooled connection.

Callers 14

get_connectivity_htmlMethod · 0.45
test_auto_vacuumFunction · 0.45
test_query_onlyFunction · 0.45
migrate_key_contactsFunction · 0.45
msgs_to_key_contactsFunction · 0.45
runFunction · 0.45
wal_checkpointFunction · 0.45
test_stats_one_contactFunction · 0.45

Calls 1

cloneMethod · 0.80

Tested by 6

test_auto_vacuumFunction · 0.36
test_query_onlyFunction · 0.36
test_stats_one_contactFunction · 0.36
test_cryptography_statsFunction · 0.36