Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 14 additions & 6 deletions src/Adapters/Laravel/Repositories/DatabaseAnalyticsRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,15 +56,23 @@ public function increment(string $name, EventType $event): void
return;
}

if (DB::table('pan_analytics')->where('name', $name)->count() === 0) {
if (DB::table('pan_analytics')->count() < $maxAnalytics) {
DB::table('pan_analytics')->insert(['name' => $name, $event->column() => 1]);
}
DB::transaction(function () use ($name, $event, $maxAnalytics) {
// Lock the table for this name to prevent race conditions
$existing = DB::table('pan_analytics')
->where('name', $name)
->lockForUpdate()
->first();

return;
}
if ($existing === null) {
// Check total count with lock to prevent race condition on max analytics
if (DB::table('pan_analytics')->lockForUpdate()->count() < $maxAnalytics) {
DB::table('pan_analytics')->insert(['name' => $name, $event->column() => 1]);
}
return;
}

DB::table('pan_analytics')->where('name', $name)->increment($event->column());
});
}

/**
Expand Down