less db transactions

This commit is contained in:
Bryan Stitt 2023-07-13 15:44:00 -07:00
parent 5d5e65ed40
commit 45af241429
5 changed files with 20 additions and 21 deletions

View File

@ -316,11 +316,11 @@ pub async fn user_login_post(
txn.commit().await?;
let txn = db_conn.begin().await?;
// First, optionally catch a referral code from the parameters if there is any
trace!(?payload.referral_code);
if let Some(referral_code) = payload.referral_code.as_ref() {
let txn = db_conn.begin().await?;
// If it is not inside, also check in the database
trace!("Using register referral code: {:?}", referral_code);
let user_referrer = referrer::Entity::find()
@ -340,20 +340,23 @@ pub async fn user_login_post(
credits_applied_for_referrer: sea_orm::Set(Decimal::new(0, 10)),
..Default::default()
};
used_referral.insert(&txn).await?;
txn.commit().await?;
}
txn.commit().await?;
(caller, vec![caller_key], StatusCode::CREATED)
}
Some(caller) => {
// Let's say that a user that exists can actually also redeem a key in retrospect...
let txn = db_conn.begin().await?;
// TODO: Move this into a common variable outside ...
// First, optionally catch a referral code from the parameters if there is any
if let Some(referral_code) = payload.referral_code.as_ref() {
// If it is not inside, also check in the database
trace!("Using referral code: {:?}", referral_code);
let txn = db_conn.begin().await?;
trace!(?referral_code, "Using");
let user_referrer = referrer::Entity::find()
.filter(referrer::Column::ReferralCode.eq(referral_code))
.one(&txn)
@ -374,8 +377,9 @@ pub async fn user_login_post(
..Default::default()
};
used_referral.insert(&txn).await?;
txn.commit().await?;
}
txn.commit().await?;
// the user is already registered
let user_rpc_keys = rpc_key::Entity::find()

View File

@ -2,10 +2,7 @@ use crate::app::Web3ProxyApp;
use crate::errors::{Web3ProxyError, Web3ProxyErrorContext, Web3ProxyResponse};
use crate::premium::grant_premium_tier;
use anyhow::Context;
use axum::{
response::IntoResponse,
Extension,
};
use axum::{response::IntoResponse, Extension};
use axum_macros::debug_handler;
use entities::{stripe_increase_balance_receipt, user, user_tier};
use http::HeaderMap;
@ -114,7 +111,6 @@ pub async fn user_balance_stripe_post(
};
// In all these cases, we should record the transaction, but not increase the balance
let txn = db_conn.begin().await?;
// Assert that it's usd
if intent.currency.to_string() != "usd" || recipient.is_none() {
@ -125,13 +121,15 @@ pub async fn user_balance_stripe_post(
currency=%intent.currency, %recipient_user_id, %intent.id,
"Please refund this transaction!",
);
let _ = insert_receipt_model.save(&txn).await;
txn.commit().await?;
let _ = insert_receipt_model.save(db_conn).await;
return Ok("Received Webhook".into_response());
}
// Otherwise, also increase the balance ...
match recipient {
Some(recipient) => {
let txn = db_conn.begin().await?;
let _ = insert_receipt_model.save(&txn).await;
let user_tier = user_tier::Entity::find_by_id(recipient.user_tier_id)

View File

@ -259,7 +259,6 @@ pub async fn modify_subuser(
let db_conn = app.db_conn()?;
let (subuser, _subuser_rpc_keys, _status_code) = match subuser {
None => {
let txn = db_conn.begin().await?;
// First add a user; the only thing we need from them is an address
// everything else is optional
let subuser = user::ActiveModel {
@ -267,6 +266,8 @@ pub async fn modify_subuser(
..Default::default()
};
let txn = db_conn.begin().await?;
let subuser = subuser.insert(&txn).await?;
// create the user's first api key
@ -323,7 +324,6 @@ pub async fn modify_subuser(
.await
.web3_context("failed using the db to check for a subuser")?;
let txn = db_conn.begin().await?;
let mut action = "no action";
match subuser_entry_secondary_user {
@ -348,7 +348,7 @@ pub async fn modify_subuser(
role: sea_orm::Set(new_role.clone()),
..Default::default()
};
active_subuser_entry_secondary_user.insert(&txn).await?;
active_subuser_entry_secondary_user.insert(db_conn).await?;
action = "added";
}
_ => {
@ -360,7 +360,6 @@ pub async fn modify_subuser(
// Do nothing in this case
}
};
txn.commit().await?;
let response = (
StatusCode::OK,

View File

@ -354,11 +354,11 @@ impl BufferedRpcQueryStats {
// Apply all the referral logic; let's keep it simple and flat for now
if self.paid_credits_used > 0.into() {
let mut invalidate_caches = false;
// Start a transaction
let txn = db_conn.begin().await?;
let mut invalidate_caches = false;
// Calculate if we are above the usage threshold, and apply a bonus
// Optimally we would read this from the balance, but if we do it like this, we only have to lock a single table (much safer w.r.t. deadlocks)
// referral_entity.credits_applied_for_referrer * (Decimal::from(10) checks (atomically using this table only), whether the user has brought in >$100 to the referer

View File

@ -10,8 +10,6 @@ use tokio::{
use tracing::{info, trace, warn};
use web3_proxy::relational_db::get_migrated_db;
use migration::sea_orm::prelude;
/// on drop, the mysql docker container will be shut down
pub struct TestMysql {
pub url: Option<String>,