Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Laracon 2024 #52710

Merged
merged 49 commits into from
Sep 11, 2024
Merged
Show file tree
Hide file tree
Changes from 44 commits
Commits
Show all changes
49 commits
Select commit Hold shift + click to select a range
ed4884a
work on defer
taylorotwell Jul 2, 2024
50aae67
refine defer
taylorotwell Jul 4, 2024
efa71ff
refactor
taylorotwell Jul 4, 2024
714cef2
formatting
taylorotwell Jul 4, 2024
98b4d4c
fix new
taylorotwell Jul 4, 2024
cfd40dd
fix status code
taylorotwell Jul 5, 2024
f39acdc
handle job releases after exceptions
taylorotwell Jul 5, 2024
b4b7a75
add job attempted event
taylorotwell Jul 5, 2024
cff79e4
formatting
taylorotwell Jul 5, 2024
3ec696c
formatting
taylorotwell Jul 5, 2024
b7fa85f
first pass at swr
taylorotwell Jul 16, 2024
c9e1b7d
rename method
taylorotwell Jul 18, 2024
11977e7
add swr test
taylorotwell Jul 18, 2024
569c6f5
starting concurrency
taylorotwell Feb 3, 2024
5ec9d6b
wip
taylorotwell Feb 3, 2024
dc3d5d7
formatting
taylorotwell Feb 7, 2024
8ce4659
driver based
taylorotwell Feb 7, 2024
c89d85a
formatting
taylorotwell Feb 7, 2024
ecd8949
block on web
taylorotwell Jul 18, 2024
5e68fb3
fix docblock
taylorotwell Jul 18, 2024
65556fa
first pass at local temporary files
taylorotwell Jul 19, 2024
f467116
remove imports
taylorotwell Jul 19, 2024
69b658b
docblock
taylorotwell Jul 19, 2024
d2db712
work on routing
taylorotwell Jul 24, 2024
989a5f5
allow customization of serve behavior
taylorotwell Jul 25, 2024
1058023
work on local file serving
taylorotwell Jul 25, 2024
3de3331
route caching
taylorotwell Jul 25, 2024
b6656e3
move route caching check
taylorotwell Jul 25, 2024
9913bbc
extract file
taylorotwell Jul 25, 2024
318218c
formatting
taylorotwell Jul 25, 2024
14a9ae2
formatting
taylorotwell Jul 25, 2024
984c4a2
formatting
taylorotwell Jul 25, 2024
8ac5618
add test
taylorotwell Jul 31, 2024
308f142
formatting
taylorotwell Aug 1, 2024
1dd890a
add cache control
taylorotwell Aug 9, 2024
cc64665
use defer in concurrency
taylorotwell Aug 9, 2024
14dc1ad
allow return from sleep
taylorotwell Aug 9, 2024
03d198b
adjust sleep behavior
taylorotwell Aug 9, 2024
7726a92
add config file
taylorotwell Aug 20, 2024
1a8853e
testing
taylorotwell Aug 21, 2024
d9da8ae
add while to sleep
taylorotwell Aug 22, 2024
eb207ac
add autoload
taylorotwell Aug 25, 2024
3229285
add function
taylorotwell Aug 25, 2024
9ed9953
Apply fixes from StyleCI
StyleCIBot Sep 9, 2024
d71f4ee
Concurrency Improvements (#52713)
crynobone Sep 9, 2024
419893c
adjust php binary
taylorotwell Sep 11, 2024
516cd78
Merge branch 'laracon-2024' of github.com:laravel/framework into lara…
taylorotwell Sep 11, 2024
16b85a3
php executable finder
taylorotwell Sep 11, 2024
1de6373
false arg
taylorotwell Sep 11, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion composer.json
crynobone marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@
"league/flysystem-sftp-v3": "^3.0",
"mockery/mockery": "^1.6",
"nyholm/psr7": "^1.2",
"orchestra/testbench-core": "^9.1.5",
"orchestra/testbench-core": "^9.3.0",
"pda/pheanstalk": "^5.0",
"phpstan/phpstan": "^1.11.5",
"phpunit/phpunit": "^10.5|^11.0",
Expand All @@ -131,6 +131,7 @@
"src/Illuminate/Events/functions.php",
"src/Illuminate/Filesystem/functions.php",
"src/Illuminate/Foundation/helpers.php",
"src/Illuminate/Log/functions.php",
"src/Illuminate/Support/helpers.php"
],
"psr-4": {
Expand Down
20 changes: 20 additions & 0 deletions config/concurrency.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

return [

/*
|--------------------------------------------------------------------------
| Default Concurrency Driver
|--------------------------------------------------------------------------
|
| This option determines the default concurrency driver that will be used
| by Laravel's concurrency functions. By default, concurrent work will
| be sent to isolated PHP processes which will return their results.
|
| Supported: "process", "fork", "sync"
|
*/

'driver' => env('CONCURRENCY_DRIVER', 'process'),

];
55 changes: 55 additions & 0 deletions src/Illuminate/Cache/Repository.php
Original file line number Diff line number Diff line change
Expand Up @@ -471,6 +471,61 @@ public function rememberForever($key, Closure $callback)
return $value;
}

