less db transactions
This commit is contained in:
parent
5d5e65ed40
commit
45af241429
@ -316,11 +316,11 @@ pub async fn user_login_post(
|
|||||||
|
|
||||||
txn.commit().await?;
|
txn.commit().await?;
|
||||||
|
|
||||||
let txn = db_conn.begin().await?;
|
|
||||||
|
|
||||||
// First, optionally catch a referral code from the parameters if there is any
|
// First, optionally catch a referral code from the parameters if there is any
|
||||||
trace!(?payload.referral_code);
|
trace!(?payload.referral_code);
|
||||||
if let Some(referral_code) = payload.referral_code.as_ref() {
|
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
|
// If it is not inside, also check in the database
|
||||||
trace!("Using register referral code: {:?}", referral_code);
|
trace!("Using register referral code: {:?}", referral_code);
|
||||||
let user_referrer = referrer::Entity::find()
|
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)),
|
credits_applied_for_referrer: sea_orm::Set(Decimal::new(0, 10)),
|
||||||
..Default::default()
|
..Default::default()
|
||||||
};
|
};
|
||||||
|
|
||||||
used_referral.insert(&txn).await?;
|
used_referral.insert(&txn).await?;
|
||||||
}
|
|
||||||
txn.commit().await?;
|
txn.commit().await?;
|
||||||
|
}
|
||||||
|
|
||||||
(caller, vec![caller_key], StatusCode::CREATED)
|
(caller, vec![caller_key], StatusCode::CREATED)
|
||||||
}
|
}
|
||||||
Some(caller) => {
|
Some(caller) => {
|
||||||
// Let's say that a user that exists can actually also redeem a key in retrospect...
|
// 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 ...
|
// TODO: Move this into a common variable outside ...
|
||||||
// First, optionally catch a referral code from the parameters if there is any
|
// First, optionally catch a referral code from the parameters if there is any
|
||||||
if let Some(referral_code) = payload.referral_code.as_ref() {
|
if let Some(referral_code) = payload.referral_code.as_ref() {
|
||||||
// If it is not inside, also check in the database
|
// 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()
|
let user_referrer = referrer::Entity::find()
|
||||||
.filter(referrer::Column::ReferralCode.eq(referral_code))
|
.filter(referrer::Column::ReferralCode.eq(referral_code))
|
||||||
.one(&txn)
|
.one(&txn)
|
||||||
@ -374,8 +377,9 @@ pub async fn user_login_post(
|
|||||||
..Default::default()
|
..Default::default()
|
||||||
};
|
};
|
||||||
used_referral.insert(&txn).await?;
|
used_referral.insert(&txn).await?;
|
||||||
}
|
|
||||||
txn.commit().await?;
|
txn.commit().await?;
|
||||||
|
}
|
||||||
|
|
||||||
// the user is already registered
|
// the user is already registered
|
||||||
let user_rpc_keys = rpc_key::Entity::find()
|
let user_rpc_keys = rpc_key::Entity::find()
|
||||||
|
@ -2,10 +2,7 @@ use crate::app::Web3ProxyApp;
|
|||||||
use crate::errors::{Web3ProxyError, Web3ProxyErrorContext, Web3ProxyResponse};
|
use crate::errors::{Web3ProxyError, Web3ProxyErrorContext, Web3ProxyResponse};
|
||||||
use crate::premium::grant_premium_tier;
|
use crate::premium::grant_premium_tier;
|
||||||
use anyhow::Context;
|
use anyhow::Context;
|
||||||
use axum::{
|
use axum::{response::IntoResponse, Extension};
|
||||||
response::IntoResponse,
|
|
||||||
Extension,
|
|
||||||
};
|
|
||||||
use axum_macros::debug_handler;
|
use axum_macros::debug_handler;
|
||||||
use entities::{stripe_increase_balance_receipt, user, user_tier};
|
use entities::{stripe_increase_balance_receipt, user, user_tier};
|
||||||
use http::HeaderMap;
|
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
|
// 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
|
// Assert that it's usd
|
||||||
if intent.currency.to_string() != "usd" || recipient.is_none() {
|
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,
|
currency=%intent.currency, %recipient_user_id, %intent.id,
|
||||||
"Please refund this transaction!",
|
"Please refund this transaction!",
|
||||||
);
|
);
|
||||||
let _ = insert_receipt_model.save(&txn).await;
|
let _ = insert_receipt_model.save(db_conn).await;
|
||||||
txn.commit().await?;
|
|
||||||
return Ok("Received Webhook".into_response());
|
return Ok("Received Webhook".into_response());
|
||||||
}
|
}
|
||||||
// Otherwise, also increase the balance ...
|
// Otherwise, also increase the balance ...
|
||||||
match recipient {
|
match recipient {
|
||||||
Some(recipient) => {
|
Some(recipient) => {
|
||||||
|
let txn = db_conn.begin().await?;
|
||||||
|
|
||||||
let _ = insert_receipt_model.save(&txn).await;
|
let _ = insert_receipt_model.save(&txn).await;
|
||||||
|
|
||||||
let user_tier = user_tier::Entity::find_by_id(recipient.user_tier_id)
|
let user_tier = user_tier::Entity::find_by_id(recipient.user_tier_id)
|
||||||
|
@ -259,7 +259,6 @@ pub async fn modify_subuser(
|
|||||||
let db_conn = app.db_conn()?;
|
let db_conn = app.db_conn()?;
|
||||||
let (subuser, _subuser_rpc_keys, _status_code) = match subuser {
|
let (subuser, _subuser_rpc_keys, _status_code) = match subuser {
|
||||||
None => {
|
None => {
|
||||||
let txn = db_conn.begin().await?;
|
|
||||||
// First add a user; the only thing we need from them is an address
|
// First add a user; the only thing we need from them is an address
|
||||||
// everything else is optional
|
// everything else is optional
|
||||||
let subuser = user::ActiveModel {
|
let subuser = user::ActiveModel {
|
||||||
@ -267,6 +266,8 @@ pub async fn modify_subuser(
|
|||||||
..Default::default()
|
..Default::default()
|
||||||
};
|
};
|
||||||
|
|
||||||
|
let txn = db_conn.begin().await?;
|
||||||
|
|
||||||
let subuser = subuser.insert(&txn).await?;
|
let subuser = subuser.insert(&txn).await?;
|
||||||
|
|
||||||
// create the user's first api key
|
// create the user's first api key
|
||||||
@ -323,7 +324,6 @@ pub async fn modify_subuser(
|
|||||||
.await
|
.await
|
||||||
.web3_context("failed using the db to check for a subuser")?;
|
.web3_context("failed using the db to check for a subuser")?;
|
||||||
|
|
||||||
let txn = db_conn.begin().await?;
|
|
||||||
let mut action = "no action";
|
let mut action = "no action";
|
||||||
|
|
||||||
match subuser_entry_secondary_user {
|
match subuser_entry_secondary_user {
|
||||||
@ -348,7 +348,7 @@ pub async fn modify_subuser(
|
|||||||
role: sea_orm::Set(new_role.clone()),
|
role: sea_orm::Set(new_role.clone()),
|
||||||
..Default::default()
|
..Default::default()
|
||||||
};
|
};
|
||||||
active_subuser_entry_secondary_user.insert(&txn).await?;
|
active_subuser_entry_secondary_user.insert(db_conn).await?;
|
||||||
action = "added";
|
action = "added";
|
||||||
}
|
}
|
||||||
_ => {
|
_ => {
|
||||||
@ -360,7 +360,6 @@ pub async fn modify_subuser(
|
|||||||
// Do nothing in this case
|
// Do nothing in this case
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
txn.commit().await?;
|
|
||||||
|
|
||||||
let response = (
|
let response = (
|
||||||
StatusCode::OK,
|
StatusCode::OK,
|
||||||
|
@ -354,11 +354,11 @@ impl BufferedRpcQueryStats {
|
|||||||
|
|
||||||
// Apply all the referral logic; let's keep it simple and flat for now
|
// Apply all the referral logic; let's keep it simple and flat for now
|
||||||
if self.paid_credits_used > 0.into() {
|
if self.paid_credits_used > 0.into() {
|
||||||
|
let mut invalidate_caches = false;
|
||||||
|
|
||||||
// Start a transaction
|
// Start a transaction
|
||||||
let txn = db_conn.begin().await?;
|
let txn = db_conn.begin().await?;
|
||||||
|
|
||||||
let mut invalidate_caches = false;
|
|
||||||
|
|
||||||
// Calculate if we are above the usage threshold, and apply a bonus
|
// 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)
|
// 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
|
// 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
|
||||||
|
@ -10,8 +10,6 @@ use tokio::{
|
|||||||
use tracing::{info, trace, warn};
|
use tracing::{info, trace, warn};
|
||||||
use web3_proxy::relational_db::get_migrated_db;
|
use web3_proxy::relational_db::get_migrated_db;
|
||||||
|
|
||||||
use migration::sea_orm::prelude;
|
|
||||||
|
|
||||||
/// on drop, the mysql docker container will be shut down
|
/// on drop, the mysql docker container will be shut down
|
||||||
pub struct TestMysql {
|
pub struct TestMysql {
|
||||||
pub url: Option<String>,
|
pub url: Option<String>,
|
||||||
|
Loading…
Reference in New Issue
Block a user