New

Just launched: Vueblocks, a brand-new search engine for Vue packages! Explore Vueblocks →

pyaesoneaung/atomic-locks-middleware

A package designed to ensure that only one request is processed at a time.
1,002 34

Larablocks Analysis

pyaesoneaung/atomic-locks-middleware works with Laravel 9 to 12 (latest release v1.3.0, May 31, 2025).

Recommendation checks

Use with care

Use with care: meets 0 of 4 checks.

  • Latest release doesn't support Laravel 13, the current version
  • No release in the last 12 months (last: May 31, 2025)
  • 480 downloads a month (needs 10,000)
  • 34 GitHub stars (needs 100)

State

Quiet
  • Last stable release: May 31, 2025.
  • No releases in the last 12 months.
  • First released: Aug 24, 2023.
  • Monthly downloads: 480.
  • 4 open issues on GitHub.

Data from Packagist and GitHub, checked by Larablocks on Nov 30, 2026.

Install
composer require pyaesoneaung/atomic-locks-middleware
Latest Version:v1.3.0
PHP:^8.1
Laravel:9 – 12
License:MIT
Last Updated:Apr 20, 2026
Links: GitHub  ·  Packagist
Maintainer: pyaesoneaung

Atomic Locks Middleware

A package designed to ensure that only one request is processed at a time.

Installation

composer require pyaesoneaung/atomic-locks-middleware

Usage

By default, the atomic-locks-middleware uses $request->user()?->id ?: $request->ip() within atomic locks.

Route::post('/order', function () {
    // ...
})->middleware('atomic-locks-middleware');

If you prefer to implement IP-based locking, you can use atomic-locks-middleware:ip.

Route::post('/order', function () {
    // ...
})->middleware('atomic-locks-middleware:ip');

However, you have the flexibility to define atomic-locks-middleware:{anything} to customize the locking mechanism according to your preferences.

Route::post('/order', function () {
    // ...
})->middleware('atomic-locks-middleware:{anything}');

You can also pass additional parameters to the middleware for more customization. The available parameters are:

  • {anything} (string) : Your custom locking mechanism.
  • {lockDuration} (int) : Duration for which the lock will be held.
  • {canBlock} (bool) : Whether the request can wait for the lock or not.
  • {blockDuration} (int) : If waiting is allowed, the maximum duration to wait for the lock.

If no additional parameters are provided, the default values from the config file will be used.

Route::post('/order', function () {
    // ...
})->middleware('atomic-locks-middleware:{anything}');


Route::post('/purchase', function () {
    // ...
})->middleware('atomic-locks-middleware:{anything},60,true,60');


Route::post('/payment/process', function () {
    // ...
})->middleware('atomic-locks-middleware:{anything},60,false');

How Does It Work?

// AtomicLocksMiddleware.php

 public function handle(Request $request, Closure $next, string $option = null, int $lockDuration = null, string $canBlock = null, int $blockDuration = null): Response
{
    if (! empty($canBlock)) {
        $canBlock = filter_var($canBlock, FILTER_VALIDATE_BOOLEAN);
    }

    $name = match ($option) {
        null => $request->user()?->id ?: $request->ip(),
        'ip' => $request->ip(),
        default => $option
    };

    $name = "{$request->path()}_{$name}";

    $lock = Cache::lock(
        config('atomic-locks-middleware.lock_prefix') . $name,
        $lockDuration ?: config('atomic-locks-middleware.default_lock_duration')
    );

    if (! $lock->get()) {
        if (! ($canBlock ?? config('atomic-locks-middleware.can_block'))) {
            return response()->json([
                'message' => config('atomic-locks-middleware.message'),
            ], 429);
        }

        try {
            $lock->block($blockDuration ?: config('atomic-locks-middleware.default_block_duration'));
        } catch (LockTimeoutException) {
            $lock->release();

            return response()->json([
                'message' => config('atomic-locks-middleware.block_timeout_error_message'),
            ], 500);
        } catch (Throwable $th) {
            $lock->release();

            return response()->json([
                'message' => $th->getMessage(),
            ], 500);
        }
    }

    app()->instance(config('atomic-locks-middleware.instance'), $lock);

    return $next($request);
}

/**
 * Handle tasks after the response has been sent to the browser.
 */
public function terminate(Request $request, Response $response): void
{
    $instanceName = config('atomic-locks-middleware.instance');

    if (app()->bound($instanceName)) {
        app($instanceName)->release();
    }
}

The Atomic Locks Middleware uses Laravel Atomic Locks in the background. It initiates a lock at the beginning of the middleware's execution and releases the lock once the response is dispatched to the browser.

Publish Configuration

Publish the configuration for customization

php artisan vendor:publish --provider="PyaeSoneAung\AtomicLocksMiddleware\AtomicLocksMiddlewareServiceProvider"
return [

    'middleware_name' => 'atomic-locks-middleware',
    'middleware_class' => PyaeSoneAung\AtomicLocksMiddleware\AtomicLocksMiddleware::class,

    'instance' => 'AtomicLocksMiddleware',

    'lock_prefix' => 'atomic_locks_middleware_',
    'default_lock_duration' => 60,

    'can_block' => false,
    'default_block_duration' => 60, // It's generally recommended to set the block duration to be longer than the lock duration.
    'block_timeout_error_message' => 'Timeout: Unable to acquire lock within the specified time.',

    'message' => 'Too Many Attempts',
];

Testing

composer test

Related Packages

shin1x1/laravel-force-https-url-scheme

Force https url schema middleware for Laravel 5

26,582 25
laravel-dev-common/union-middleware

Laravel dev middleware util

111 0
fsy-laravel/middleware-checklogin

Laravel dev mid_checklogin util

71 0
laravel-dev-common/union-middleware-checklogin

Laravel dev mid_checklogin util

20 0
bhavikshah/page-title-middleware

Laravel 5.4 middleware package for Page Title

13 0

Version History

Version Released PHP Laravel License
v1.3.0 ^8.1 ^9.0|^10.0|^11.0|^12.0 MIT
v1.2.0 ^8.1 ^9.0|^10.0|^11.0 MIT
v1.1.1 ^8.1 ^9.0|^10.0 MIT
v1.1.0 ^8.1 ^9.0|^10.0 MIT
v1.0.3 ^8.1 ^9.0|^10.0 MIT