New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add Loop::now() #208
Add Loop::now() #208
Conversation
* {@inheritdoc} | ||
*/ | ||
public function now(): int { | ||
return \uv_now($this->handle); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
uv_now
is a monotonic timer and not related to the current real time.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Right,that was my intention that will need to be clear through documentation, that Loop::now()
is not necessarily wall time, but the loop time in milliseconds.
The event loop should use a monotonic timer for now instead of |
lib/Loop/EvDriver.php
Outdated
private $now; | ||
|
||
/** @var bool */ | ||
private $nowUpdateNeeded = true; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
in the other loop-impls., this defaults to false
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oops, it should be the same to be consistent, though the initial value doesn't matter in this loop.
@kelunik Yes, I should have stated that |
lib/Loop/Driver.php
Outdated
* | ||
* @return int | ||
*/ | ||
abstract public function now(): int; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is a BC break.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ugh… I suppose so. We could add an ExtendedDriver
, but I'd rather wait for v3 then.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What about adding the methods in the implementation classes but not in the interface?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@staabm Then it can't be used, because the interface / abstract class doesn't cover it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Users which dont need to abstract the impl and use it at app level could use it.
Inside amp you couldn‘t
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The function is mostly useful in libraries, not in applications, so...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This patch breaks BC in the current form and can't be merged into the current version.
Not sure why this needs to be abstract. Can't we just simply use the
v3 then can make it abstract. But for now simply not requiring abstract should be just fine. |
c0dd52f
to
d9a937a
Compare
public function __construct() | ||
{ | ||
$this->handle = new \EvLoop; | ||
$this->nowOffset = (int) (\microtime(true) * self::MILLISEC_PER_SEC); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd even randomize it, so e.g. * random_int(1, 100) / 100
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.
public function __construct() | ||
{ | ||
$this->handle = new \EvLoop; | ||
$this->nowOffset = (int) (\microtime(true) * self::MILLISEC_PER_SEC); | ||
$this->now = \random_int(0, $this->nowOffset); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Any reason why $this->now
is updated here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Picking a random starting value for $this->now
. It was always 0, but this makes the value returned by Loop::now()
unpredictable.
We frequently use
microtime()
ortime()
to keep track of last activity time, update time, etc. These system calls are relatively expensive.Loop::now()
updates only on the first call in a tick (or inUvLoop
the functionuv_now()
is used).