Skip to content

Commit

Permalink
Mutex
Browse files Browse the repository at this point in the history
  • Loading branch information
timkelty committed Nov 20, 2023
1 parent 095f43e commit be22e05
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 8 deletions.
1 change: 0 additions & 1 deletion src/Helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
use craft\db\Table;
use craft\helpers\App;
use craft\helpers\ConfigHelper;
use craft\mutex\Mutex;
use craft\queue\Queue as CraftQueue;
use yii\web\DbSession;

Expand Down
15 changes: 11 additions & 4 deletions src/Module.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,12 @@
use craft\services\ImageTransforms;
use craft\utilities\ClearCaches;
use craft\web\Application as WebApplication;
use craft\web\Response;
use craft\web\Response as WebResponse;
use craft\web\twig\variables\CraftVariable;
use craft\web\View;
use Illuminate\Support\Collection;
use yii\base\InvalidConfigException;
use yii\web\Response as YiiResponse;
use yii\web\Response as YiiWebResponse;

/**
* @property-read Config $config
Expand Down Expand Up @@ -197,6 +197,13 @@ protected function bootstrapCloud(ConsoleApplication|WebApplication $app): void
],
);

Event::on(
WebResponse::class,
YiiWebResponse::EVENT_BEFORE_SEND,
[$this->get('mutex'), 'handleBeforeSend'],
);


$this->setComponents([
'staticCaching' => StaticCaching::class,
]);
Expand Down Expand Up @@ -226,8 +233,8 @@ protected function bootstrapCloud(ConsoleApplication|WebApplication $app): void
);

Event::on(
Response::class,
YiiResponse::EVENT_BEFORE_SEND,
WebResponse::class,
YiiWebResponse::EVENT_BEFORE_SEND,
[$this->get('staticCaching'), 'handleBeforeSend'],
);
}
Expand Down
27 changes: 25 additions & 2 deletions src/Mutex.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Craft;
use craft\mutex\MutexTrait;
use GuzzleHttp\Exception\RequestException;
use Illuminate\Support\Collection;
use yii\base\Exception;

class Mutex extends \yii\mutex\Mutex
Expand All @@ -25,7 +26,7 @@ protected function acquireLock($name, $timeout = 0): bool

try {
Craft::createGuzzleClient()
->request('HEAD', $url, [
->request('HEAD', (string) $url, [
'headers' => [
HeaderEnum::MUTEX_ACQUIRE_LOCK->value => $name,
],
Expand Down Expand Up @@ -60,11 +61,13 @@ protected function releaseLock($name): bool

try {
Craft::createGuzzleClient()
->request('HEAD', $url, [
->request('HEAD', (string) $url, [
'headers' => [
HeaderEnum::MUTEX_RELEASE_LOCK->value => $name,
],
]);

return true;
} catch (RequestException $e) {
Craft::error('Unable to release mutex lock: ' . $e->getMessage());

Expand All @@ -78,4 +81,24 @@ protected function releaseLock($name): bool

return true;
}

public function handleBeforeSend(): void
{
$headers = Collection::make(Craft::$app->getRequest()->getHeaders())
->only([
HeaderEnum::MUTEX_ACQUIRE_LOCK->value,
HeaderEnum::MUTEX_RELEASE_LOCK->value,
]);

if ($headers->isNotEmpty()) {
Craft::$app->getResponse()->setNoCacheHeaders();
}

$headers->each(function($value, $key) {
Craft::$app->getResponse()->getHeaders()->setDefault(
$key,
$value,
);
});
}
}
2 changes: 1 addition & 1 deletion src/StaticCaching.php
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ public function purgeAll(): void
// TODO: should this hit a ping/healthcheck controller instead?
// TODO: send authorization header
Craft::createGuzzleClient()
->request('HEAD', $url, [
->request('HEAD', (string) $url, [
'headers' => [
HeaderEnum::CACHE_PURGE->value => '*',
],
Expand Down

0 comments on commit be22e05

Please sign in to comment.