Skip to content

Commit 32c149e

Browse files
Merge pull request #56255 from nextcloud/availability-same-request
make failed availability check apply in the same request
2 parents eaeabac + 17104bf commit 32c149e

File tree

4 files changed

+37
-6
lines changed

4 files changed

+37
-6
lines changed

lib/private/Files/Cache/Storage.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ public static function getNumericStorageId($storageId) {
126126
}
127127

128128
/**
129-
* @return array [ available, last_checked ]
129+
* @return array{available: bool, last_checked: int}
130130
*/
131131
public function getAvailability() {
132132
if ($row = self::getStorageById($this->storageId)) {

lib/private/Files/Storage/Common.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -711,7 +711,7 @@ private function getLockLogger(): ?LoggerInterface {
711711
}
712712

713713
/**
714-
* @return array [ available, last_checked ]
714+
* @return array{available: bool, last_checked: int}
715715
*/
716716
public function getAvailability(): array {
717717
return $this->getStorageCache()->getAvailability();

lib/private/Files/Storage/Wrapper/Availability.php

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ class Availability extends Wrapper {
2222

2323
/** @var IConfig */
2424
protected $config;
25+
protected ?bool $available = null;
2526

2627
public function __construct(array $parameters) {
2728
$this->config = $parameters['config'] ?? \OCP\Server::get(IConfig::class);
@@ -54,11 +55,14 @@ private function updateAvailability(): bool {
5455
}
5556

5657
private function isAvailable(): bool {
57-
$availability = $this->getAvailability();
58-
if (self::shouldRecheck($availability)) {
59-
return $this->updateAvailability();
58+
if (is_null($this->available)) {
59+
$availability = $this->getAvailability();
60+
if (self::shouldRecheck($availability)) {
61+
return $this->updateAvailability();
62+
}
63+
$this->available = $availability['available'];
6064
}
61-
return $availability['available'];
65+
return $this->available;
6266
}
6367

6468
/**
@@ -257,6 +261,7 @@ protected function setUnavailable(?StorageNotAvailableException $e): void {
257261
self::RECHECK_TTL_SEC
258262
);
259263
}
264+
$this->available = false;
260265
$this->getStorageCache()->setAvailability(false, $delay);
261266
if ($e !== null) {
262267
throw $e;

tests/lib/Files/Storage/Wrapper/AvailabilityTest.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,4 +155,30 @@ public function testAvailableThrow(): void {
155155

156156
$this->wrapper->mkdir('foobar');
157157
}
158+
159+
public function testUnavailableMultiple(): void {
160+
$this->storage->expects($this->once())
161+
->method('getAvailability')
162+
->willReturn(['available' => true, 'last_checked' => 0]);
163+
$this->storage->expects($this->never())
164+
->method('test');
165+
$this->storage
166+
->expects($this->once()) // load-bearing `once`
167+
->method('mkdir')
168+
->willThrowException(new StorageNotAvailableException());
169+
170+
try {
171+
$this->wrapper->mkdir('foobar');
172+
$this->fail();
173+
} catch (StorageNotAvailableException) {
174+
}
175+
176+
$this->storage->expects($this->never())->method('file_exists');
177+
178+
try {
179+
$this->wrapper->mkdir('foobar');
180+
$this->fail();
181+
} catch (StorageNotAvailableException) {
182+
}
183+
}
158184
}

0 commit comments

Comments
 (0)