cockroachdb / cockroachdb/example-app-rust-postgres

Question - Tokio Postgres (Async)

Đang mở
#6 0 bình luận 0 reaction 0 người được giao Xem trên GitHub
Ngôn ngữ chính
Rust
Star
6
Fork
1
Chỉ số merge pull request
Không có pull request nào được merge trong 30 ngày

Mô tả

Hello, i have been trying to replicate the example but using Tokio-Postgres. I would like to know if my implementation is correct.

I'm using **deadpool_postgres** too.

```rust
#[derive(Clone)]
pub struct TransactionRepository {
pool: Pool,
}

impl TransactionRepository {
pub fn new(pool: Pool) -> Self {
TransactionRepository { pool }
}
}

#[async_trait]
impl TransactionRepositoryInterface for &TransactionRepository {
async fn make_transaction(&self, data: &WalletTransferEntity) -> Result<(), AppError> {
let mut client: Client = self.pool.get().await.map_err(|e| AppError::DatasourceError(e.to_string()))?;
let mut txn = client.transaction().await?;
let mut i = 0;
match loop {
if i == 3 {
break Ok("Max number of retries".to_string());
}
i = i + 1;
// Set a retry savepoint
// See https://www.cockroachlabs.com/docs/stable/advanced-client-side-transaction-retries
let mut sp = txn.savepoint("cockroach_restart").await?;
match self.transfer(&mut sp, &data).await {
Ok(e) => {
if e.is_empty() {
sp.commit().await?;
break Ok("".to_string());
} else {
break Ok(e);
}
}
Err(ref err)
if err
.code()
.map(|e| *e == SqlState::T_R_SERIALIZATION_FAILURE)
.unwrap_or(false) => {
println!("T_R_SERIALIZATION_FAILURE {:?}", err);
}
r => break r,
}
} {
Ok(t) => {
if t.is_empty() {
txn.commit().await.map_err(|e| AppError::DatasourceError(format!("Error on commit {}", e)))
} else {
Err(AppError::ValidationError(t))
}
}
Err(e) => Err(AppError::DatasourceError(e.to_string()))
}
}

async fn transfer(&self, txn: &mut Transaction, data: &WalletTransferEntity) -> Result {
let from_balance: Decimal = txn.query_one(r#"SELECT balance FROM wallet_accounts WHERE user_id = $1"#, &[&data.from_user_id]).await?.get(0);

if from_balance < data.amount {
return Ok(
"No Enough balance".to_string()
);
}

// Perform the transfer.
txn.execute(
"UPDATE wallet_accounts SET balance = balance - $1 WHERE user_id = $2",
&[&data.amount, &data.from_user_id],
).await?;
txn.execute(
"UPDATE wallet_accounts SET balance = balance + $1 WHERE user_id = $2",
&[&data.amount, &data.to_user_id],
).await?;
Ok("".to_string())
}
}
```

Thanks

Hướng dẫn đóng góp

Chưa lập chỉ mục được hướng dẫn đóng góp cho kho mã nguồn này

Đánh giá

Issue này chưa được đánh giá.

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.