Skip to content

Commit

Permalink
Limit number of concurrent preview generations
Browse files Browse the repository at this point in the history
Signed-off-by: Bowen Ding <dbw9580@live.com>
  • Loading branch information
dbw9580 committed Nov 4, 2020
1 parent b65d9eb commit 18eeb79
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 1 deletion.
40 changes: 40 additions & 0 deletions lib/private/Preview/Generator.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@

class Generator {

public const SEMAPHORE_ID_ALL = 0x0a11;
public const SEMAPHORE_ID_NEW = 0x07ea;

/** @var IPreview */
private $previewManager;
/** @var IConfig */
Expand Down Expand Up @@ -204,6 +207,34 @@ public function generatePreviews(File $file, array $specifications, $mimeType =
return $preview;
}

/**
* @param int $semId
* @param int $concurrency
* @return bool|resource the semaphore on success or false on failure
*/
public static function guardWithSemaphore(int $semId, int $concurrency) {
if (extension_loaded('sysvsem')) {
$sem = sem_get($semId, $concurrency);
sem_acquire($sem);
return $sem;
}
return false;
}

/**
* @param resource $sem
* @return bool
*/
public static function unguardWithSemaphore($sem) {
if (!is_resource($sem)) {
return false;
}
if (extension_loaded('sysvsem')) {
return sem_release($sem);
}
return false;
}

/**
* @param ISimpleFolder $previewFolder
* @param File $file
Expand Down Expand Up @@ -241,8 +272,13 @@ private function getMaxPreview(ISimpleFolder $previewFolder, File $file, $mimeTy
$maxWidth = (int)$this->config->getSystemValue('preview_max_x', 4096);
$maxHeight = (int)$this->config->getSystemValue('preview_max_y', 4096);

$previewConcurrency = $this->config->getSystemValueInt('preview_concurrency_new', 4);
$sem = self::guardWithSemaphore(self::SEMAPHORE_ID_NEW, $previewConcurrency);

$preview = $this->helper->getThumbnail($provider, $file, $maxWidth, $maxHeight);

self::unguardWithSemaphore($sem);

if (!($preview instanceof IImage)) {
continue;
}
Expand Down Expand Up @@ -407,6 +443,9 @@ private function generatePreview(ISimpleFolder $previewFolder, IImage $maxPrevie
throw new \InvalidArgumentException('Failed to generate preview, failed to load image');
}

$previewConcurrency = $this->config->getSystemValueInt('preview_concurrency_new', 4);
$sem = self::guardWithSemaphore(self::SEMAPHORE_ID_NEW, $previewConcurrency);

if ($crop) {
if ($height !== $preview->height() && $width !== $preview->width()) {
//Resize
Expand All @@ -429,6 +468,7 @@ private function generatePreview(ISimpleFolder $previewFolder, IImage $maxPrevie
$preview = $maxPreview->resizeCopy(max($width, $height));
}

self::unguardWithSemaphore($sem);

$path = $this->generatePath($width, $height, $crop, $preview->dataMimeType(), $prefix);
try {
Expand Down
9 changes: 8 additions & 1 deletion lib/private/PreviewManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,14 @@ private function getGenerator(): Generator {
* @since 11.0.0 - \InvalidArgumentException was added in 12.0.0
*/
public function getPreview(File $file, $width = -1, $height = -1, $crop = false, $mode = IPreview::MODE_FILL, $mimeType = null) {
return $this->getGenerator()->getPreview($file, $width, $height, $crop, $mode, $mimeType);
$previewConcurrency = $this->config->getSystemValueInt('preview_concurrency_all', 8);
$sem = Generator::guardWithSemaphore(Generator::SEMAPHORE_ID_ALL, $previewConcurrency);

$preview = $this->getGenerator()->getPreview($file, $width, $height, $crop, $mode, $mimeType);

Generator::unguardWithSemaphore($sem);

return $preview;
}

/**
Expand Down

0 comments on commit 18eeb79

Please sign in to comment.