Two teams try and fill in any horizontal, vertical, or diagonal line on a bingo board by playing maps on osu!
osu.bingo
osu
1use super::DbContext;
2
3use r2d2::{Pool, PooledConnection};
4use redis::Client;
5
6impl DbContext {
7 pub(super) async fn get_redis_connection(&mut self) -> Option<PooledConnection<Client>> {
8 if let Some(pool) = self.redis_pool.as_ref() {
9 match pool.get() {
10 Ok(x) => return Some(x),
11 Err(e) => {
12 log::error!("Failed to grab redis connection: {e}");
13 }
14 }
15 }
16
17 // Reset redis_pool if we cannot grab a connection
18 self.redis_pool = None;
19 self.redis_connect().await;
20
21 let pool = match self.redis_pool.as_ref() {
22 Some(x) => x,
23 None => return None,
24 };
25
26 let conn = match pool.get() {
27 Ok(x) => x,
28 Err(e) => {
29 log::error!("Failed to grab redis connection: {e}");
30 return None;
31 }
32 };
33
34 return Some(conn);
35 }
36
37 pub(super) async fn redis_connect(&mut self) -> () {
38 let db_url = match std::env::var("REDIS_URL") {
39 Ok(url) => url,
40 Err(e) => {
41 panic!(
42 "REDIS_URL environment variable is missing, cannot connect to database: {e}"
43 );
44 }
45 };
46
47 let client = match Client::open(db_url) {
48 Ok(x) => x,
49 Err(e) => {
50 log::error!("Failed to connect to database at REDIS_URL: {e}");
51 log::warn!(
52 "Requests requiring redis will request a connection retry, but may fail."
53 );
54 return;
55 }
56 };
57
58 let pool = match Pool::builder().build(client) {
59 Ok(x) => x,
60 Err(e) => {
61 log::error!("Failed initialize pool of redis connections: {e}");
62 log::warn!(
63 "Requests requiring redis will request a connection retry, but may fail."
64 );
65 return;
66 }
67 };
68
69 self.redis_pool = Some(pool);
70 log::info!("Successfully connected to Redis instance");
71 }
72}