Skip to content

Commit 81c5ba3

Browse files
authored
fix: convert auctions in InMemoryStore to DashMap (#573)
* dash map * wrongful rmv * add todo * bump version
1 parent 9f59f99 commit 81c5ba3

16 files changed

+51
-55
lines changed

Cargo.lock

Lines changed: 19 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

auction-server/Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "auction-server"
3-
version = "0.34.2"
3+
version = "0.34.3"
44
edition = "2021"
55
license-file = "license.txt"
66

@@ -63,6 +63,7 @@ tokio-metrics = { version = "0.4.2", features = ["rt"] }
6363
clickhouse = { version = "0.13.2", features = ["time", "uuid", "native-tls", "inserter"] }
6464
sha2 = "0.10.9"
6565
tokio-tungstenite = { workspace = true, features = ["native-tls"] }
66+
dashmap = "6.1.0"
6667

6768
[dev-dependencies]
6869
mockall = "0.13.1"

auction-server/src/auction/repository/add_auction.rs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,7 @@ use {
66
impl Repository {
77
#[tracing::instrument(skip_all)]
88
async fn add_in_memory_auction(&self, auction: entities::Auction) {
9-
self.in_memory_store
10-
.auctions
11-
.write()
12-
.await
13-
.insert(auction.id, auction);
9+
self.in_memory_store.auctions.insert(auction.id, auction);
1410
}
1511

1612
// NOTE: Do not call this function directly. Instead call `add_auction` from `Service`.

auction-server/src/auction/repository/conclude_auction.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ impl Repository {
1010
pub async fn conclude_auction(&self, auction_id: entities::AuctionId) -> anyhow::Result<()> {
1111
tracing::Span::current().record("auction_id", auction_id.to_string());
1212
self.db.conclude_auction(auction_id).await?;
13-
self.remove_in_memory_auction(auction_id).await;
13+
self.remove_in_memory_auction(auction_id);
1414
Ok(())
1515
}
1616
}

auction-server/src/auction/repository/get_in_memory_auction_by_bid_id.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,11 @@ use {
44
};
55

66
impl Repository {
7-
pub async fn get_in_memory_auction_bid_by_bid_id(
7+
pub fn get_in_memory_auction_bid_by_bid_id(
88
&self,
99
bid_id: entities::BidId,
1010
) -> Option<entities::Bid> {
1111
self.get_in_memory_auctions()
12-
.await
1312
.into_iter()
1413
.find_map(|auction| auction.bids.iter().find(|bid| bid.id == bid_id).cloned())
1514
}

auction-server/src/auction/repository/get_in_memory_auction_by_id.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,13 @@ use {
44
};
55

66
impl Repository {
7-
pub async fn get_in_memory_auction_by_id(
7+
pub fn get_in_memory_auction_by_id(
88
&self,
99
auction_id: entities::AuctionId,
1010
) -> Option<entities::Auction> {
1111
self.in_memory_store
1212
.auctions
13-
.read()
14-
.await
1513
.get(&auction_id)
16-
.cloned()
14+
.map(|auction_ref| auction_ref.clone())
1715
}
1816
}

auction-server/src/auction/repository/get_in_memory_auctions.rs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,11 @@ use {
44
};
55

66
impl Repository {
7-
pub async fn get_in_memory_auctions(&self) -> Vec<entities::Auction> {
7+
pub fn get_in_memory_auctions(&self) -> Vec<entities::Auction> {
88
self.in_memory_store
99
.auctions
10-
.read()
11-
.await
12-
.values()
13-
.cloned()
10+
.iter()
11+
.map(|entry| entry.value().clone())
1412
.collect()
1513
}
1614
}

auction-server/src/auction/repository/mod.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ use {
55
entities::ChainId,
66
},
77
axum_prometheus::metrics,
8+
dashmap::DashMap,
89
solana_sdk::pubkey::Pubkey,
910
std::collections::{
1011
HashMap,
@@ -66,7 +67,7 @@ pub struct ChainStoreEvm {}
6667
#[derive(Debug)]
6768
pub struct InMemoryStore {
6869
pub pending_bids: RwLock<HashMap<PermissionKeySvm, Vec<entities::Bid>>>,
69-
pub auctions: RwLock<HashMap<entities::AuctionId, entities::Auction>>,
70+
pub auctions: DashMap<entities::AuctionId, entities::Auction>,
7071

7172
pub auction_lock: Mutex<HashMap<PermissionKeySvm, entities::AuctionLock>>,
7273
pub bid_lock: Mutex<HashMap<entities::BidId, entities::BidLock>>,
@@ -78,7 +79,7 @@ impl Default for InMemoryStore {
7879
fn default() -> Self {
7980
Self {
8081
pending_bids: RwLock::new(HashMap::new()),
81-
auctions: RwLock::new(HashMap::new()),
82+
auctions: DashMap::new(),
8283
auction_lock: Mutex::new(HashMap::new()),
8384
bid_lock: Mutex::new(HashMap::new()),
8485
chain_store: ChainStoreSvm::default(),
@@ -110,7 +111,7 @@ impl Repository {
110111
pub(super) async fn update_metrics(&self) {
111112
let label = [("chain_id", self.chain_id.to_string())];
112113
let store = &self.in_memory_store;
113-
metrics::gauge!("in_memory_auctions", &label).set(store.auctions.read().await.len() as f64);
114+
metrics::gauge!("in_memory_auctions", &label).set(store.auctions.len() as f64);
114115
metrics::gauge!("in_memory_pending_bids", &label)
115116
.set(store.pending_bids.read().await.len() as f64);
116117
metrics::gauge!("in_memory_auction_locks", &label)

auction-server/src/auction/repository/remove_in_memory_auction.rs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,8 @@ use {
55

66
impl Repository {
77
#[tracing::instrument(skip_all, fields(auction_id))]
8-
pub async fn remove_in_memory_auction(&self, auction_id: entities::AuctionId) {
8+
pub fn remove_in_memory_auction(&self, auction_id: entities::AuctionId) {
99
tracing::Span::current().record("auction_id", auction_id.to_string());
10-
self.in_memory_store
11-
.auctions
12-
.write()
13-
.await
14-
.remove(&auction_id);
10+
self.in_memory_store.auctions.remove(&auction_id);
1511
}
1612
}

auction-server/src/auction/repository/update_bid_status.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,14 @@ use {
1010
impl Repository {
1111
// Find the in memory auction which contains the bid and update the bid status
1212
#[tracing::instrument(skip_all)]
13-
async fn update_in_memory_auction_bid(
13+
fn update_in_memory_auction_bid(
1414
&self,
1515
bid: &entities::Bid,
1616
new_status: entities::BidStatusSvm,
1717
) {
1818
if let Some(auction_id) = new_status.get_auction_id() {
19-
let mut write_guard = self.in_memory_store.auctions.write().await;
20-
if let Some(auction) = write_guard.get_mut(&auction_id) {
19+
if let Some(mut auction_ref) = self.in_memory_store.auctions.get_mut(&auction_id) {
20+
let auction = auction_ref.value_mut();
2121
let bid_index = auction.bids.iter().position(|b| b.id == bid.id);
2222
if let Some(index) = bid_index {
2323
auction.bids[index].status = new_status;
@@ -36,7 +36,7 @@ impl Repository {
3636
self.db.update_bid_status(&bid, &new_status).await?;
3737
if is_updated && !new_status.is_pending() {
3838
self.remove_in_memory_pending_bids(&[bid.clone()]).await;
39-
self.update_in_memory_auction_bid(&bid, new_status).await;
39+
self.update_in_memory_auction_bid(&bid, new_status);
4040
}
4141
Ok((is_updated, conclusion_time_new))
4242
}

0 commit comments

Comments
 (0)