ILIAS  release_8 Revision v8.19-1-g4e8f2f9140c
All Data Structures Namespaces Files Functions Variables Modules Pages
ILIAS\HTTP\Duration\CallbackDuration Class Reference
+ Inheritance diagram for ILIAS\HTTP\Duration\CallbackDuration:
+ Collaboration diagram for ILIAS\HTTP\Duration\CallbackDuration:

Public Member Functions

 stretch (callable $callback)
 
- Public Member Functions inherited from ILIAS\HTTP\Duration\Duration
 __construct (int $duration_in_ms)
 
 withIncrement (IncrementStrategy $increment)
 
 increment ()
 
 withDuration (int $duration_in_ms)
 
 getDuration ()
 

Additional Inherited Members

- Protected Attributes inherited from ILIAS\HTTP\Duration\Duration
const S_TO_US = 1_000_000
 number used for converting seconds (s) into microseconds (us/µ) or vise-versa. More...
 
const MS_TO_US = 1_000
 number used for converting miliseconds (ms) into microseconds (us/µ) or vise-versa. More...
 
IncrementStrategy $increment = null
 
int $duration_in_ms
 

Detailed Description

Author
Thibeau Fuhrer thibe.nosp@m.au@s.nosp@m.r.sol.nosp@m.utio.nosp@m.ns

Definition at line 25 of file CallbackDuration.php.

Member Function Documentation

◆ stretch()

ILIAS\HTTP\Duration\CallbackDuration::stretch ( callable  $callback)
Returns
mixed|null

Definition at line 30 of file CallbackDuration.php.

31  {
32  $start_time = microtime(true);
33  $halted = true;
34 
35  register_shutdown_function(static function () use (&$halted) {
36  if ($halted) {
37  throw new \LogicException("Callback could not be stretched because it halted the programm.");
38  }
39  });
40 
41  $return = $callback();
42  $halted = false;
43 
44  $elapsed_time_in_us = ((microtime(true) - $start_time) * self::S_TO_US);
45  $duration_in_us = ($this->duration_in_ms * self::MS_TO_US);
46 
47  if ($elapsed_time_in_us > $duration_in_us) {
48  throw new \RuntimeException("Execution of callback exceeded the given duration and could not be stretched.");
49  }
50 
51  if ($elapsed_time_in_us < $duration_in_us) {
52  usleep((int) round($duration_in_us - $elapsed_time_in_us));
53  }
54 
55  return $return;
56  }

The documentation for this class was generated from the following file: