mirror of
https://github.com/shlinkio/shlink.git
synced 2025-03-29 04:52:54 +03:00
Make sure VisitsTracker wraps as little operations as possible in the transaction
This commit is contained in:
parent
c4fd3a74c5
commit
ab96297e58
2 changed files with 8 additions and 5 deletions
|
@ -17,7 +17,7 @@ use Shlinkio\Shlink\IpGeolocation\Model\Location;
|
||||||
use Shlinkio\Shlink\IpGeolocation\Resolver\IpLocationResolverInterface;
|
use Shlinkio\Shlink\IpGeolocation\Resolver\IpLocationResolverInterface;
|
||||||
use Throwable;
|
use Throwable;
|
||||||
|
|
||||||
class LocateVisit
|
readonly class LocateVisit
|
||||||
{
|
{
|
||||||
public function __construct(
|
public function __construct(
|
||||||
private IpLocationResolverInterface $ipLocationResolver,
|
private IpLocationResolverInterface $ipLocationResolver,
|
||||||
|
|
|
@ -71,12 +71,15 @@ readonly class VisitsTracker implements VisitsTrackerInterface
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
$this->em->wrapInTransaction(function () use ($createVisit, $visitor): void {
|
$visit = $createVisit($visitor->normalizeForTrackingOptions($this->options));
|
||||||
$visit = $createVisit($visitor->normalizeForTrackingOptions($this->options));
|
|
||||||
|
// Wrap persisting and flushing the visit in a transaction, so that the ShortUrlVisitsCountTracker performs
|
||||||
|
// changes inside that very same transaction atomically
|
||||||
|
$this->em->wrapInTransaction(function () use ($visit): void {
|
||||||
$this->em->persist($visit);
|
$this->em->persist($visit);
|
||||||
$this->em->flush();
|
$this->em->flush();
|
||||||
|
|
||||||
$this->eventDispatcher->dispatch(new UrlVisited($visit->getId(), $visitor->remoteAddress));
|
|
||||||
});
|
});
|
||||||
|
|
||||||
|
$this->eventDispatcher->dispatch(new UrlVisited($visit->getId(), $visitor->remoteAddress));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue