зеркало из https://github.com/nextcloud/notes.git
retry if locked
This commit is contained in:
Родитель
8a24c98c07
Коммит
32de460dd1
|
@ -5,6 +5,7 @@ declare(strict_types=1);
|
|||
namespace OCA\Notes\Controller;
|
||||
|
||||
use OCA\Notes\Application;
|
||||
use OCA\Notes\Service\Util;
|
||||
|
||||
use OCP\AppFramework\Http;
|
||||
use OCP\AppFramework\Http\JSONResponse;
|
||||
|
@ -47,19 +48,7 @@ class Helper {
|
|||
|
||||
public function handleErrorResponse(callable $respond) : JSONResponse {
|
||||
try {
|
||||
// retry on LockedException
|
||||
$maxRetries = 5;
|
||||
for ($try=1; $try <= $maxRetries; $try++) {
|
||||
try {
|
||||
$data = $respond();
|
||||
break;
|
||||
} catch (\OCP\Lock\LockedException $e) {
|
||||
if ($try >= $maxRetries) {
|
||||
throw $e;
|
||||
}
|
||||
sleep(1);
|
||||
}
|
||||
}
|
||||
$data = Util::retryIfLocked($respond);
|
||||
$response = $data instanceof JSONResponse ? $data : new JSONResponse($data);
|
||||
} catch (\OCA\Notes\Service\NoteDoesNotExistException $e) {
|
||||
$this->logException($e);
|
||||
|
|
|
@ -155,7 +155,9 @@ class MetaService {
|
|||
|
||||
// warning: this is expensive
|
||||
private function generateContentEtag(Note $note) : string {
|
||||
return md5($note->getContent());
|
||||
return Util::retryIfLocked(function () use ($note) {
|
||||
return md5($note->getContent());
|
||||
}, 3);
|
||||
}
|
||||
|
||||
// this is not expensive, since we use the content ETag instead of the content itself
|
||||
|
|
|
@ -0,0 +1,20 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace OCA\Notes\Service;
|
||||
|
||||
class Util {
|
||||
public static function retryIfLocked(callable $f, int $maxRetries = 5, int $sleep = 1) {
|
||||
for ($try=1; $try <= $maxRetries; $try++) {
|
||||
try {
|
||||
return $f();
|
||||
} catch (\OCP\Lock\LockedException $e) {
|
||||
if ($try >= $maxRetries) {
|
||||
throw $e;
|
||||
}
|
||||
sleep($sleep);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
Загрузка…
Ссылка в новой задаче