├── tests ├── laravel │ ├── public │ │ ├── favicon.ico │ │ ├── robots.txt │ │ ├── .htaccess │ │ └── index.php │ ├── app │ │ ├── Listeners │ │ │ └── .gitkeep │ │ ├── Policies │ │ │ └── .gitkeep │ │ ├── Events │ │ │ └── Event.php │ │ ├── Http │ │ │ ├── Requests │ │ │ │ └── Request.php │ │ │ ├── Middleware │ │ │ │ ├── EncryptCookies.php │ │ │ │ ├── VerifyCsrfToken.php │ │ │ │ ├── RedirectIfAuthenticated.php │ │ │ │ └── Authenticate.php │ │ │ ├── Controllers │ │ │ │ ├── Controller.php │ │ │ │ └── Auth │ │ │ │ │ ├── PasswordController.php │ │ │ │ │ └── AuthController.php │ │ │ ├── routes.php │ │ │ └── Kernel.php │ │ ├── Providers │ │ │ ├── AppServiceProvider.php │ │ │ ├── AuthServiceProvider.php │ │ │ ├── EventServiceProvider.php │ │ │ └── RouteServiceProvider.php │ │ ├── Jobs │ │ │ └── Job.php │ │ ├── Console │ │ │ ├── Commands │ │ │ │ └── Inspire.php │ │ │ └── Kernel.php │ │ ├── User.php │ │ └── Exceptions │ │ │ └── Handler.php │ ├── database │ │ ├── seeds │ │ │ ├── .gitkeep │ │ │ └── DatabaseSeeder.php │ │ ├── migrations │ │ │ ├── .gitkeep │ │ │ ├── 2014_10_12_100000_create_password_resets_table.php │ │ │ └── 2014_10_12_000000_create_users_table.php │ │ ├── .gitignore │ │ └── factories │ │ │ └── ModelFactory.php │ ├── resources │ │ ├── views │ │ │ ├── vendor │ │ │ │ └── .gitkeep │ │ │ ├── welcome.blade.php │ │ │ └── errors │ │ │ │ └── 503.blade.php │ │ ├── model_views │ │ │ └── DummyModel │ │ │ │ ├── templateABC.blade.php │ │ │ │ ├── templateXYZ.blade.php │ │ │ │ └── templateDEF.blade.php │ │ ├── assets │ │ │ └── sass │ │ │ │ └── app.scss │ │ └── lang │ │ │ └── en │ │ │ ├── pagination.php │ │ │ ├── auth.php │ │ │ ├── passwords.php │ │ │ └── validation.php │ ├── storage │ │ ├── app │ │ │ └── .gitignore │ │ ├── logs │ │ │ └── .gitignore │ │ └── framework │ │ │ ├── cache │ │ │ └── .gitignore │ │ │ ├── sessions │ │ │ └── .gitignore │ │ │ ├── views │ │ │ └── .gitignore │ │ │ └── .gitignore │ ├── bootstrap │ │ ├── cache │ │ │ └── .gitignore │ │ ├── autoload.php │ │ └── app.php │ ├── .gitattributes │ ├── .gitignore │ ├── phpspec.yml │ ├── .env.example │ ├── gulpfile.js │ ├── tests │ │ └── TestCase.php │ ├── server.php │ ├── phpunit.xml │ ├── config │ │ ├── compile.php │ │ ├── services.php │ │ ├── view.php │ │ ├── broadcasting.php │ │ ├── cache.php │ │ ├── auth.php │ │ ├── filesystems.php │ │ ├── queue.php │ │ ├── database.php │ │ ├── mail.php │ │ ├── session.php │ │ └── app.php │ └── artisan ├── TestCase.php ├── RedLockFacadeTest.php ├── fixtures │ ├── QueueWithoutOverlapJob.php │ └── QueueWithoutOverlapJobDefaultLockTime.php ├── QueueWithoutOverlapTest.php └── RedLockTest.php ├── .gitignore ├── src ├── Exceptions │ ├── ClosureRefreshException.php │ └── QueueWithoutOverlapRefreshException.php ├── Facades │ └── RedLock.php ├── RedLockServiceProvider.php ├── Traits │ └── QueueWithoutOverlap.php └── RedLock.php ├── composer.json ├── phpunit.xml └── README.md /tests/laravel/public/favicon.ico: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /tests/laravel/app/Listeners/.gitkeep: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /tests/laravel/app/Policies/.gitkeep: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /tests/laravel/database/seeds/.gitkeep: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /tests/laravel/database/migrations/.gitkeep: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /tests/laravel/database/.gitignore: -------------------------------------------------------------------------------- 1 | *.sqlite 2 | -------------------------------------------------------------------------------- /tests/laravel/resources/views/vendor/.gitkeep: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /tests/laravel/storage/app/.gitignore: -------------------------------------------------------------------------------- 1 | * 2 | !.gitignore -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | sftp-config.json 2 | vendor 3 | composer.lock 4 | -------------------------------------------------------------------------------- /tests/laravel/bootstrap/cache/.gitignore: -------------------------------------------------------------------------------- 1 | * 2 | !.gitignore 3 | -------------------------------------------------------------------------------- /tests/laravel/public/robots.txt: -------------------------------------------------------------------------------- 1 | User-agent: * 2 | Disallow: 3 | -------------------------------------------------------------------------------- /tests/laravel/storage/logs/.gitignore: -------------------------------------------------------------------------------- 1 | * 2 | !.gitignore 3 | -------------------------------------------------------------------------------- /tests/laravel/storage/framework/cache/.gitignore: -------------------------------------------------------------------------------- 1 | * 2 | !.gitignore -------------------------------------------------------------------------------- /tests/laravel/storage/framework/sessions/.gitignore: -------------------------------------------------------------------------------- 1 | * 2 | !.gitignore 3 | -------------------------------------------------------------------------------- /tests/laravel/storage/framework/views/.gitignore: -------------------------------------------------------------------------------- 1 | * 2 | !.gitignore 3 | -------------------------------------------------------------------------------- /tests/laravel/resources/model_views/DummyModel/templateABC.blade.php: -------------------------------------------------------------------------------- 1 |
Template ABC
-------------------------------------------------------------------------------- /tests/laravel/resources/model_views/DummyModel/templateXYZ.blade.php: -------------------------------------------------------------------------------- 1 |
Template XYZ
-------------------------------------------------------------------------------- /tests/laravel/.gitattributes: -------------------------------------------------------------------------------- 1 | * text=auto 2 | *.css linguist-vendored 3 | *.less linguist-vendored 4 | -------------------------------------------------------------------------------- /tests/laravel/.gitignore: -------------------------------------------------------------------------------- 1 | /vendor 2 | /node_modules 3 | Homestead.yaml 4 | Homestead.json 5 | .env 6 | -------------------------------------------------------------------------------- /tests/laravel/resources/model_views/DummyModel/templateDEF.blade.php: -------------------------------------------------------------------------------- 1 |
{{ json_encode([$object, $a, $b, $c]) }}
-------------------------------------------------------------------------------- /tests/laravel/app/Events/Event.php: -------------------------------------------------------------------------------- 1 | call(UserTableSeeder::class); 18 | 19 | Model::reguard(); 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /tests/laravel/public/.htaccess: -------------------------------------------------------------------------------- 1 | 2 | 3 | Options -MultiViews 4 | 5 | 6 | RewriteEngine On 7 | 8 | # Redirect Trailing Slashes If Not A Folder... 9 | RewriteCond %{REQUEST_FILENAME} !-d 10 | RewriteRule ^(.*)/$ /$1 [L,R=301] 11 | 12 | # Handle Front Controller... 13 | RewriteCond %{REQUEST_FILENAME} !-d 14 | RewriteCond %{REQUEST_FILENAME} !-f 15 | RewriteRule ^ index.php [L] 16 | 17 | -------------------------------------------------------------------------------- /tests/TestCase.php: -------------------------------------------------------------------------------- 1 | make(Kernel::class)->bootstrap(); 19 | 20 | return $app; 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /tests/laravel/app/Http/routes.php: -------------------------------------------------------------------------------- 1 | shouldReceive('doodad')->once(); 16 | App::instance('redlock', $mock); 17 | 18 | RedLock::doodad(); 19 | } 20 | 21 | public function testRoot() 22 | { 23 | $this->assertTrue(RedLock::getFacadeRoot() instanceof \ThatsUs\RedLock\RedLock); 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /tests/laravel/app/Jobs/Job.php: -------------------------------------------------------------------------------- 1 | '« Previous', 17 | 'next' => 'Next »', 18 | 19 | ]; 20 | -------------------------------------------------------------------------------- /tests/laravel/tests/TestCase.php: -------------------------------------------------------------------------------- 1 | make(Illuminate\Contracts\Console\Kernel::class)->bootstrap(); 22 | 23 | return $app; 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /tests/laravel/server.php: -------------------------------------------------------------------------------- 1 | 8 | */ 9 | 10 | $uri = urldecode( 11 | parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH) 12 | ); 13 | 14 | // This file allows us to emulate Apache's "mod_rewrite" functionality from the 15 | // built-in PHP web server. This provides a convenient way to test a Laravel 16 | // application without having installed a "real" web server software here. 17 | if ($uri !== '/' && file_exists(__DIR__.'/public'.$uri)) { 18 | return false; 19 | } 20 | 21 | require_once __DIR__.'/public/index.php'; 22 | -------------------------------------------------------------------------------- /tests/fixtures/QueueWithoutOverlapJob.php: -------------------------------------------------------------------------------- 1 | refreshLock(); 25 | $this->ran = true; 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /tests/laravel/resources/lang/en/auth.php: -------------------------------------------------------------------------------- 1 | 'These credentials do not match our records.', 17 | 'throttle' => 'Too many login attempts. Please try again in :seconds seconds.', 18 | 19 | ]; 20 | -------------------------------------------------------------------------------- /tests/fixtures/QueueWithoutOverlapJobDefaultLockTime.php: -------------------------------------------------------------------------------- 1 | refreshLock(); 24 | $this->ran = true; 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /tests/laravel/database/factories/ModelFactory.php: -------------------------------------------------------------------------------- 1 | define(App\User::class, function (Faker\Generator $faker) { 15 | return [ 16 | 'name' => $faker->name, 17 | 'email' => $faker->email, 18 | 'password' => bcrypt(str_random(10)), 19 | 'remember_token' => str_random(10), 20 | ]; 21 | }); 22 | -------------------------------------------------------------------------------- /tests/laravel/app/Console/Commands/Inspire.php: -------------------------------------------------------------------------------- 1 | comment(PHP_EOL.Inspiring::quote().PHP_EOL); 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /tests/laravel/app/Console/Kernel.php: -------------------------------------------------------------------------------- 1 | command('inspire') 28 | ->hourly(); 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /tests/laravel/database/migrations/2014_10_12_100000_create_password_resets_table.php: -------------------------------------------------------------------------------- 1 | string('email')->index(); 17 | $table->string('token')->index(); 18 | $table->timestamp('created_at'); 19 | }); 20 | } 21 | 22 | /** 23 | * Reverse the migrations. 24 | * 25 | * @return void 26 | */ 27 | public function down() 28 | { 29 | Schema::drop('password_resets'); 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /src/RedLockServiceProvider.php: -------------------------------------------------------------------------------- 1 | app->singleton('redlock', function ($app) { 24 | return new RedLock( 25 | config('database.redis.servers') ?: [config('database.redis.default')], 26 | config('database.redis.redis_lock.retry_delay'), 27 | config('database.redis.redis_lock.retry_count') 28 | ); 29 | }); 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /tests/laravel/app/Providers/AuthServiceProvider.php: -------------------------------------------------------------------------------- 1 | 'App\Policies\ModelPolicy', 17 | ]; 18 | 19 | /** 20 | * Register any application authentication / authorization services. 21 | * 22 | * @param \Illuminate\Contracts\Auth\Access\Gate $gate 23 | * @return void 24 | */ 25 | public function boot(GateContract $gate) 26 | { 27 | parent::registerPolicies($gate); 28 | 29 | // 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /tests/laravel/database/migrations/2014_10_12_000000_create_users_table.php: -------------------------------------------------------------------------------- 1 | increments('id'); 17 | $table->string('name'); 18 | $table->string('email')->unique(); 19 | $table->string('password', 60); 20 | $table->rememberToken(); 21 | $table->timestamps(); 22 | }); 23 | } 24 | 25 | /** 26 | * Reverse the migrations. 27 | * 28 | * @return void 29 | */ 30 | public function down() 31 | { 32 | Schema::drop('users'); 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /tests/laravel/resources/lang/en/passwords.php: -------------------------------------------------------------------------------- 1 | 'Passwords must be at least six characters and match the confirmation.', 17 | 'reset' => 'Your password has been reset!', 18 | 'sent' => 'We have e-mailed your password reset link!', 19 | 'token' => 'This password reset token is invalid.', 20 | 'user' => "We can't find a user with that e-mail address.", 21 | 22 | ]; 23 | -------------------------------------------------------------------------------- /tests/laravel/app/Providers/EventServiceProvider.php: -------------------------------------------------------------------------------- 1 | [ 17 | 'App\Listeners\EventListener', 18 | ], 19 | ]; 20 | 21 | /** 22 | * Register any other events for your application. 23 | * 24 | * @param \Illuminate\Contracts\Events\Dispatcher $events 25 | * @return void 26 | */ 27 | public function boot(DispatcherContract $events) 28 | { 29 | parent::boot($events); 30 | 31 | // 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /tests/laravel/app/Http/Middleware/RedirectIfAuthenticated.php: -------------------------------------------------------------------------------- 1 | auth = $auth; 26 | } 27 | 28 | /** 29 | * Handle an incoming request. 30 | * 31 | * @param \Illuminate\Http\Request $request 32 | * @param \Closure $next 33 | * @return mixed 34 | */ 35 | public function handle($request, Closure $next) 36 | { 37 | if ($this->auth->check()) { 38 | return redirect('/home'); 39 | } 40 | 41 | return $next($request); 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /tests/laravel/app/Http/Controllers/Auth/PasswordController.php: -------------------------------------------------------------------------------- 1 | middleware('guest'); 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /tests/laravel/phpunit.xml: -------------------------------------------------------------------------------- 1 | 2 | 12 | 13 | 14 | ./tests/ 15 | 16 | 17 | 18 | 19 | app/ 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "thatsus/laravel-redlock", 3 | "description": "Redis distributed locks for laravel", 4 | "license": "MIT", 5 | "keywords": ["redlock", "laravel redis lock", "redis lock"], 6 | "authors": [ 7 | { 8 | "name": "LibiChai", 9 | "email": "chaiguoxing@qq.com" 10 | }, 11 | { 12 | "name": "Daniel Kuck-Alvarez", 13 | "email": "dkuck@thatsus.com" 14 | }, 15 | { 16 | "name": "Potsky", 17 | "email": "potsky@me.com" 18 | } 19 | ], 20 | "require": { 21 | "php": ">=5.4.0", 22 | "illuminate/support": "^5.0,<5.5", 23 | "illuminate/console": "^5.0,<5.5", 24 | "predis/predis": "^1.1" 25 | }, 26 | "require-dev": { 27 | "laravel/framework": "5.1.*", 28 | "php-mock/php-mock-mockery": "^1.1", 29 | "phpunit/phpunit": "~5.7" 30 | }, 31 | "autoload": { 32 | "psr-4": { 33 | "ThatsUs\\RedLock\\": "src/" 34 | } 35 | }, 36 | "autoload-dev": { 37 | "classmap": [ 38 | "tests/" 39 | ] 40 | }, 41 | "minimum-stability": "stable" 42 | } 43 | -------------------------------------------------------------------------------- /phpunit.xml: -------------------------------------------------------------------------------- 1 | 2 | 12 | 13 | 14 | ./tests/ 15 | 16 | 17 | 18 | 19 | src/ 20 | 21 | 22 | app/Http/routes.php 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | -------------------------------------------------------------------------------- /tests/laravel/app/Http/Middleware/Authenticate.php: -------------------------------------------------------------------------------- 1 | auth = $auth; 26 | } 27 | 28 | /** 29 | * Handle an incoming request. 30 | * 31 | * @param \Illuminate\Http\Request $request 32 | * @param \Closure $next 33 | * @return mixed 34 | */ 35 | public function handle($request, Closure $next) 36 | { 37 | if ($this->auth->guest()) { 38 | if ($request->ajax()) { 39 | return response('Unauthorized.', 401); 40 | } else { 41 | return redirect()->guest('auth/login'); 42 | } 43 | } 44 | 45 | return $next($request); 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /tests/laravel/app/Http/Kernel.php: -------------------------------------------------------------------------------- 1 | \App\Http\Middleware\Authenticate::class, 30 | 'auth.basic' => \Illuminate\Auth\Middleware\AuthenticateWithBasicAuth::class, 31 | 'guest' => \App\Http\Middleware\RedirectIfAuthenticated::class, 32 | ]; 33 | } 34 | -------------------------------------------------------------------------------- /tests/laravel/config/compile.php: -------------------------------------------------------------------------------- 1 | [ 17 | // 18 | ], 19 | 20 | /* 21 | |-------------------------------------------------------------------------- 22 | | Compiled File Providers 23 | |-------------------------------------------------------------------------- 24 | | 25 | | Here you may list service providers which define a "compiles" function 26 | | that returns additional files that should be compiled, providing an 27 | | easy way to get common files from any packages you are utilizing. 28 | | 29 | */ 30 | 31 | 'providers' => [ 32 | // 33 | ], 34 | 35 | ]; 36 | -------------------------------------------------------------------------------- /tests/laravel/config/services.php: -------------------------------------------------------------------------------- 1 | [ 18 | 'domain' => env('MAILGUN_DOMAIN'), 19 | 'secret' => env('MAILGUN_SECRET'), 20 | ], 21 | 22 | 'mandrill' => [ 23 | 'secret' => env('MANDRILL_SECRET'), 24 | ], 25 | 26 | 'ses' => [ 27 | 'key' => env('SES_KEY'), 28 | 'secret' => env('SES_SECRET'), 29 | 'region' => 'us-east-1', 30 | ], 31 | 32 | 'stripe' => [ 33 | 'model' => App\User::class, 34 | 'key' => env('STRIPE_KEY'), 35 | 'secret' => env('STRIPE_SECRET'), 36 | ], 37 | 38 | ]; 39 | -------------------------------------------------------------------------------- /tests/laravel/config/view.php: -------------------------------------------------------------------------------- 1 | [ 17 | realpath(base_path('resources/views')), 18 | ], 19 | 20 | /* 21 | |-------------------------------------------------------------------------- 22 | | Compiled View Path 23 | |-------------------------------------------------------------------------- 24 | | 25 | | This option determines where all the compiled Blade templates will be 26 | | stored for your application. Typically, this is within the storage 27 | | directory. However, as usual, you are free to change this value. 28 | | 29 | */ 30 | 31 | 'compiled' => realpath(storage_path('framework/views')), 32 | 33 | ]; 34 | -------------------------------------------------------------------------------- /tests/laravel/bootstrap/autoload.php: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Laravel 5 | 6 | 7 | 8 | 37 | 38 | 39 |
40 |
41 |
Laravel 5
42 |
43 |
44 | 45 | 46 | -------------------------------------------------------------------------------- /tests/laravel/app/Providers/RouteServiceProvider.php: -------------------------------------------------------------------------------- 1 | group(['namespace' => $this->namespace], function ($router) { 41 | require app_path('Http/routes.php'); 42 | }); 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /tests/laravel/app/User.php: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Be right back. 5 | 6 | 7 | 8 | 39 | 40 | 41 |
42 |
43 |
Be right back.
44 |
45 |
46 | 47 | 48 | -------------------------------------------------------------------------------- /tests/laravel/app/Exceptions/Handler.php: -------------------------------------------------------------------------------- 1 | getMessage(), $e); 47 | } 48 | 49 | return parent::render($request, $e); 50 | } 51 | } 52 | -------------------------------------------------------------------------------- /tests/laravel/config/broadcasting.php: -------------------------------------------------------------------------------- 1 | env('BROADCAST_DRIVER', 'pusher'), 17 | 18 | /* 19 | |-------------------------------------------------------------------------- 20 | | Broadcast Connections 21 | |-------------------------------------------------------------------------- 22 | | 23 | | Here you may define all of the broadcast connections that will be used 24 | | to broadcast events to other systems or over websockets. Samples of 25 | | each available type of connection are provided inside this array. 26 | | 27 | */ 28 | 29 | 'connections' => [ 30 | 31 | 'pusher' => [ 32 | 'driver' => 'pusher', 33 | 'key' => env('PUSHER_KEY'), 34 | 'secret' => env('PUSHER_SECRET'), 35 | 'app_id' => env('PUSHER_APP_ID'), 36 | ], 37 | 38 | 'redis' => [ 39 | 'driver' => 'redis', 40 | 'connection' => 'default', 41 | ], 42 | 43 | 'log' => [ 44 | 'driver' => 'log', 45 | ], 46 | 47 | ], 48 | 49 | ]; 50 | -------------------------------------------------------------------------------- /tests/laravel/bootstrap/app.php: -------------------------------------------------------------------------------- 1 | singleton( 30 | Illuminate\Contracts\Http\Kernel::class, 31 | App\Http\Kernel::class 32 | ); 33 | 34 | $app->singleton( 35 | Illuminate\Contracts\Console\Kernel::class, 36 | App\Console\Kernel::class 37 | ); 38 | 39 | $app->singleton( 40 | Illuminate\Contracts\Debug\ExceptionHandler::class, 41 | App\Exceptions\Handler::class 42 | ); 43 | 44 | /* 45 | |-------------------------------------------------------------------------- 46 | | Return The Application 47 | |-------------------------------------------------------------------------- 48 | | 49 | | This script returns the application instance. The instance is given to 50 | | the calling script so we can separate the building of the instances 51 | | from the actual running of the application and sending responses. 52 | | 53 | */ 54 | 55 | return $app; 56 | -------------------------------------------------------------------------------- /tests/laravel/artisan: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env php 2 | make(Illuminate\Contracts\Console\Kernel::class); 32 | 33 | $status = $kernel->handle( 34 | $input = new Symfony\Component\Console\Input\ArgvInput, 35 | new Symfony\Component\Console\Output\ConsoleOutput 36 | ); 37 | 38 | /* 39 | |-------------------------------------------------------------------------- 40 | | Shutdown The Application 41 | |-------------------------------------------------------------------------- 42 | | 43 | | Once Artisan has finished running. We will fire off the shutdown events 44 | | so that any final work may be done by the application before we shut 45 | | down the process. This is the last thing to happen to the request. 46 | | 47 | */ 48 | 49 | $kernel->terminate($input, $status); 50 | 51 | exit($status); 52 | -------------------------------------------------------------------------------- /tests/laravel/public/index.php: -------------------------------------------------------------------------------- 1 | 8 | */ 9 | 10 | /* 11 | |-------------------------------------------------------------------------- 12 | | Register The Auto Loader 13 | |-------------------------------------------------------------------------- 14 | | 15 | | Composer provides a convenient, automatically generated class loader for 16 | | our application. We just need to utilize it! We'll simply require it 17 | | into the script here so that we don't have to worry about manual 18 | | loading any of our classes later on. It feels nice to relax. 19 | | 20 | */ 21 | 22 | require __DIR__.'/../bootstrap/autoload.php'; 23 | 24 | /* 25 | |-------------------------------------------------------------------------- 26 | | Turn On The Lights 27 | |-------------------------------------------------------------------------- 28 | | 29 | | We need to illuminate PHP development, so let us turn on the lights. 30 | | This bootstraps the framework and gets it ready for use, then it 31 | | will load up this application so that we can run it and send 32 | | the responses back to the browser and delight our users. 33 | | 34 | */ 35 | 36 | $app = require_once __DIR__.'/../bootstrap/app.php'; 37 | 38 | /* 39 | |-------------------------------------------------------------------------- 40 | | Run The Application 41 | |-------------------------------------------------------------------------- 42 | | 43 | | Once we have the application, we can handle the incoming request 44 | | through the kernel, and send the associated response back to 45 | | the client's browser allowing them to enjoy the creative 46 | | and wonderful application we have prepared for them. 47 | | 48 | */ 49 | 50 | $kernel = $app->make(Illuminate\Contracts\Http\Kernel::class); 51 | 52 | $response = $kernel->handle( 53 | $request = Illuminate\Http\Request::capture() 54 | ); 55 | 56 | $response->send(); 57 | 58 | $kernel->terminate($request, $response); 59 | -------------------------------------------------------------------------------- /tests/laravel/app/Http/Controllers/Auth/AuthController.php: -------------------------------------------------------------------------------- 1 | middleware('guest', ['except' => 'getLogout']); 34 | } 35 | 36 | /** 37 | * Get a validator for an incoming registration request. 38 | * 39 | * @param array $data 40 | * @return \Illuminate\Contracts\Validation\Validator 41 | */ 42 | protected function validator(array $data) 43 | { 44 | return Validator::make($data, [ 45 | 'name' => 'required|max:255', 46 | 'email' => 'required|email|max:255|unique:users', 47 | 'password' => 'required|confirmed|min:6', 48 | ]); 49 | } 50 | 51 | /** 52 | * Create a new user instance after a valid registration. 53 | * 54 | * @param array $data 55 | * @return User 56 | */ 57 | protected function create(array $data) 58 | { 59 | return User::create([ 60 | 'name' => $data['name'], 61 | 'email' => $data['email'], 62 | 'password' => bcrypt($data['password']), 63 | ]); 64 | } 65 | } 66 | -------------------------------------------------------------------------------- /tests/laravel/config/cache.php: -------------------------------------------------------------------------------- 1 | env('CACHE_DRIVER', 'file'), 17 | 18 | /* 19 | |-------------------------------------------------------------------------- 20 | | Cache Stores 21 | |-------------------------------------------------------------------------- 22 | | 23 | | Here you may define all of the cache "stores" for your application as 24 | | well as their drivers. You may even define multiple stores for the 25 | | same cache driver to group types of items stored in your caches. 26 | | 27 | */ 28 | 29 | 'stores' => [ 30 | 31 | 'apc' => [ 32 | 'driver' => 'apc', 33 | ], 34 | 35 | 'array' => [ 36 | 'driver' => 'array', 37 | ], 38 | 39 | 'database' => [ 40 | 'driver' => 'database', 41 | 'table' => 'cache', 42 | 'connection' => null, 43 | ], 44 | 45 | 'file' => [ 46 | 'driver' => 'file', 47 | 'path' => storage_path('framework/cache'), 48 | ], 49 | 50 | 'memcached' => [ 51 | 'driver' => 'memcached', 52 | 'servers' => [ 53 | [ 54 | 'host' => '127.0.0.1', 'port' => 11211, 'weight' => 100, 55 | ], 56 | ], 57 | ], 58 | 59 | 'redis' => [ 60 | 'driver' => 'redis', 61 | 'connection' => 'default', 62 | ], 63 | 64 | ], 65 | 66 | /* 67 | |-------------------------------------------------------------------------- 68 | | Cache Key Prefix 69 | |-------------------------------------------------------------------------- 70 | | 71 | | When utilizing a RAM based store such as APC or Memcached, there might 72 | | be other applications utilizing the same cache. So, we'll specify a 73 | | value to get prefixed to all our keys so we can avoid collisions. 74 | | 75 | */ 76 | 77 | 'prefix' => 'laravel', 78 | 79 | ]; 80 | -------------------------------------------------------------------------------- /tests/laravel/config/auth.php: -------------------------------------------------------------------------------- 1 | 'eloquent', 19 | 20 | /* 21 | |-------------------------------------------------------------------------- 22 | | Authentication Model 23 | |-------------------------------------------------------------------------- 24 | | 25 | | When using the "Eloquent" authentication driver, we need to know which 26 | | Eloquent model should be used to retrieve your users. Of course, it 27 | | is often just the "User" model but you may use whatever you like. 28 | | 29 | */ 30 | 31 | 'model' => App\User::class, 32 | 33 | /* 34 | |-------------------------------------------------------------------------- 35 | | Authentication Table 36 | |-------------------------------------------------------------------------- 37 | | 38 | | When using the "Database" authentication driver, we need to know which 39 | | table should be used to retrieve your users. We have chosen a basic 40 | | default value but you may easily change it to any table you like. 41 | | 42 | */ 43 | 44 | 'table' => 'users', 45 | 46 | /* 47 | |-------------------------------------------------------------------------- 48 | | Password Reset Settings 49 | |-------------------------------------------------------------------------- 50 | | 51 | | Here you may set the options for resetting passwords including the view 52 | | that is your password reset e-mail. You can also set the name of the 53 | | table that maintains all of the reset tokens for your application. 54 | | 55 | | The expire time is the number of minutes that the reset token should be 56 | | considered valid. This security feature keeps tokens short-lived so 57 | | they have less time to be guessed. You may change this as needed. 58 | | 59 | */ 60 | 61 | 'password' => [ 62 | 'email' => 'emails.password', 63 | 'table' => 'password_resets', 64 | 'expire' => 60, 65 | ], 66 | 67 | ]; 68 | -------------------------------------------------------------------------------- /tests/laravel/config/filesystems.php: -------------------------------------------------------------------------------- 1 | 'local', 19 | 20 | /* 21 | |-------------------------------------------------------------------------- 22 | | Default Cloud Filesystem Disk 23 | |-------------------------------------------------------------------------- 24 | | 25 | | Many applications store files both locally and in the cloud. For this 26 | | reason, you may specify a default "cloud" driver here. This driver 27 | | will be bound as the Cloud disk implementation in the container. 28 | | 29 | */ 30 | 31 | 'cloud' => 's3', 32 | 33 | /* 34 | |-------------------------------------------------------------------------- 35 | | Filesystem Disks 36 | |-------------------------------------------------------------------------- 37 | | 38 | | Here you may configure as many filesystem "disks" as you wish, and you 39 | | may even configure multiple disks of the same driver. Defaults have 40 | | been setup for each driver as an example of the required options. 41 | | 42 | */ 43 | 44 | 'disks' => [ 45 | 46 | 'local' => [ 47 | 'driver' => 'local', 48 | 'root' => storage_path('app'), 49 | ], 50 | 51 | 'ftp' => [ 52 | 'driver' => 'ftp', 53 | 'host' => 'ftp.example.com', 54 | 'username' => 'your-username', 55 | 'password' => 'your-password', 56 | 57 | // Optional FTP Settings... 58 | // 'port' => 21, 59 | // 'root' => '', 60 | // 'passive' => true, 61 | // 'ssl' => true, 62 | // 'timeout' => 30, 63 | ], 64 | 65 | 's3' => [ 66 | 'driver' => 's3', 67 | 'key' => 'your-key', 68 | 'secret' => 'your-secret', 69 | 'region' => 'your-region', 70 | 'bucket' => 'your-bucket', 71 | ], 72 | 73 | 'rackspace' => [ 74 | 'driver' => 'rackspace', 75 | 'username' => 'your-username', 76 | 'key' => 'your-key', 77 | 'container' => 'your-container', 78 | 'endpoint' => 'https://identity.api.rackspacecloud.com/v2.0/', 79 | 'region' => 'IAD', 80 | 'url_type' => 'publicURL', 81 | ], 82 | 83 | ], 84 | 85 | ]; 86 | -------------------------------------------------------------------------------- /tests/laravel/config/queue.php: -------------------------------------------------------------------------------- 1 | env('QUEUE_DRIVER', 'sync'), 20 | 21 | /* 22 | |-------------------------------------------------------------------------- 23 | | Queue Connections 24 | |-------------------------------------------------------------------------- 25 | | 26 | | Here you may configure the connection information for each server that 27 | | is used by your application. A default configuration has been added 28 | | for each back-end shipped with Laravel. You are free to add more. 29 | | 30 | */ 31 | 32 | 'connections' => [ 33 | 34 | 'sync' => [ 35 | 'driver' => 'sync', 36 | ], 37 | 38 | 'database' => [ 39 | 'driver' => 'database', 40 | 'table' => 'jobs', 41 | 'queue' => 'default', 42 | 'expire' => 60, 43 | ], 44 | 45 | 'beanstalkd' => [ 46 | 'driver' => 'beanstalkd', 47 | 'host' => 'localhost', 48 | 'queue' => 'default', 49 | 'ttr' => 60, 50 | ], 51 | 52 | 'sqs' => [ 53 | 'driver' => 'sqs', 54 | 'key' => 'your-public-key', 55 | 'secret' => 'your-secret-key', 56 | 'queue' => 'your-queue-url', 57 | 'region' => 'us-east-1', 58 | ], 59 | 60 | 'iron' => [ 61 | 'driver' => 'iron', 62 | 'host' => 'mq-aws-us-east-1.iron.io', 63 | 'token' => 'your-token', 64 | 'project' => 'your-project-id', 65 | 'queue' => 'your-queue-name', 66 | 'encrypt' => true, 67 | ], 68 | 69 | 'redis' => [ 70 | 'driver' => 'redis', 71 | 'connection' => 'default', 72 | 'queue' => 'default', 73 | 'expire' => 60, 74 | ], 75 | 76 | ], 77 | 78 | /* 79 | |-------------------------------------------------------------------------- 80 | | Failed Queue Jobs 81 | |-------------------------------------------------------------------------- 82 | | 83 | | These options configure the behavior of failed queue job logging so you 84 | | can control which database and table are used to store the jobs that 85 | | have failed. You may change them to any database / table you wish. 86 | | 87 | */ 88 | 89 | 'failed' => [ 90 | 'database' => 'mysql', 'table' => 'failed_jobs', 91 | ], 92 | 93 | ]; 94 | -------------------------------------------------------------------------------- /tests/QueueWithoutOverlapTest.php: -------------------------------------------------------------------------------- 1 | shouldReceive('push')->with($job)->once(); 22 | 23 | RedLock::shouldReceive('lock') 24 | ->with("ThatsUs\RedLock\Traits\QueueWithoutOverlapJob::1000:", 1000000) 25 | ->twice() 26 | ->andReturn(['resource' => 'ThatsUs\RedLock\Traits\QueueWithoutOverlapJob::1000:']); 27 | RedLock::shouldReceive('unlock') 28 | ->with(['resource' => 'ThatsUs\RedLock\Traits\QueueWithoutOverlapJob::1000:']) 29 | ->twice() 30 | ->andReturn(true); 31 | 32 | $job->queue($queue, $job); 33 | 34 | $job->handle(); 35 | 36 | $this->assertTrue($job->ran); 37 | } 38 | 39 | public function testFailToLock() 40 | { 41 | $job = new QueueWithoutOverlapJob(); 42 | 43 | $queue = Mockery::mock(); 44 | 45 | RedLock::shouldReceive('lock') 46 | ->with("ThatsUs\RedLock\Traits\QueueWithoutOverlapJob::1000:", 1000000) 47 | ->once() 48 | ->andReturn(false); 49 | 50 | $id = $job->queue($queue, $job); 51 | 52 | $this->assertFalse($id); 53 | } 54 | 55 | public function testFailToRefresh() 56 | { 57 | $job = new QueueWithoutOverlapJob(); 58 | 59 | $queue = Mockery::mock(); 60 | $queue->shouldReceive('push')->with($job)->once(); 61 | 62 | RedLock::shouldReceive('lock') 63 | ->with("ThatsUs\RedLock\Traits\QueueWithoutOverlapJob::1000:", 1000000) 64 | ->twice() 65 | ->andReturn( 66 | ['resource' => 'ThatsUs\RedLock\Traits\QueueWithoutOverlapJob::1000:'], 67 | false 68 | ); 69 | RedLock::shouldReceive('unlock') 70 | ->with(['resource' => 'ThatsUs\RedLock\Traits\QueueWithoutOverlapJob::1000:']) 71 | ->once() 72 | ->andReturn(true); 73 | 74 | $job->queue($queue, $job); 75 | 76 | $this->expectException('ThatsUs\RedLock\Exceptions\QueueWithoutOverlapRefreshException'); 77 | 78 | $job->handle(); 79 | } 80 | 81 | public function testAllOfItDefaultLockTime() 82 | { 83 | $job = new QueueWithoutOverlapJobDefaultLockTime(); 84 | 85 | $queue = Mockery::mock(); 86 | $queue->shouldReceive('push')->with($job)->once(); 87 | 88 | RedLock::shouldReceive('lock') 89 | ->with("ThatsUs\RedLock\Traits\QueueWithoutOverlapJobDefaultLockTime::", 300000) 90 | ->twice() 91 | ->andReturn(['resource' => "ThatsUs\RedLock\Traits\QueueWithoutOverlapJobDefaultLockTime::"]); 92 | RedLock::shouldReceive('unlock') 93 | ->with(['resource' => "ThatsUs\RedLock\Traits\QueueWithoutOverlapJobDefaultLockTime::"]) 94 | ->twice() 95 | ->andReturn(true); 96 | 97 | $job->queue($queue, $job); 98 | 99 | $job->handle(); 100 | 101 | $this->assertTrue($job->ran); 102 | } 103 | } 104 | -------------------------------------------------------------------------------- /src/Traits/QueueWithoutOverlap.php: -------------------------------------------------------------------------------- 1 | acquireLock()) { 24 | return $this->pushCommandToQueue($queue, $command); 25 | } else { 26 | // do nothing, could not get lock 27 | return false; 28 | } 29 | } 30 | 31 | /** 32 | * Lock this job's key in redis, so no other 33 | * jobs can run with the same key. 34 | * @return bool - false if it fails to lock 35 | */ 36 | protected function acquireLock(array $lock = []) 37 | { 38 | $lock_time = isset($this->lock_time) ? $this->lock_time : 300; // in seconds; 5 minutes default 39 | $this->lock = RedLock::lock($lock['resource'] ?? $this->getLockKey(), $lock_time * 1000); 40 | return (bool)$this->lock; 41 | } 42 | 43 | /** 44 | * Unlock this job's key in redis, so other 45 | * jobs can run with the same key. 46 | * @return void 47 | */ 48 | protected function releaseLock() 49 | { 50 | if ($this->lock) { 51 | RedLock::unlock($this->lock); 52 | } 53 | } 54 | 55 | /** 56 | * Build a unique key based on the values stored in this job. 57 | * Any job with the same values is assumed to represent the same 58 | * task and so will not overlap this. 59 | * 60 | * Override this method if necessary. 61 | * 62 | * @return string 63 | */ 64 | protected function getLockKey() 65 | { 66 | $values = collect((array)$this) 67 | ->values() 68 | ->map(function ($value) { 69 | if ($value instanceof Model) { 70 | return $value->id; 71 | } else if (is_object($value) || is_array($value)) { 72 | throw new \Exception('This job cannot auto-generate a lock-key. Please define getLockKey() on ' . get_class($this) . '.'); 73 | } else { 74 | return $value; 75 | } 76 | }); 77 | return get_class($this) . ':' . $values->implode(':'); 78 | } 79 | 80 | /** 81 | * This code is copied from Illuminate\Bus\Dispatcher v5.4 82 | * @ https://github.com/laravel/framework/blob/5.4/src/Illuminate/Bus/Dispatcher.php#L163 83 | * 84 | * Push the command onto the given queue instance. 85 | * 86 | * @param \Illuminate\Contracts\Queue\Queue $queue 87 | * @param mixed $command 88 | * @return mixed 89 | */ 90 | protected function pushCommandToQueue($queue, $command) 91 | { 92 | if (isset($command->queue, $command->delay)) { 93 | return $queue->laterOn($command->queue, $command->delay, $command); 94 | } 95 | if (isset($command->queue)) { 96 | return $queue->pushOn($command->queue, $command); 97 | } 98 | if (isset($command->delay)) { 99 | return $queue->later($command->delay, $command); 100 | } 101 | return $queue->push($command); 102 | } 103 | 104 | /** 105 | * Normal jobs are called via handle. Use handleSync instead. 106 | * @return void 107 | */ 108 | public function handle() 109 | { 110 | try { 111 | $this->handleSync(); 112 | } finally { 113 | $this->releaseLock(); 114 | } 115 | } 116 | 117 | /** 118 | * Attempt to reacquire and extend the lock. 119 | * @return bool true if the lock is reacquired, false if it is not 120 | */ 121 | protected function refreshLock() 122 | { 123 | $this->releaseLock(); 124 | if (!$this->acquireLock($this->lock)) { 125 | throw new QueueWithoutOverlapRefreshException(); 126 | } 127 | } 128 | } 129 | -------------------------------------------------------------------------------- /tests/laravel/config/database.php: -------------------------------------------------------------------------------- 1 | PDO::FETCH_CLASS, 17 | 18 | /* 19 | |-------------------------------------------------------------------------- 20 | | Default Database Connection Name 21 | |-------------------------------------------------------------------------- 22 | | 23 | | Here you may specify which of the database connections below you wish 24 | | to use as your default connection for all database work. Of course 25 | | you may use many connections at once using the Database library. 26 | | 27 | */ 28 | 29 | 'default' => env('DB_CONNECTION', 'mysql'), 30 | 31 | /* 32 | |-------------------------------------------------------------------------- 33 | | Database Connections 34 | |-------------------------------------------------------------------------- 35 | | 36 | | Here are each of the database connections setup for your application. 37 | | Of course, examples of configuring each database platform that is 38 | | supported by Laravel is shown below to make development simple. 39 | | 40 | | 41 | | All database work in Laravel is done through the PHP PDO facilities 42 | | so make sure you have the driver for your particular database of 43 | | choice installed on your machine before you begin development. 44 | | 45 | */ 46 | 47 | 'connections' => [ 48 | 49 | 'sqlite' => [ 50 | 'driver' => 'sqlite', 51 | 'database' => storage_path('database.sqlite'), 52 | 'prefix' => '', 53 | ], 54 | 55 | 'mysql' => [ 56 | 'driver' => 'mysql', 57 | 'host' => env('DB_HOST', 'localhost'), 58 | 'database' => env('DB_DATABASE', 'forge'), 59 | 'username' => env('DB_USERNAME', 'forge'), 60 | 'password' => env('DB_PASSWORD', ''), 61 | 'charset' => 'utf8', 62 | 'collation' => 'utf8_unicode_ci', 63 | 'prefix' => '', 64 | 'strict' => false, 65 | ], 66 | 67 | 'pgsql' => [ 68 | 'driver' => 'pgsql', 69 | 'host' => env('DB_HOST', 'localhost'), 70 | 'database' => env('DB_DATABASE', 'forge'), 71 | 'username' => env('DB_USERNAME', 'forge'), 72 | 'password' => env('DB_PASSWORD', ''), 73 | 'charset' => 'utf8', 74 | 'prefix' => '', 75 | 'schema' => 'public', 76 | ], 77 | 78 | 'sqlsrv' => [ 79 | 'driver' => 'sqlsrv', 80 | 'host' => env('DB_HOST', 'localhost'), 81 | 'database' => env('DB_DATABASE', 'forge'), 82 | 'username' => env('DB_USERNAME', 'forge'), 83 | 'password' => env('DB_PASSWORD', ''), 84 | 'charset' => 'utf8', 85 | 'prefix' => '', 86 | ], 87 | 88 | ], 89 | 90 | /* 91 | |-------------------------------------------------------------------------- 92 | | Migration Repository Table 93 | |-------------------------------------------------------------------------- 94 | | 95 | | This table keeps track of all the migrations that have already run for 96 | | your application. Using this information, we can determine which of 97 | | the migrations on disk haven't actually been run in the database. 98 | | 99 | */ 100 | 101 | 'migrations' => 'migrations', 102 | 103 | /* 104 | |-------------------------------------------------------------------------- 105 | | Redis Databases 106 | |-------------------------------------------------------------------------- 107 | | 108 | | Redis is an open source, fast, and advanced key-value store that also 109 | | provides a richer set of commands than a typical key-value systems 110 | | such as APC or Memcached. Laravel makes it easy to dig right in. 111 | | 112 | */ 113 | 114 | 'redis' => [ 115 | 116 | 'cluster' => false, 117 | 118 | 'default' => [ 119 | 'host' => '127.0.0.1', 120 | 'port' => 6379, 121 | 'database' => 0, 122 | ], 123 | 124 | ], 125 | 126 | ]; 127 | -------------------------------------------------------------------------------- /src/RedLock.php: -------------------------------------------------------------------------------- 1 | servers = $servers; 20 | $this->retryDelay = $retryDelay; 21 | $this->retryCount = $retryCount; 22 | $this->quorum = min(count($servers), (count($servers) / 2 + 1)); 23 | } 24 | 25 | public function lock($resource, $ttl) 26 | { 27 | $this->initInstances(); 28 | $token = uniqid(); 29 | $retry = $this->retryCount; 30 | do { 31 | $n = 0; 32 | $startTime = microtime(true) * 1000; 33 | foreach ($this->instances as $instance) { 34 | if ($this->lockInstance($instance, $resource, $token, $ttl)) { 35 | $n++; 36 | } 37 | } 38 | # Add 2 milliseconds to the drift to account for Redis expires 39 | # precision, which is 1 millisecond, plus 1 millisecond min drift 40 | # for small TTLs. 41 | $drift = ($ttl * $this->clockDriftFactor) + 2; 42 | $validityTime = $ttl - (microtime(true) * 1000 - $startTime) - $drift; 43 | if ($n >= $this->quorum && $validityTime > 0) { 44 | return [ 45 | 'validity' => $validityTime, 46 | 'resource' => $resource, 47 | 'token' => $token, 48 | 'ttl' => $ttl, 49 | ]; 50 | } else { 51 | foreach ($this->instances as $instance) { 52 | $this->unlockInstance($instance, $resource, $token); 53 | } 54 | } 55 | // Wait a random delay before to retry 56 | $delay = mt_rand(floor($this->retryDelay / 2), $this->retryDelay); 57 | usleep($delay * 1000); 58 | $retry--; 59 | } while ($retry > 0); 60 | return false; 61 | } 62 | 63 | public function unlock(array $lock) 64 | { 65 | $this->initInstances(); 66 | $resource = $lock['resource']; 67 | $token = $lock['token']; 68 | foreach ($this->instances as $instance) { 69 | $this->unlockInstance($instance, $resource, $token); 70 | } 71 | } 72 | 73 | private function initInstances() 74 | { 75 | if (empty($this->instances)) { 76 | foreach ($this->servers as $server) { 77 | //list($host, $port, $database) = $server; 78 | $redis = App::make(Redis::class, [$server]); 79 | //$redis->connect($host, $port, $timeout); 80 | $this->instances[] = $redis; 81 | } 82 | } 83 | } 84 | 85 | private function lockInstance($instance, $resource, $token, $ttl) 86 | { 87 | return $instance->set($resource, $token, "PX", $ttl, "NX"); 88 | //return $instance->set($resource, $token, ['NX', 'PX' => $ttl]); 89 | } 90 | 91 | private function unlockInstance($instance, $resource, $token) 92 | { 93 | $script = ' 94 | if redis.call("GET", KEYS[1]) == ARGV[1] then 95 | return redis.call("DEL", KEYS[1]) 96 | else 97 | return 0 98 | end 99 | '; 100 | return $instance->eval($script, 1, $resource, $token); 101 | //return $instance->eval($script, [$resource, $token], 1); 102 | } 103 | 104 | public function refreshLock(array $lock) 105 | { 106 | $this->unlock($lock); 107 | return $this->lock($lock['resource'], $lock['ttl']); 108 | } 109 | 110 | public function runLocked($resource, $ttl, $closure) 111 | { 112 | $lock = $this->lock($resource, $ttl); 113 | if (!$lock) { 114 | return false; 115 | } 116 | $refresh = function () use (&$lock) { 117 | $lock = $this->refreshLock($lock); 118 | if (!$lock) { 119 | throw new Exceptions\ClosureRefreshException(); 120 | } 121 | }; 122 | try { 123 | $result = $closure($refresh); 124 | } catch (Exceptions\ClosureRefreshException $e) { 125 | return false; 126 | } finally { 127 | if (is_array($lock)) { 128 | $this->unlock($lock); 129 | } 130 | } 131 | return $result; 132 | } 133 | } 134 | -------------------------------------------------------------------------------- /tests/laravel/config/mail.php: -------------------------------------------------------------------------------- 1 | env('MAIL_DRIVER', 'smtp'), 19 | 20 | /* 21 | |-------------------------------------------------------------------------- 22 | | SMTP Host Address 23 | |-------------------------------------------------------------------------- 24 | | 25 | | Here you may provide the host address of the SMTP server used by your 26 | | applications. A default option is provided that is compatible with 27 | | the Mailgun mail service which will provide reliable deliveries. 28 | | 29 | */ 30 | 31 | 'host' => env('MAIL_HOST', 'smtp.mailgun.org'), 32 | 33 | /* 34 | |-------------------------------------------------------------------------- 35 | | SMTP Host Port 36 | |-------------------------------------------------------------------------- 37 | | 38 | | This is the SMTP port used by your application to deliver e-mails to 39 | | users of the application. Like the host we have set this value to 40 | | stay compatible with the Mailgun e-mail application by default. 41 | | 42 | */ 43 | 44 | 'port' => env('MAIL_PORT', 587), 45 | 46 | /* 47 | |-------------------------------------------------------------------------- 48 | | Global "From" Address 49 | |-------------------------------------------------------------------------- 50 | | 51 | | You may wish for all e-mails sent by your application to be sent from 52 | | the same address. Here, you may specify a name and address that is 53 | | used globally for all e-mails that are sent by your application. 54 | | 55 | */ 56 | 57 | 'from' => ['address' => null, 'name' => null], 58 | 59 | /* 60 | |-------------------------------------------------------------------------- 61 | | E-Mail Encryption Protocol 62 | |-------------------------------------------------------------------------- 63 | | 64 | | Here you may specify the encryption protocol that should be used when 65 | | the application send e-mail messages. A sensible default using the 66 | | transport layer security protocol should provide great security. 67 | | 68 | */ 69 | 70 | 'encryption' => env('MAIL_ENCRYPTION', 'tls'), 71 | 72 | /* 73 | |-------------------------------------------------------------------------- 74 | | SMTP Server Username 75 | |-------------------------------------------------------------------------- 76 | | 77 | | If your SMTP server requires a username for authentication, you should 78 | | set it here. This will get used to authenticate with your server on 79 | | connection. You may also set the "password" value below this one. 80 | | 81 | */ 82 | 83 | 'username' => env('MAIL_USERNAME'), 84 | 85 | /* 86 | |-------------------------------------------------------------------------- 87 | | SMTP Server Password 88 | |-------------------------------------------------------------------------- 89 | | 90 | | Here you may set the password required by your SMTP server to send out 91 | | messages from your application. This will be given to the server on 92 | | connection so that the application will be able to send messages. 93 | | 94 | */ 95 | 96 | 'password' => env('MAIL_PASSWORD'), 97 | 98 | /* 99 | |-------------------------------------------------------------------------- 100 | | Sendmail System Path 101 | |-------------------------------------------------------------------------- 102 | | 103 | | When using the "sendmail" driver to send e-mails, we will need to know 104 | | the path to where Sendmail lives on this server. A default path has 105 | | been provided here, which will work well on most of your systems. 106 | | 107 | */ 108 | 109 | 'sendmail' => '/usr/sbin/sendmail -bs', 110 | 111 | /* 112 | |-------------------------------------------------------------------------- 113 | | Mail "Pretend" 114 | |-------------------------------------------------------------------------- 115 | | 116 | | When this option is enabled, e-mail will not actually be sent over the 117 | | web and will instead be written to your application's logs files so 118 | | you may inspect the message. This is great for local development. 119 | | 120 | */ 121 | 122 | 'pretend' => false, 123 | 124 | ]; 125 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Laravel RedLock 2 | 3 | Provides a generic locking mechanism using Redis. Implements the locking standard proposed by Redis. 4 | 5 | 6 | 7 | ### Acknowledgements 8 | 9 | This library was originally built by LibiChai based on the Redlock algorithm developed by antirez. The library was reworked by the team at That's Us, Inc. 10 | 11 | ### Installation 12 | 13 | 1. `composer require thatsus/laravel-redlock` 14 | 2. Add `ThatsUs\RedLock\RedLockServiceProvider::class,` to the `providers` array in config/app.php 15 | 3. Enjoy! 16 | 17 | 18 | ### It's Simple! 19 | 20 | Set a lock on any scalar. If the `lock()` method returns false, you did not acquire the lock. 21 | 22 | Store results of the `lock()` method. Use this value to release the lock with `unlock()`. 23 | 24 | ### Example 25 | 26 | This example sets a lock on the key "1" with a 3 second expiration time. 27 | 28 | If it acquired the lock, it does some work and releases the lock. 29 | 30 | ```php 31 | use ThatsUs\RedLock\Facades\RedLock; 32 | 33 | $product_id = 1; 34 | 35 | $lock_token = RedLock::lock($product_id, 3000); 36 | 37 | if ($lock_token) { 38 | 39 | $order->submit($product_id); 40 | 41 | RedLock::unlock($lock_token); 42 | } 43 | ``` 44 | 45 | ### Refresh 46 | 47 | Use `refreshLock()` to reacquire and extend the time of your lock. 48 | 49 | ```php 50 | use ThatsUs\RedLock\Facades\RedLock; 51 | 52 | $product_ids = [1, 2, 3, 5, 7]; 53 | 54 | $lock_token = RedLock::lock('order-submitter', 3000); 55 | 56 | while ($lock_token) { 57 | 58 | $order->submit(array_shift($product_ids)); 59 | 60 | $lock_token = RedLock::refreshLock($lock_token); 61 | } 62 | 63 | RedLock::unlock($lock_token); 64 | ``` 65 | 66 | ### Even Easier with Closures 67 | 68 | Use `runLocked()` for nicer syntax. The method returns the results of the closure, or else false if the lock could not be acquired. 69 | 70 | ```php 71 | use ThatsUs\RedLock\Facades\RedLock; 72 | 73 | $product_id = 1; 74 | 75 | $result = RedLock::runLocked($product_id, 3000, function () use ($order, $product_id) { 76 | $order->submit($product_id); 77 | return true; 78 | }); 79 | 80 | echo $result ? 'Worked!' : 'Lock not acquired.'; 81 | ``` 82 | 83 | ### Refresh with Closures 84 | 85 | You can easily refresh your tokens when using closures. The first parameter to your closure is `$refresh`. Simply call it when you want to refresh. If the lock cannot be refreshed, the closure will return. 86 | 87 | ```php 88 | use ThatsUs\RedLock\Facades\RedLock; 89 | 90 | $product_ids = [1, 2, 3, 5, 7]; 91 | 92 | $result = RedLock::runLocked($product_id, 3000, function ($refresh) use ($order, $product_ids) { 93 | foreach ($product_ids as $product_id) { 94 | $refresh(); 95 | $order->submit($product_id); 96 | } 97 | return true; 98 | }); 99 | 100 | echo $result ? 'Worked!' : 'Lock lost or never acquired.'; 101 | ``` 102 | 103 | ### Lock Queue Jobs Easily 104 | 105 | If you're running jobs on a Laravel queue, you may want to avoid queuing up the same job more than once at a time. 106 | 107 | The `ThatsUs\RedLock\Traits\QueueWithoutOverlap` trait provides this functionality with very few changes to your job. Usually only two changes are necessary. 108 | 109 | 1. `use ThatsUs\RedLock\Traits\QueueWithoutOverlap` as a trait 110 | 2. Change the `handle()` method to `handleSync()` 111 | 112 | ```php 113 | use ThatsUs\RedLock\Traits\QueueWithoutOverlap; 114 | 115 | class OrderProductJob 116 | { 117 | use QueueWithoutOverlap; 118 | 119 | public function __construct($order, $product_id) 120 | { 121 | $this->order = $order; 122 | $this->product_id = $product_id; 123 | } 124 | 125 | public function handleSync() 126 | { 127 | $this->order->submit($this->product_id); 128 | } 129 | 130 | } 131 | ``` 132 | 133 | Sometimes it's also necessary to specify a `getLockKey()` method. This method must return the string that needs to be locked. 134 | 135 | This is typically unnecessary because the lock key can be generated automatically. But if the job's data is not easy to stringify, you must define the `getLockKey()` method. 136 | 137 | This trait also provides a refresh method called `refreshLock()`. If `refreshLock()` is unable to refresh the lock, an exception is thrown and the job fails. 138 | 139 | Finally, you can change the lock time-to-live from the default 300 seconds to another 140 | value using the `$lock_time` property. 141 | 142 | ```php 143 | use ThatsUs\RedLock\Traits\QueueWithoutOverlap; 144 | 145 | class OrderProductsJob 146 | { 147 | use QueueWithoutOverlap; 148 | 149 | protected $lock_time = 600; // 10 minutes in seconds 150 | 151 | public function __construct($order, array $product_ids) 152 | { 153 | $this->order = $order; 154 | $this->product_ids = $product_ids; 155 | } 156 | 157 | // We need to define getLockKey() because $product_ids is an array and the 158 | // automatic key generator can't deal with arrays. 159 | protected function getLockKey() 160 | { 161 | $product_ids = implode(',', $this->product_ids); 162 | return "OrderProductsJob:{$this->order->id}:{$product_ids}"; 163 | } 164 | 165 | public function handleSync() 166 | { 167 | foreach ($this->product_ids as $product_id) { 168 | $this->refreshLock(); 169 | $this->order->submit($product_id); 170 | } 171 | } 172 | 173 | } 174 | ``` 175 | 176 | 177 | 178 | 179 | -------------------------------------------------------------------------------- /tests/RedLockTest.php: -------------------------------------------------------------------------------- 1 | shouldReceive('set') 21 | ->with('XYZ', Mockery::any(), "PX", 300000, "NX") 22 | ->once() 23 | ->andReturn(true); 24 | App::instance(Redis::class, $predis); 25 | 26 | $redlock = new RedLock([['tester']]); 27 | $lock = $redlock->lock('XYZ', 300000); 28 | 29 | $this->assertEquals('XYZ', $lock['resource']); 30 | $this->assertTrue(is_numeric($lock['validity'])); 31 | $this->assertNotNull($lock['token']); 32 | } 33 | 34 | public function testUnlock() 35 | { 36 | $predis = Mockery::mock(Redis::class); 37 | $predis->shouldReceive('eval') 38 | ->with(Mockery::any(), 1, 'XYZ', '1234') 39 | ->once() 40 | ->andReturn(true); 41 | App::instance(Redis::class, $predis); 42 | 43 | $redlock = new RedLock([['tester']]); 44 | $redlock->unlock([ 45 | 'resource' => 'XYZ', 46 | 'validity' => 300000, 47 | 'token' => 1234, 48 | ]); 49 | } 50 | 51 | public function testLockFail() 52 | { 53 | $predis = Mockery::mock(Redis::class); 54 | $predis->shouldReceive('set') 55 | ->with('XYZ', Mockery::any(), "PX", 300000, "NX") 56 | ->times(3) 57 | ->andReturn(false); 58 | $predis->shouldReceive('eval') 59 | ->with(Mockery::any(), 1, 'XYZ', Mockery::any()) 60 | ->times(3) 61 | ->andReturn(true); 62 | App::instance(Redis::class, $predis); 63 | 64 | $redlock = new RedLock([['tester']]); 65 | $lock = $redlock->lock('XYZ', 300000); 66 | 67 | $this->assertFalse($lock); 68 | } 69 | 70 | public function testUnlockFail() 71 | { 72 | $predis = Mockery::mock(Redis::class); 73 | $predis->shouldReceive('eval') 74 | ->with(Mockery::any(), 1, 'XYZ', '1234') 75 | ->once() 76 | ->andReturn(false); 77 | App::instance(Redis::class, $predis); 78 | 79 | $redlock = new RedLock([['tester']]); 80 | $redlock->unlock([ 81 | 'resource' => 'XYZ', 82 | 'validity' => 300000, 83 | 'token' => 1234, 84 | ]); 85 | } 86 | 87 | public function testRefresh() 88 | { 89 | $predis = Mockery::mock(Redis::class); 90 | $predis->shouldReceive('eval') 91 | ->with(Mockery::any(), 1, 'XYZ', '1234') 92 | ->once() 93 | ->andReturn(true); 94 | $predis->shouldReceive('set') 95 | ->with('XYZ', Mockery::any(), "PX", 300000, "NX") 96 | ->once() 97 | ->andReturn(true); 98 | App::instance(Redis::class, $predis); 99 | 100 | $redlock = new RedLock([['tester']]); 101 | $lock = $redlock->refreshLock([ 102 | 'resource' => 'XYZ', 103 | 'validity' => 300000, 104 | 'token' => 1234, 105 | 'ttl' => 300000, 106 | ]); 107 | 108 | $this->assertEquals('XYZ', $lock['resource']); 109 | $this->assertTrue(is_numeric($lock['validity'])); 110 | $this->assertNotNull($lock['token']); 111 | } 112 | 113 | public function testRunLocked() 114 | { 115 | $predis = Mockery::mock(Redis::class); 116 | $predis->shouldReceive('set') 117 | ->with('XYZ', Mockery::any(), "PX", 300000, "NX") 118 | ->once() 119 | ->andReturn(true); 120 | $predis->shouldReceive('eval') 121 | ->with(Mockery::any(), 1, 'XYZ', Mockery::any()) 122 | ->once() 123 | ->andReturn(true); 124 | App::instance(Redis::class, $predis); 125 | 126 | $redlock = new RedLock([['tester']]); 127 | $results = $redlock->runLocked('XYZ', 300000, function () { 128 | return "ABC"; 129 | }); 130 | 131 | $this->assertEquals('ABC', $results); 132 | } 133 | 134 | public function testRunLockedRefresh() 135 | { 136 | $predis = Mockery::mock(Redis::class); 137 | $predis->shouldReceive('set') 138 | ->with('XYZ', Mockery::any(), "PX", 300000, "NX") 139 | ->twice() 140 | ->andReturn(true); 141 | $predis->shouldReceive('eval') 142 | ->with(Mockery::any(), 1, 'XYZ', Mockery::any()) 143 | ->twice() 144 | ->andReturn(true); 145 | App::instance(Redis::class, $predis); 146 | 147 | $redlock = new RedLock([['tester']]); 148 | $results = $redlock->runLocked('XYZ', 300000, function ($refresh) { 149 | $refresh(); 150 | return "ABC"; 151 | }); 152 | 153 | $this->assertEquals('ABC', $results); 154 | } 155 | 156 | public function testRunLockedRefreshFail() 157 | { 158 | $predis = Mockery::mock(Redis::class); 159 | $predis->shouldReceive('set') 160 | ->with('XYZ', Mockery::any(), "PX", 300000, "NX") 161 | ->times(4) 162 | ->andReturn(true, false, false, false); 163 | $predis->shouldReceive('eval') 164 | ->with(Mockery::any(), 1, 'XYZ', Mockery::any()) 165 | ->times(4) 166 | ->andReturn(true); 167 | App::instance(Redis::class, $predis); 168 | 169 | $redlock = new RedLock([['tester']]); 170 | $results = $redlock->runLocked('XYZ', 300000, function ($refresh) { 171 | $refresh(); 172 | return "ABC"; 173 | }); 174 | 175 | $this->assertFalse($results); 176 | } 177 | } 178 | -------------------------------------------------------------------------------- /tests/laravel/config/session.php: -------------------------------------------------------------------------------- 1 | env('SESSION_DRIVER', 'file'), 20 | 21 | /* 22 | |-------------------------------------------------------------------------- 23 | | Session Lifetime 24 | |-------------------------------------------------------------------------- 25 | | 26 | | Here you may specify the number of minutes that you wish the session 27 | | to be allowed to remain idle before it expires. If you want them 28 | | to immediately expire on the browser closing, set that option. 29 | | 30 | */ 31 | 32 | 'lifetime' => 120, 33 | 34 | 'expire_on_close' => false, 35 | 36 | /* 37 | |-------------------------------------------------------------------------- 38 | | Session Encryption 39 | |-------------------------------------------------------------------------- 40 | | 41 | | This option allows you to easily specify that all of your session data 42 | | should be encrypted before it is stored. All encryption will be run 43 | | automatically by Laravel and you can use the Session like normal. 44 | | 45 | */ 46 | 47 | 'encrypt' => false, 48 | 49 | /* 50 | |-------------------------------------------------------------------------- 51 | | Session File Location 52 | |-------------------------------------------------------------------------- 53 | | 54 | | When using the native session driver, we need a location where session 55 | | files may be stored. A default has been set for you but a different 56 | | location may be specified. This is only needed for file sessions. 57 | | 58 | */ 59 | 60 | 'files' => storage_path('framework/sessions'), 61 | 62 | /* 63 | |-------------------------------------------------------------------------- 64 | | Session Database Connection 65 | |-------------------------------------------------------------------------- 66 | | 67 | | When using the "database" or "redis" session drivers, you may specify a 68 | | connection that should be used to manage these sessions. This should 69 | | correspond to a connection in your database configuration options. 70 | | 71 | */ 72 | 73 | 'connection' => null, 74 | 75 | /* 76 | |-------------------------------------------------------------------------- 77 | | Session Database Table 78 | |-------------------------------------------------------------------------- 79 | | 80 | | When using the "database" session driver, you may specify the table we 81 | | should use to manage the sessions. Of course, a sensible default is 82 | | provided for you; however, you are free to change this as needed. 83 | | 84 | */ 85 | 86 | 'table' => 'sessions', 87 | 88 | /* 89 | |-------------------------------------------------------------------------- 90 | | Session Sweeping Lottery 91 | |-------------------------------------------------------------------------- 92 | | 93 | | Some session drivers must manually sweep their storage location to get 94 | | rid of old sessions from storage. Here are the chances that it will 95 | | happen on a given request. By default, the odds are 2 out of 100. 96 | | 97 | */ 98 | 99 | 'lottery' => [2, 100], 100 | 101 | /* 102 | |-------------------------------------------------------------------------- 103 | | Session Cookie Name 104 | |-------------------------------------------------------------------------- 105 | | 106 | | Here you may change the name of the cookie used to identify a session 107 | | instance by ID. The name specified here will get used every time a 108 | | new session cookie is created by the framework for every driver. 109 | | 110 | */ 111 | 112 | 'cookie' => 'laravel_session', 113 | 114 | /* 115 | |-------------------------------------------------------------------------- 116 | | Session Cookie Path 117 | |-------------------------------------------------------------------------- 118 | | 119 | | The session cookie path determines the path for which the cookie will 120 | | be regarded as available. Typically, this will be the root path of 121 | | your application but you are free to change this when necessary. 122 | | 123 | */ 124 | 125 | 'path' => '/', 126 | 127 | /* 128 | |-------------------------------------------------------------------------- 129 | | Session Cookie Domain 130 | |-------------------------------------------------------------------------- 131 | | 132 | | Here you may change the domain of the cookie used to identify a session 133 | | in your application. This will determine which domains the cookie is 134 | | available to in your application. A sensible default has been set. 135 | | 136 | */ 137 | 138 | 'domain' => null, 139 | 140 | /* 141 | |-------------------------------------------------------------------------- 142 | | HTTPS Only Cookies 143 | |-------------------------------------------------------------------------- 144 | | 145 | | By setting this option to true, session cookies will only be sent back 146 | | to the server if the browser has a HTTPS connection. This will keep 147 | | the cookie from being sent to you if it can not be done securely. 148 | | 149 | */ 150 | 151 | 'secure' => false, 152 | 153 | ]; 154 | -------------------------------------------------------------------------------- /tests/laravel/resources/lang/en/validation.php: -------------------------------------------------------------------------------- 1 | 'The :attribute must be accepted.', 17 | 'active_url' => 'The :attribute is not a valid URL.', 18 | 'after' => 'The :attribute must be a date after :date.', 19 | 'alpha' => 'The :attribute may only contain letters.', 20 | 'alpha_dash' => 'The :attribute may only contain letters, numbers, and dashes.', 21 | 'alpha_num' => 'The :attribute may only contain letters and numbers.', 22 | 'array' => 'The :attribute must be an array.', 23 | 'before' => 'The :attribute must be a date before :date.', 24 | 'between' => [ 25 | 'numeric' => 'The :attribute must be between :min and :max.', 26 | 'file' => 'The :attribute must be between :min and :max kilobytes.', 27 | 'string' => 'The :attribute must be between :min and :max characters.', 28 | 'array' => 'The :attribute must have between :min and :max items.', 29 | ], 30 | 'boolean' => 'The :attribute field must be true or false.', 31 | 'confirmed' => 'The :attribute confirmation does not match.', 32 | 'date' => 'The :attribute is not a valid date.', 33 | 'date_format' => 'The :attribute does not match the format :format.', 34 | 'different' => 'The :attribute and :other must be different.', 35 | 'digits' => 'The :attribute must be :digits digits.', 36 | 'digits_between' => 'The :attribute must be between :min and :max digits.', 37 | 'email' => 'The :attribute must be a valid email address.', 38 | 'exists' => 'The selected :attribute is invalid.', 39 | 'filled' => 'The :attribute field is required.', 40 | 'image' => 'The :attribute must be an image.', 41 | 'in' => 'The selected :attribute is invalid.', 42 | 'integer' => 'The :attribute must be an integer.', 43 | 'ip' => 'The :attribute must be a valid IP address.', 44 | 'json' => 'The :attribute must be a valid JSON string.', 45 | 'max' => [ 46 | 'numeric' => 'The :attribute may not be greater than :max.', 47 | 'file' => 'The :attribute may not be greater than :max kilobytes.', 48 | 'string' => 'The :attribute may not be greater than :max characters.', 49 | 'array' => 'The :attribute may not have more than :max items.', 50 | ], 51 | 'mimes' => 'The :attribute must be a file of type: :values.', 52 | 'min' => [ 53 | 'numeric' => 'The :attribute must be at least :min.', 54 | 'file' => 'The :attribute must be at least :min kilobytes.', 55 | 'string' => 'The :attribute must be at least :min characters.', 56 | 'array' => 'The :attribute must have at least :min items.', 57 | ], 58 | 'not_in' => 'The selected :attribute is invalid.', 59 | 'numeric' => 'The :attribute must be a number.', 60 | 'regex' => 'The :attribute format is invalid.', 61 | 'required' => 'The :attribute field is required.', 62 | 'required_if' => 'The :attribute field is required when :other is :value.', 63 | 'required_with' => 'The :attribute field is required when :values is present.', 64 | 'required_with_all' => 'The :attribute field is required when :values is present.', 65 | 'required_without' => 'The :attribute field is required when :values is not present.', 66 | 'required_without_all' => 'The :attribute field is required when none of :values are present.', 67 | 'same' => 'The :attribute and :other must match.', 68 | 'size' => [ 69 | 'numeric' => 'The :attribute must be :size.', 70 | 'file' => 'The :attribute must be :size kilobytes.', 71 | 'string' => 'The :attribute must be :size characters.', 72 | 'array' => 'The :attribute must contain :size items.', 73 | ], 74 | 'string' => 'The :attribute must be a string.', 75 | 'timezone' => 'The :attribute must be a valid zone.', 76 | 'unique' => 'The :attribute has already been taken.', 77 | 'url' => 'The :attribute format is invalid.', 78 | 79 | /* 80 | |-------------------------------------------------------------------------- 81 | | Custom Validation Language Lines 82 | |-------------------------------------------------------------------------- 83 | | 84 | | Here you may specify custom validation messages for attributes using the 85 | | convention "attribute.rule" to name the lines. This makes it quick to 86 | | specify a specific custom language line for a given attribute rule. 87 | | 88 | */ 89 | 90 | 'custom' => [ 91 | 'attribute-name' => [ 92 | 'rule-name' => 'custom-message', 93 | ], 94 | ], 95 | 96 | /* 97 | |-------------------------------------------------------------------------- 98 | | Custom Validation Attributes 99 | |-------------------------------------------------------------------------- 100 | | 101 | | The following language lines are used to swap attribute place-holders 102 | | with something more reader friendly such as E-Mail Address instead 103 | | of "email". This simply helps us make messages a little cleaner. 104 | | 105 | */ 106 | 107 | 'attributes' => [], 108 | 109 | ]; 110 | -------------------------------------------------------------------------------- /tests/laravel/config/app.php: -------------------------------------------------------------------------------- 1 | env('APP_DEBUG', false), 17 | 18 | /* 19 | |-------------------------------------------------------------------------- 20 | | Application URL 21 | |-------------------------------------------------------------------------- 22 | | 23 | | This URL is used by the console to properly generate URLs when using 24 | | the Artisan command line tool. You should set this to the root of 25 | | your application so that it is used when running Artisan tasks. 26 | | 27 | */ 28 | 29 | 'url' => 'http://localhost', 30 | 31 | /* 32 | |-------------------------------------------------------------------------- 33 | | Application Timezone 34 | |-------------------------------------------------------------------------- 35 | | 36 | | Here you may specify the default timezone for your application, which 37 | | will be used by the PHP date and date-time functions. We have gone 38 | | ahead and set this to a sensible default for you out of the box. 39 | | 40 | */ 41 | 42 | 'timezone' => 'UTC', 43 | 44 | /* 45 | |-------------------------------------------------------------------------- 46 | | Application Locale Configuration 47 | |-------------------------------------------------------------------------- 48 | | 49 | | The application locale determines the default locale that will be used 50 | | by the translation service provider. You are free to set this value 51 | | to any of the locales which will be supported by the application. 52 | | 53 | */ 54 | 55 | 'locale' => 'en', 56 | 57 | /* 58 | |-------------------------------------------------------------------------- 59 | | Application Fallback Locale 60 | |-------------------------------------------------------------------------- 61 | | 62 | | The fallback locale determines the locale to use when the current one 63 | | is not available. You may change the value to correspond to any of 64 | | the language folders that are provided through your application. 65 | | 66 | */ 67 | 68 | 'fallback_locale' => 'en', 69 | 70 | /* 71 | |-------------------------------------------------------------------------- 72 | | Encryption Key 73 | |-------------------------------------------------------------------------- 74 | | 75 | | This key is used by the Illuminate encrypter service and should be set 76 | | to a random, 32 character string, otherwise these encrypted strings 77 | | will not be safe. Please do this before deploying an application! 78 | | 79 | */ 80 | 81 | 'key' => env('APP_KEY', 'SomeRandomString'), 82 | 83 | 'cipher' => 'AES-256-CBC', 84 | 85 | /* 86 | |-------------------------------------------------------------------------- 87 | | Logging Configuration 88 | |-------------------------------------------------------------------------- 89 | | 90 | | Here you may configure the log settings for your application. Out of 91 | | the box, Laravel uses the Monolog PHP logging library. This gives 92 | | you a variety of powerful log handlers / formatters to utilize. 93 | | 94 | | Available Settings: "single", "daily", "syslog", "errorlog" 95 | | 96 | */ 97 | 98 | 'log' => 'single', 99 | 100 | /* 101 | |-------------------------------------------------------------------------- 102 | | Autoloaded Service Providers 103 | |-------------------------------------------------------------------------- 104 | | 105 | | The service providers listed here will be automatically loaded on the 106 | | request to your application. Feel free to add your own services to 107 | | this array to grant expanded functionality to your applications. 108 | | 109 | */ 110 | 111 | 'providers' => [ 112 | 113 | /* 114 | * Laravel Framework Service Providers... 115 | */ 116 | Illuminate\Foundation\Providers\ArtisanServiceProvider::class, 117 | Illuminate\Auth\AuthServiceProvider::class, 118 | Illuminate\Broadcasting\BroadcastServiceProvider::class, 119 | Illuminate\Bus\BusServiceProvider::class, 120 | Illuminate\Cache\CacheServiceProvider::class, 121 | Illuminate\Foundation\Providers\ConsoleSupportServiceProvider::class, 122 | Illuminate\Routing\ControllerServiceProvider::class, 123 | Illuminate\Cookie\CookieServiceProvider::class, 124 | Illuminate\Database\DatabaseServiceProvider::class, 125 | Illuminate\Encryption\EncryptionServiceProvider::class, 126 | Illuminate\Filesystem\FilesystemServiceProvider::class, 127 | Illuminate\Foundation\Providers\FoundationServiceProvider::class, 128 | Illuminate\Hashing\HashServiceProvider::class, 129 | Illuminate\Mail\MailServiceProvider::class, 130 | Illuminate\Pagination\PaginationServiceProvider::class, 131 | Illuminate\Pipeline\PipelineServiceProvider::class, 132 | Illuminate\Queue\QueueServiceProvider::class, 133 | Illuminate\Redis\RedisServiceProvider::class, 134 | Illuminate\Auth\Passwords\PasswordResetServiceProvider::class, 135 | Illuminate\Session\SessionServiceProvider::class, 136 | Illuminate\Translation\TranslationServiceProvider::class, 137 | Illuminate\Validation\ValidationServiceProvider::class, 138 | Illuminate\View\ViewServiceProvider::class, 139 | ThatsUs\RedLock\RedLockServiceProvider::class, 140 | 141 | /* 142 | * Application Service Providers... 143 | */ 144 | App\Providers\AppServiceProvider::class, 145 | App\Providers\AuthServiceProvider::class, 146 | App\Providers\EventServiceProvider::class, 147 | App\Providers\RouteServiceProvider::class, 148 | 149 | ], 150 | 151 | /* 152 | |-------------------------------------------------------------------------- 153 | | Class Aliases 154 | |-------------------------------------------------------------------------- 155 | | 156 | | This array of class aliases will be registered when this application 157 | | is started. However, feel free to register as many as you wish as 158 | | the aliases are "lazy" loaded so they don't hinder performance. 159 | | 160 | */ 161 | 162 | 'aliases' => [ 163 | 164 | 'App' => Illuminate\Support\Facades\App::class, 165 | 'Artisan' => Illuminate\Support\Facades\Artisan::class, 166 | 'Auth' => Illuminate\Support\Facades\Auth::class, 167 | 'Blade' => Illuminate\Support\Facades\Blade::class, 168 | 'Bus' => Illuminate\Support\Facades\Bus::class, 169 | 'Cache' => Illuminate\Support\Facades\Cache::class, 170 | 'Config' => Illuminate\Support\Facades\Config::class, 171 | 'Cookie' => Illuminate\Support\Facades\Cookie::class, 172 | 'Crypt' => Illuminate\Support\Facades\Crypt::class, 173 | 'DB' => Illuminate\Support\Facades\DB::class, 174 | 'Eloquent' => Illuminate\Database\Eloquent\Model::class, 175 | 'Event' => Illuminate\Support\Facades\Event::class, 176 | 'File' => Illuminate\Support\Facades\File::class, 177 | 'Gate' => Illuminate\Support\Facades\Gate::class, 178 | 'Hash' => Illuminate\Support\Facades\Hash::class, 179 | 'Input' => Illuminate\Support\Facades\Input::class, 180 | 'Inspiring' => Illuminate\Foundation\Inspiring::class, 181 | 'Lang' => Illuminate\Support\Facades\Lang::class, 182 | 'Log' => Illuminate\Support\Facades\Log::class, 183 | 'Mail' => Illuminate\Support\Facades\Mail::class, 184 | 'Password' => Illuminate\Support\Facades\Password::class, 185 | 'Queue' => Illuminate\Support\Facades\Queue::class, 186 | 'Redirect' => Illuminate\Support\Facades\Redirect::class, 187 | 'Redis' => Illuminate\Support\Facades\Redis::class, 188 | 'Request' => Illuminate\Support\Facades\Request::class, 189 | 'Response' => Illuminate\Support\Facades\Response::class, 190 | 'Route' => Illuminate\Support\Facades\Route::class, 191 | 'Schema' => Illuminate\Support\Facades\Schema::class, 192 | 'Session' => Illuminate\Support\Facades\Session::class, 193 | 'Storage' => Illuminate\Support\Facades\Storage::class, 194 | 'URL' => Illuminate\Support\Facades\URL::class, 195 | 'Validator' => Illuminate\Support\Facades\Validator::class, 196 | 'View' => Illuminate\Support\Facades\View::class, 197 | 198 | ], 199 | 200 | ]; 201 | --------------------------------------------------------------------------------