Skip to content

Commit

Permalink
refactor: Move leveldb as unstable feature
Browse files Browse the repository at this point in the history
* issue: dermesser/leveldb-rs#6

Signed-off-by: KunoiSayami <i@leanhe.dev>
  • Loading branch information
KunoiSayami committed Aug 22, 2023
1 parent 93bcaed commit 6e97bfc
Show file tree
Hide file tree
Showing 8 changed files with 220 additions and 143 deletions.
118 changes: 53 additions & 65 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 4 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "teamspeak-management-tools"
version = "4.0.2"
version = "4.0.3"
edition = "2021"

[dependencies]
Expand All @@ -15,7 +15,7 @@ kstool = "0.2.1"
log = { version = "0.4", features = ["max_level_trace", "release_max_level_debug"] }
once_cell = "^1.10"
redis = { version = "0.23", features = ["tokio-comp"] }
rusty-leveldb = { version = "2.0.0", features = ["async"] }
rusty-leveldb = { version = "2.0.0", features = ["async"], optional = true }
serde = "1"
serde-teamspeak-querystring = "0.2.1"
serde_derive = "1"
Expand All @@ -36,5 +36,6 @@ panic = "abort"

[features]
default = []
all = ["tracker"]
all = ["tracker", "leveldb"]
tracker = ["sqlx"]
leveldb = ["rusty-leveldb"]
27 changes: 16 additions & 11 deletions src/auto_channel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ use crate::socketlib::SocketConn;
use crate::{AUTO_CHANNEL_NICKNAME_OVERRIDE, DEFAULT_AUTO_CHANNEL_NICKNAME};
use anyhow::anyhow;
use log::{debug, error, info, trace, warn};
use redis::AsyncCommands;
use std::time::Duration;
use tap::{Tap, TapFallible};
use tokio::sync::mpsc;
Expand Down Expand Up @@ -138,12 +137,11 @@ pub async fn auto_channel_staff(
config: Config,
thread_id: String,
) -> anyhow::Result<()> {
let redis = redis::Client::open(config.server().redis_server())
.map_err(|e| anyhow!("Connect redis server error! {:?}", e))?;
let mut redis_conn = redis
.get_async_connection()
let mut kv_map = config
.server()
.get_kv_map()
.await
.map_err(|e| anyhow!("Get redis connection error: {:?}", e))?;
.map_err(|e| anyhow!("Got error while create KV map: {:?}", e))?;

let monitor_channels = config.server().channels();
let privilege_group = config.server().privilege_group_id();
Expand Down Expand Up @@ -192,8 +190,8 @@ pub async fn auto_channel_staff(
*channel_id,
);

redis_conn
.del::<_, i64>(&key)
kv_map
.delete(&key)
.await
.tap(|_| trace!("[{}] Deleted", thread_id))
.tap_err(|e| {
Expand Down Expand Up @@ -257,7 +255,14 @@ pub async fn auto_channel_staff(
pid = client.channel_id()
);

let ret: Option<i64> = redis_conn.get(&key).await?;
let ret: Option<i64> = kv_map
.get(&key)
.await?
.map(|v| v.parse())
.transpose()
.tap_err(|e| error!("[{}] Unable to parse result: {:?}", thread_id, e))
.ok()
.flatten();
let create_new = ret.is_none();
let target_channel = if create_new {
let mut name = format!("{}'s channel", client.client_nickname());
Expand Down Expand Up @@ -326,7 +331,7 @@ pub async fn auto_channel_staff(
Ok(ret) => ret,
Err(e) => {
if e.code() == 768 {
redis_conn.del(&key).await?;
kv_map.delete(&key).await?;
skip_sleep = true;
continue;
}
Expand All @@ -348,7 +353,7 @@ pub async fn auto_channel_staff(
conn.move_client(who_am_i.client_id(), client.channel_id())
.await
.map_err(|e| anyhow!("Unable move self out of channel. {:?}", e))?;
redis_conn.set(&key, target_channel).await?;
kv_map.set(&key, target_channel).await?;
}

info!("Move {} to {}", client.client_nickname(), target_channel);
Expand Down
Loading

0 comments on commit 6e97bfc

Please sign in to comment.