Git Product home page Git Product logo

atomic-locks-middleware's Introduction

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}');

How Does It Work?

// AtomicLocksMiddleware.php

public function handle(Request $request, Closure $next, string $option = null): Response
{
    $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,
        config('atomic-locks-middleware.lock_seconds')
    );
    app()->instance(config('atomic-locks-middleware.instance'), $lock);
    if ($lock->get()) {
        return $next($request);
    }

    return response()->json([
        'message' => config('atomic-locks-middleware.message'),
    ], 429);
}

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' => 'atomic-locks-middleware',
    'instance' => 'AtomicLocksMiddleware',
    'lock_prefix' => 'atomic_locks_middleware_',
    'lock_seconds' => 60,
    'message' => 'Too Many Attempts',
];

Testing

composer test

atomic-locks-middleware's People

Contributors

pyaesoneaungrgn avatar dependabot[bot] avatar github-actions[bot] avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.