Skip to content

Commit

Permalink
add an option to set max retries
Browse files Browse the repository at this point in the history
  • Loading branch information
sj-i committed May 27, 2020
1 parent 5d9c3b9 commit f20bd67
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 3 deletions.
6 changes: 6 additions & 0 deletions src/Command/Inspector/GetCurrentFunctionNameCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,12 @@ public function configure(): void
's',
InputOption::VALUE_OPTIONAL,
'nanoseconds between traces (default: 1000 * 1000 * 10)'
)
->addOption(
'max-retries',
'r',
InputOption::VALUE_OPTIONAL,
'max retries on contiguous errors of read (default: 10)'
);
}

Expand Down
6 changes: 6 additions & 0 deletions src/Command/Inspector/GetTraceCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,12 @@ public function configure(): void
's',
InputOption::VALUE_OPTIONAL,
'nanoseconds between traces (default: 1000 * 1000 * 10)'
)
->addOption(
'max-retries',
'r',
InputOption::VALUE_OPTIONAL,
'max retries on contiguous errors of read (default: 10)'
);
}

Expand Down
17 changes: 15 additions & 2 deletions src/Command/Inspector/Settings/LoopSettings.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,19 +20,23 @@ class LoopSettings
{
private const SLEEP_NANO_SECONDS_DEFAULT = 1000 * 1000 * 10;
private const CANCEL_KEY_DEFAULT = 'q';
private const MAX_RETRY_DEFAULT = 10;

public int $sleep_nano_seconds;
public string $cancel_key;
public int $max_retries;

/**
* TraceLoopSettings constructor.
* @param int $sleep_nano_seconds
* @param string $cancel_key
* @param int $max_retries
*/
public function __construct(int $sleep_nano_seconds, string $cancel_key)
public function __construct(int $sleep_nano_seconds, string $cancel_key, int $max_retries)
{
$this->sleep_nano_seconds = $sleep_nano_seconds;
$this->cancel_key = $cancel_key;
$this->max_retries = $max_retries;
}

/**
Expand All @@ -51,6 +55,15 @@ public static function fromConsoleInput(InputInterface $input): self
throw LoopSettingsException::create(LoopSettingsException::SLEEP_NS_IS_NOT_INTEGER);
}

return new self($sleep_nano_seconds, self::CANCEL_KEY_DEFAULT);
$max_retries = $input->getOption('max-retries');
if (is_null($max_retries)) {
$max_retries = self::MAX_RETRY_DEFAULT;
}
$max_retries = filter_var($max_retries, FILTER_VALIDATE_INT);
if ($max_retries === false) {
throw LoopSettingsException::create(LoopSettingsException::MAX_RETRY_IS_NOT_INTEGER);
}

return new self($sleep_nano_seconds, self::CANCEL_KEY_DEFAULT, $max_retries);
}
}
2 changes: 2 additions & 0 deletions src/Command/Inspector/Settings/LoopSettingsException.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,10 @@
class LoopSettingsException extends CommandSettingsException
{
public const SLEEP_NS_IS_NOT_INTEGER = 1;
public const MAX_RETRY_IS_NOT_INTEGER = 2;

protected const ERRORS = [
self::SLEEP_NS_IS_NOT_INTEGER => 'sleep-ns is not integer',
self::MAX_RETRY_IS_NOT_INTEGER => 'max-retries is not integer',
];
}
2 changes: 1 addition & 1 deletion src/Command/Inspector/TraceLoopProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public function __construct(LoopBuilder $loop_builder)
public function getMainLoop(callable $main, LoopSettings $settings): Loop
{
return $this->loop_builder->addProcess(NanoSleepLoop::class, [$settings->sleep_nano_seconds])
->addProcess(RetryOnExceptionLoop::class, [10, [MemoryReaderException::class]])
->addProcess(RetryOnExceptionLoop::class, [$settings->max_retries, [MemoryReaderException::class]])
->addProcess(KeyboardCancelLoop::class, [$settings->cancel_key])
->addProcess(CallableLoop::class, [$main])
->build();
Expand Down

0 comments on commit f20bd67

Please sign in to comment.