Skip to content

Commit 932c5bd

Browse files
committed
update docs
1 parent caf38b7 commit 932c5bd

File tree

3 files changed

+81
-1
lines changed

3 files changed

+81
-1
lines changed

docs/_sidebar.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,12 @@
2828
- [Refund](refund)
2929
- [Gift](gift)
3030
- [Cart](cart)
31-
31+
32+
- Transactions
33+
34+
- [Transaction](transaction)
35+
- [Race condition](race-condition)
36+
3237
- Additions
3338

3439
- [Wallet Swap](laravel-wallet-swap)

docs/race-condition.md

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
## Race Condition
2+
3+
A common issue in the issue is about race conditions.
4+
5+
If you have not yet imported the config into the project, then you need to do this.
6+
```bash
7+
php artisan vendor:publish --tag=laravel-wallet-config
8+
```
9+
10+
Previously, there was a vacuum package, but now it is a part of the core. You just need to configure the lock service and the cache service in the package configuration `wallet.php`.
11+
12+
```php
13+
/**
14+
* A system for dealing with race conditions.
15+
*/
16+
'lock' => [
17+
'driver' => 'array',
18+
'seconds' => 1,
19+
],
20+
```
21+
22+
To enable the fight against race conditions, you need to select a provider that supports work with locks. I recommend `redis`.
23+
24+
There is a setting for storing the state of the wallet, I recommend choosing `redis` here too.
25+
26+
```php
27+
28+
/**
29+
* Storage of the state of the balance of wallets.
30+
*/
31+
'cache' => ['driver' => 'array'],
32+
```
33+
34+
You need `redis-server` and `php-redis`.
35+
36+
Redis is recommended but not required. You can choose whatever the [framework](https://laravel.com/docs/8.x/cache#introduction) offers you.
37+
38+
It worked!

docs/transaction.md

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
## Transaction
2+
3+
Sometimes you need to execute many simple queries. You want to keep the data atomic. To do this, you need `laravel-wallet` v7.1+.
4+
5+
It is necessary to write off the amount from the balance and raise the ad in the search. What happens if the service for raising an ad fails? We wrote off the money, but did not raise the ad. Received reputational losses. We can imagine the opposite situation, we first raise the ad in the search, but it does not work to write off the money. There are not enough funds. This functionality will help to solve all this. We monitor ONLY the state of the wallet, the rest falls on the developer. Let's take an unsuccessful lift, for example.
6+
7+
```php
8+
use Bavix\Wallet\Internal\Service\DatabaseServiceInterface;
9+
10+
/** @var object $businessLogicService */
11+
/** @var \Bavix\Wallet\Models\Wallet $payer */
12+
$payer->balanceInt; // 9999
13+
app(DatabaseServiceInterface::class)->transaction(statuc function () use ($payer) {
14+
$payer->withdraw(1000); // 8999
15+
$businessLogicService->doingMagic($payer); // throws an exception
16+
}); // rollback payer balance
17+
18+
$payer->balanceInt; // 9999
19+
```
20+
21+
Now let's look at the successful raising of the ad.
22+
23+
```php
24+
use Bavix\Wallet\Internal\Service\DatabaseServiceInterface;
25+
26+
/** @var object $businessLogicService */
27+
/** @var \Bavix\Wallet\Models\Wallet $payer */
28+
$payer->balanceInt; // 9999
29+
app(DatabaseServiceInterface::class)->transaction(statuc function () use ($payer) {
30+
$payer->withdraw(1000); // 8999
31+
$businessLogicService->doingMagic($payer); // successfully
32+
}); // commit payer balance
33+
34+
$payer->balanceInt; // 8999
35+
```
36+
37+
It worked!

0 commit comments

Comments
 (0)