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
Use new Poll class for filebrowser contents polling. #6305
Conversation
Thanks for making a pull request to JupyterLab! To try out this branch on binder, follow this link: |
packages/filebrowser/src/model.ts
Outdated
let date = new Date().getTime(); | ||
if (date - this._lastRefresh > refreshInterval) { | ||
return this.refresh(); | ||
} |
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.
All of this logic can go away with the new Poll class, I think. It's sufficient to just call the update method. When we want to have the poll immediately refresh, we call the poll's refresh method.
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.
Ah, good point. It also does some logic around a minimum refresh interval, which I tried to keep intact.
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.
We used to have a minimum interval option in Poll, but it didn't handle manual refreshes (it assumed a manual refresh was always immediate). Perhaps we add a minimum refresh interval to Poll for this debouncing. I think it would be pretty straightforward - the poll refresh() method would just schedule using logic like here.
So this logic isn't really a true debounce, in that it doesn't schedule a refresh according to MIN_REFRESH, it simply ignores requests if they are too soon after the last tick. That's perhaps a little frustrating - you click and click and click, and if you stop clicking before MIN_REFRESH, it's like you never clicked at all.
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.
actually, it's a bit more complicated for Poll, since we really want to debounce the callback (and every time we are about to call the callback, we check to see if we should go into standby, etc.). So that means the Poll should really be storing the last time it called the callback and make sure it's not calling the callback more often than it should.
packages/filebrowser/src/model.ts
Outdated
@@ -104,7 +104,23 @@ export class FileBrowserModel implements IDisposable { | |||
}; | |||
window.addEventListener('beforeunload', this._unloadEventListener); | |||
this._scheduleUpdate(); |
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.
Something should be done with _scheduleUpdate
too - that should probably be going away with the conversion to using Poll.
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.
Looks like the refresh()
method may need some work too.
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.
_scheduleUpdate
has some logic around minimum refresh intervals, which I was endeavoring to leave intact for now. This is triggered upon fileChanged
signals. I'm not aware of the history of that logic, but had thought that maybe there was some debouncing that was necessary.
If not, I'm happy to simplify this further, was just starting out with a more conservative approach.
1604811
to
07d7b4d
Compare
if (date - this._lastRefresh > MIN_REFRESH) { | ||
void this.refresh(); | ||
} else { | ||
this._requested = 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.
ah, so it actually was doing a debounce, in that if we tried to schedule an update too soon, it would note it and follow up with an update at some point in the future.
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.
Yes, it was doing that.
Note, however, that the user clicking refresh()
circumvents the polling all-together, so they get immediate feedback.
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, the original intent was for the auto-poll to not happen too soon after a user or programmatically triggered poll.
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.
Then we should be okay with deleting the logic entirely, then. If a user clicking on the refresh button calls the poll refresh, the poll knows about the refresh and its interval starts over again.
Is "refresh" the user-facing function to refresh the browser? If so, I think we should change refresh to call the poll refresh, and move the current |
I think if we wanted to know the last time it was run that could be a property of the Poll object, but we aren't currently using that info iirc. |
Okay, sounds to me like the consensus is to completely remove the special logic and use a vanilla poll. If we find we still want some of the richer behavior around minimum poll intervals or storing the last-polled-time, we can add them to |
packages/filebrowser/src/model.ts
Outdated
@@ -239,8 +228,7 @@ export class FileBrowserModel implements IDisposable { | |||
* Force a refresh of the directory contents. | |||
*/ | |||
refresh(): Promise<void> { | |||
this._lastRefresh = new Date().getTime(); | |||
return this.cd('.'); | |||
return this._poll.refresh(); |
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.
You probably want to refresh, then return the poll's tick promise. The refresh just schedules the refresh, the actual action happens at the end of the tick.
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.
Or just make an async function like the tests:
await this._poll.refresh();
await this._poll.tick;
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.
Ah, thanks, I had misunderstood the function signature.
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.
We discussed making the refresh return the tick promise, but then it would be inconsistent with the other scheduling functions like start
, etc. It would be convenient, though.
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.
We should at least make a note in the refresh method about this subtlety.
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.
It looks like Poll
has some logic around pending requests in which new calls to refresh()
supersede older ones. That is not really what we want here, since calls to FileBrowserModel.refresh()
can depend on a previous cd
(which is what #5224 was fixing). I suspect this is a case that is not really intended to be addressed by the Poll
functionality, where invocations of its actions are more-or-less memory-less.
We can get around this by not calling poll.refresh
in model.refresh
, and relying on the custom logic in cd
around pending paths, but it's not particularly clear.
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.
Roughly, the order I think should be (IIRC):
- Call poll.refresh
- poll.refresh calls schedule async
- poll.schedule resolves the current poll state (awaiting current tick promise resolve, which should happen after every other thing that was waiting on the current tick)
- poll.schedule then requests an animation frame to run the poll action (but does not await it).
- poll.schedule returns
At this point the await poll.refresh should return, and you await the poll.tick promise. The animation frame triggers, which awaits the poll action, then poll.schedules a 'resolved' state for the normal poll interval, and as part of the poll schedule, it resolves the poll.tick promise you were awaiting. The poll.tick promise is resolved with the poll instance, so you can check the poll state (which should be the 'resolved' state that was scheduled).
Does that help?
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.
so (await poll.tick()).state.payload
should be the value returned from your factory function.
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.
Roughly, the order I think should be (IIRC):
* Call poll.refresh
This was happening as expected.
* poll.refresh calls schedule async
Also happening as expected.
* poll.schedule resolves the current poll state (awaiting current tick promise resolve, which should happen after every other thing that was waiting on the current tick)
I don't think this is occurring as described, in particular, the resolution of the current tick does not seem to wait on the factory function:
jupyterlab/packages/coreutils/src/poll.ts
Lines 400 to 403 in 8c72b12
// Emit ticked signal, resolve pending promise, and await its settlement. | |
this._ticked.emit(this.state); | |
pending.resolve(this); | |
await pending.promise; |
* poll.schedule then requests an animation frame to run the poll action (but does not await it).
Agreed.
* poll.schedule returns
Agreed.
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 don't think this is occurring as described, in particular, the resolution of the current tick does not seem to wait on the factory function:
The poll action happens in the execute, and after the factory is run and the result resolves, then schedule is called to schedule a resolve state. That's why the tick happens after the factory runs.
Ooh, kind-of-confusing test from #5224 paying off. |
previous invocations of refresh, breaking chained directory changes.
The poll design is such that you should always await the tick to ensure the last action was accomplished, otherwise things can preempt it (for example, since promise chains use microtasks, which are scheduled before tasks like requestAnimationFrame, doing: Was it still having an issue when you awaited the poll.tick promise in the filebrowser refresh method? |
Yes, awaiting the |
this._scheduleUpdate(); | ||
this._startTimer(); | ||
this._poll = new Poll({ | ||
factory: () => this.cd('.'), |
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 think the problem is really that this factory function should be async. It's not waiting for the cd to finish.
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.
No, that was an incorrect comment. It is returning a promise, so that should be good.
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.
It is async, since this.cd('.')
returns a promise. In some of my testing it was explicitly marked as async
, which made no difference.
Was the difference that https://github.com/jupyterlab/jupyterlab/pull/5224/files#diff-d1e4e18fcba11094ca3a885e7d8b9d3aR273 just returned |
So I think a consequence of using poll for the model refresh is that you can call refresh all you want in a task, and only the last one will trigger a server request in an upcoming task. |
No, the expected behavior is that it is first in the root directory, then in |
Why is I'm a bit surprised you get two |
The original problem of #4009 was that our initial fetch of the directory contents was called in the constructor and not |
So |
I see your latest commit changes this. What is your reasoning now? |
* #### Notes | ||
* The returned promise resolves after the tick is scheduled, but before | ||
* the polling action is run. To wait until after the poll action executes, | ||
* await the `poll.tick` promise: `await poll.refresh(); await poll.tick;` |
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.
👍
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.
Awesome, thank you @ian-r-rose. Looks good.
👍
I'd like to take one more pass at figuring out the "out-of-order-cd" test before merging. |
Since the poll emits a signal every tick, a client can keep track of the last tick that matches whatever criterion the client cares about, e.g., if the tick was successful ( Also, |
Another way to think about it is |
@afshin, where did you get the time machine? |
Okay, I think this is good to go. @jasongrout I took a closer look at the |
+29, -62 lines! Hooray for cleaning out code! |
Code looks good, thanks! I also tested this in binder and it seemed to work well there too. |
Uses the
Poll
class for the filebrowser refresh scheduling.References
Fixes #6157.
Code changes
Internal changes to polling logic of the filebrowser. Replaces some crude backoff logic with the smarter
Poll
.User-facing changes
Some differences in how polling backs off upon failures to connect, but the user would have to be looking very hard to see it.
Backwards-incompatible changes
None