/**
* Retrieve an item from the cache by key, refreshing it in the background if it is stale.
*
* @template TCacheValue
*
* @param string $key
* @param array{ 0: int, 1: int } $ttl
* @param (callable(): TCacheValue) $callback
* @param array{ seconds?: int, owner?: string }|null $lock
* @return TCacheValue
*/
public function flexible($key, $ttl, $callback, $lock = null)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Curious what you think about calling this (or aliasing it to) Cache::throttle() or Cache::debounce(), since it is literally throttling/debouncing the refreshing of the cached value. Regardless can't wait to use it!

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Another is why a 2-element array for ttl and not spreading it as, say, ($key, $ttlFresh, $ttlStale, ...)

{
[
$key => $value,
"{$key}:created" => $created,
] = $this->many([$key, "{$key}:created"]);

if ($created === null) {
return tap(value($callback), fn ($value) => $this->putMany([
$key => $value,
"{$key}:created" => Carbon::now()->getTimestamp(),
], $ttl[1]));
}

if (($created + $this->getSeconds($ttl[0])) > Carbon::now()->getTimestamp()) {
return $value;
}

$refresh = function () use ($key, $ttl, $callback, $lock, $created) {
$this->store->lock(
"illuminate:cache:refresh:lock:{$key}",
$lock['seconds'] ?? 0,
$lock['owner'] ?? null,
)->get(function () use ($key, $callback, $created, $ttl) {
if ($created !== $this->get("{$key}:created")) {
return;
}

$this->putMany([
$key => value($callback),
"{$key}:created" => Carbon::now()->getTimestamp(),
], $ttl[1]);
});
};

if (function_exists('defer')) {
defer($refresh, "illuminate:cache:refresh:{$key}");
} else {
$refresh();
}

return $value;
}

/**
* Remove an item from the cache.
*
Expand Down
2 changes: 2 additions & 0 deletions src/Illuminate/Concurrency/.gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/.github export-ignore
.gitattributes export-ignore
100 changes: 100 additions & 0 deletions src/Illuminate/Concurrency/ConcurrencyManager.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
<?php

namespace Illuminate\Concurrency;

use Illuminate\Process\Factory as ProcessFactory;
use Illuminate\Support\MultipleInstanceManager;
use RuntimeException;
use Spatie\Fork\Fork;

/**
* @mixin \Illuminate\Contracts\Concurrency\Driver
*/
class ConcurrencyManager extends MultipleInstanceManager
{
/**
* Get a driver instance by name.
*
* @param string|null $name
* @return mixed
*/
public function driver($name = null)
{
return $this->instance($name);
}

/**
* Create an instance of the process concurrency driver.
*
* @param array $config
* @return \Illuminate\Concurrency\ProcessDriver
*/
public function createProcessDriver(array $config)
{
return new ProcessDriver($this->app->make(ProcessFactory::class));
}

/**
* Create an instance of the fork concurrency driver.
*
* @param array $config
* @return \Illuminate\Concurrency\ForkDriver
*/
public function createForkDriver(array $config)
{
if (! $this->app->runningInConsole()) {
throw new RuntimeException('Due to PHP limitations, the fork driver may not be used within web requests.');
}

if (! class_exists(Fork::class)) {
throw new RuntimeException('Please install the "spatie/fork" Composer package in order to utilize the "fork" driver.');
}

return new ForkDriver;
}

/**
* Create an instance of the sync concurrency driver.
*
* @param array $config
* @return \Illuminate\Concurrency\SyncDriver
*/
public function createSyncDriver(array $config)
{
return new SyncDriver;
}

/**
* Get the default instance name.
*
* @return string
*/
public function getDefaultInstance()
{
return $this->app['config']['concurrency.default'] ?? 'process';
}

/**
* Set the default instance name.
*
* @param string $name
* @return void
*/
public function setDefaultInstance($name)
{
$this->app['config']['concurrency.default'] = $name;
}

/**
* Get the instance specific configuration.
*
* @param string $name
* @return array
*/
public function getInstanceConfig($name)
{
return $this->app['config']->get(
'concurrency.drivers.'.$name, ['driver' => $name],
);
}
}
33 changes: 33 additions & 0 deletions src/Illuminate/Concurrency/ConcurrencyServiceProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

