Make sure VisitsTracker wraps as little operations as possible in the transaction

This commit is contained in:
Alejandro Celaya 2024-03-28 17:06:18 +01:00
parent c4fd3a74c5
commit ab96297e58
2 changed files with 8 additions and 5 deletions

View File

@ -17,7 +17,7 @@ use Shlinkio\Shlink\IpGeolocation\Model\Location;
use Shlinkio\Shlink\IpGeolocation\Resolver\IpLocationResolverInterface;
use Throwable;
class LocateVisit
readonly class LocateVisit
{
public function __construct(
private IpLocationResolverInterface $ipLocationResolver,

View File

@ -71,12 +71,15 @@ readonly class VisitsTracker implements VisitsTrackerInterface
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->flush();
$this->eventDispatcher->dispatch(new UrlVisited($visit->getId(), $visitor->remoteAddress));
});
$this->eventDispatcher->dispatch(new UrlVisited($visit->getId(), $visitor->remoteAddress));
}
}