Skip to content

Commit

Permalink
Merge pull request #3 from fakeshadow/atomic
Browse files Browse the repository at this point in the history
Remove usage of Mutex.
  • Loading branch information
fakeshadow authored Jun 27, 2020
2 parents a818155 + ec9df5c commit 2b65b90
Show file tree
Hide file tree
Showing 11 changed files with 537 additions and 138 deletions.
6 changes: 5 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "tang-rs"
version = "0.1.3"
version = "0.2.0"
authors = ["fakeshadow <24548779@qq.com>"]
edition = "2018"
description = "A light weight asynchronous connection pool"
Expand Down Expand Up @@ -38,6 +38,10 @@ features = ["attributes", "unstable"]
[dev-dependencies.smol]
version = "0.1.10"

[dev-dependencies.tokio]
version = "0.2.21"
features = ["full"]

[profile.bench]
opt-level = 3
debug = false
Expand Down
4 changes: 0 additions & 4 deletions examples/rocket_example/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,6 @@ version = "1.0.101"
[dependencies.serde_json]
version = "1.0.40"

[dependencies.tokio]
version = "0.2.16"
features = ["full"]

[dependencies.tokio-postgres]
version = "0.5.3"

Expand Down
4 changes: 2 additions & 2 deletions examples/rocket_example/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ const IDS: &[u32] = &[
1, 11, 9, 20, 3, 5, 2, 6, 19, 8, 9, 10, 12, 13, 14, 15, 16, 17, 18, 4,
];

#[tokio::main]
#[rocket::main]
async fn main() {
let db_url = "postgres://postgres:123@localhost/test";

Expand Down Expand Up @@ -71,7 +71,7 @@ async fn main() {
.mount("/test", routes![index, index2])
.manage(pool)
.manage(pool_redis)
.serve()
.launch()
.await;
}

Expand Down
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@
//! let _handler = task::spawn(fut);
//! }
//!
//! // Boilerplate implement for runtime specific timeout future.
//! fn timeout<Fut: Future>(&self,fut: Fut, dur: Duration) -> ManagerTimeout<Fut, Self::Timeout> {
//! ManagerTimeout::new(fut, Timer::after(dur))
//! }
Expand Down
14 changes: 7 additions & 7 deletions src/pool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ impl<M: Manager> ManagedPool<M> {
fn drop_conn(&self, marker: usize, should_spawn_new: bool) -> Option<usize> {
// We might need to spin up more connections to maintain the idle limit.
// e.g. if we hit connection lifetime limits
self.pool_lock.decr_spawned(marker, should_spawn_new)
self.pool_lock.dec_spawned(marker, should_spawn_new)
}

pub(crate) async fn add_idle_conn(&self, marker: usize) -> Result<(), M::Error> {
Expand All @@ -144,16 +144,16 @@ impl<M: Manager> ManagedPool<M> {
.timeout(fut, timeout)
.await
.map_err(|e| {
self.pool_lock.decr_pending(1);
self.pool_lock.dec_pending(1);
e
})?
.map_err(|e| {
self.pool_lock.decr_pending(1);
self.pool_lock.dec_pending(1);
e
})?;

self.pool_lock
.put_back_incr_spawned(IdleConn::new(conn, marker));
.put_back_inc_spawned(IdleConn::new(conn, marker));

Ok(())
}
Expand All @@ -179,7 +179,7 @@ impl<M: Manager> ManagedPool<M> {
// (the pending of i is already dropped in add_connection method)
let count = pending_count - i - 1;
if count > 0 {
self.pool_lock.decr_pending(count);
self.pool_lock.dec_pending(count);
};
e
})?;
Expand Down Expand Up @@ -215,7 +215,7 @@ impl<M: Manager> ManagedPool<M> {
pub async fn reap_idle_conn(&self) -> Result<(), M::Error> {
let now = Instant::now();

let pending_new = self.pool_lock.try_drop_conns(|conn| {
let pending_new = self.pool_lock.try_drop_conn(|conn| {
let mut should_drop = false;
if let Some(timeout) = self.builder.idle_timeout {
should_drop |= now >= conn.idle_start + timeout;
Expand All @@ -234,7 +234,7 @@ impl<M: Manager> ManagedPool<M> {

pub fn garbage_collect(&self) {
self.pool_lock
.drop_pendings(|pending| pending.should_remove(self.builder.connection_timeout));
.drop_pending(|pending| pending.should_remove(self.builder.connection_timeout));
}

/// expose `Builder` to public
Expand Down
Loading

0 comments on commit 2b65b90

Please sign in to comment.