namespace Illuminate\Concurrency;

use Illuminate\Contracts\Support\DeferrableProvider;
use Illuminate\Support\ServiceProvider;

class ConcurrencyServiceProvider extends ServiceProvider implements DeferrableProvider
{
/**
* Register the service provider.
*
* @return void
*/
public function register()
{
$this->app->singleton(ConcurrencyManager::class, function ($app) {
return new ConcurrencyManager($app);
});
}

/**
* Get the services provided by the provider.
*
* @return array
*/
public function provides()
{
return [
ConcurrencyManager::class,
];
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
<?php

namespace Illuminate\Concurrency\Console;

use Illuminate\Console\Command;
use Symfony\Component\Console\Attribute\AsCommand;
use Throwable;

#[AsCommand(name: 'invoke-serialized-closure')]
class InvokeSerializedClosureCommand extends Command
{
/**
* The console command signature.
*
* @var string
*/
protected $signature = 'invoke-serialized-closure {code? : The serialized closure}';

/**
* The console command description.
*
* @var string
*/
protected $description = 'Invoke the given serialized closure';

/**
* Indicates whether the command should be shown in the Artisan command list.
*
* @var bool
*/
protected $hidden = true;

/**
* Execute the console command.
*
* @return void
*
* @throws \RuntimeException
*/
public function handle()
{
try {
$this->output->write(json_encode([
'successful' => true,
'result' => serialize($this->laravel->call(match (true) {
! is_null($this->argument('code')) => unserialize($this->argument('code')),
isset($_SERVER['LARAVEL_INVOKABLE_CLOSURE']) => unserialize($_SERVER['LARAVEL_INVOKABLE_CLOSURE']),
default => fn () => null,
})),
]));
} catch (Throwable $e) {
report($e);

$this->output->write(json_encode([
'successful' => false,
'exception' => get_class($e),
'message' => $e->getMessage(),
'file' => $e->getFile(),
'line' => $e->getLine(),
]));
}
}
}
27 changes: 27 additions & 0 deletions src/Illuminate/Concurrency/ForkDriver.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

namespace Illuminate\Concurrency;

use Closure;
use Illuminate\Foundation\Defer\DeferredCallback;
use Illuminate\Support\Arr;
use Spatie\Fork\Fork;

class ForkDriver
{
/**
* Run the given tasks concurrently and return an array containing the results.
*/
public function run(Closure|array $tasks): array
{
return Fork::new()->run(...Arr::wrap($tasks));

Check failure on line 17 in src/Illuminate/Concurrency/ForkDriver.php

View workflow job for this annotation

GitHub Actions / Source Code

Call to static method new() on an unknown class Spatie\Fork\Fork.
}

/**
* Start the given tasks in the background after the current task has finished.
*/
public function defer(Closure|array $tasks): DeferredCallback
{
return defer(fn () => $this->run($tasks));
}
}
21 changes: 21 additions & 0 deletions src/Illuminate/Concurrency/LICENSE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
The MIT License (MIT)

Copyright (c) Taylor Otwell

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
Loading
Loading