Skip to content

Commit 0908919

Browse files
authored
Merge pull request #56406 from nextcloud/backport/56255/stable31
[stable31] make failed availability check apply in the same request
2 parents f92244d + edb370d commit 0908919

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
@@ -710,7 +710,7 @@ private function getLockLogger(): ?LoggerInterface {
710710
}
711711

712712
/**
713-
* @return array [ available, last_checked ]
713+
* @return array{available: bool, last_checked: int}
714714
*/
715715
public function getAvailability(): array {
716716
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
@@ -150,4 +150,30 @@ public function testAvailableThrow(): void {
150150

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

0 commit comments

Comments
 (0)