Commit ea90a03
ENG-9028: Move event queue to backend (#6267)
* Add StateToken[TOKEN_TYPE] for flexible state manager
Special case access patterns for BaseState retrieval to allow for other types
of state that have different semantics.
* Use StateToken with redis and memory managers
Update all unit test cases to use the new StateToken / BaseStateToken API
* disambiguate class name
* handle legacy tokens passed to app.modify_state
* Add EventContext module
* Move BaseState event processing to reflex.ievent.processor package
Create the EventProcessor class to manage the backend event queue.
Move event processing logic out of the BaseState and into a separate module.
* Overhaul EventProcessor lifecycle
* No tasks start until `.start()` is called
* add graceful shutdown timeout to allow tasks to finish before cancellation
* use more keyword only parameters
* move BaseState-specific processing to new BaseStateEventProcessor subclass
* add test fixtures for `mock_event_processor` that can process simple registered events
* Fix test_app to use BaseStateEventProcessor
make Event.substate_token no longer work, because we're deprecating `token` as
an Event field, so we cannot rely on it under the covers.
* Avoid handling the same exception multiple times in EventProcessor
* Attach cls to setter event handlers
* fix test_app, test_state and friends
Use the new mock_base_state_event_processor fixture to process arbitrary events
and assert on emitted events or deltas.
* ENG-9198: implement ContextVar-based registry for BaseState and EventHandler
This allows better control over which states and events are part of a given app
and avoiding true global variables makes cleanup and testing much simpler.
* Remove `token` field from Event
* Clean up frontend Event and StateUpdate
remove null/default fields when serializing Event from the frontend and
StateUpdate from the backend.
* remove get_app dependency from get_state and background tasks
* Remove remaining get_app / mock_app dependency from tests
Fix issue with background task delta calculation not starting from the root state
* EventContext inherits from BaseContext
remove extra `event_context` ContextVar being passed around
* additional fixups
* apply changes to migrated files separately
* add missing import
* remove pyleak integration from base_state_processor
* EventProcessor.enqueue_stream_delta and task Future
EventProcessor.enqueue now returns a Future that tracks the completion of the
event (and can be used to cancel the event)
EventProcessor.enqueue_stream_delta overrides the default emit_delta
implementation and instead yields deltas directly to the caller as the event is
processing.
* Adapt upload endpoint to new EventProcessor
* Fix test_expiration.py and other new state tests
* Fix upload tests for new EventProcessor fixtures
* add OPLOCK_ENABLED state_manager.close to tests
* state.js: pass around params as a ref
The function () => params.current baked inside the ensureSocketConnected
function was getting a stale reference and the early events (hydrate, on load,
client state) were missing the query parameters in their router_data and thus
on_load was not working correctly.
* registry: substate tracking and stateful component cache
Store the state_full_name to substate mapping in RegistrationContext
Make it easier to register / re-register select states and event handlers in a
new RegistrationContext
Store StatefulComponent cached components in RegistrationContext for easier
resetting/dropping after compilation or for use in testing.
* close old locks in disk/memory state manager
* Remove state_manager from AppHarness
Update all associated tests to make assertions using the browser/app and not
attempting to fetch the backend state directly.
This makes the tests more robust, reduces state_manager related hacks, and
makes the tests easier to eventually migrate to an external process using
granian where direct state_manager access will not be available.
* Move reflex._internal to reflex_core._internal
* move reflex.ievent to reflex_core._internal.event
* replace "reload" functionality with internal rehydration
instead of telling the frontend to reload, just hydrate and run on_load
internally before processing the user's requested event.
* incldue coverage from subpackages
raise coverage bar back up to 72 at least
* remove simulated pre-hydrated states
* Add unit test cases for new registry/context/processor modules
Add unit test cases for reflex.istate.manager.token
* Use correct token in enqueue_stream_delta
Use the delta's token when emitting to the processor queue.
Return after emitting to the processor queue so the caller does not get deltas from unrelated tokens.
Co-authored-by: greptile-apps[bot] <165735046+greptile-apps[bot]@users.noreply.github.com>
* Fix StateToken.deserialize implementation
Co-authored-by: greptile-apps[bot] <165735046+greptile-apps[bot]@users.noreply.github.com>
* Update packages/reflex-core/src/reflex_core/_internal/event/processor/base_state_processor.py
Co-authored-by: greptile-apps[bot] <165735046+greptile-apps[bot]@users.noreply.github.com>
* Fix StateToken mismerge (Thanks greptile)
Remove TODO now that issue is created in repo.
* move EventChain import to avoid circular dep
* fix StateToken deserialize tests
* Python 3.11 and 3.12 compatibility
* Vendor the new async iterator `asyncio.as_completed` for < 3.13
* Alternative Queue.shutdown mechanism for < 3.13
* Alternative to `Task.get_context` for < 3.12
* from __future__ import annotations for new modules (so TYPE_CHECKING imports work)
* bug with cls super() call on dataclass with slots=True
* typing_extensions.deprecated
* py3.10 Self compat
* py3.10: typing_extensions Self
* ugh more py3.10 Self compat
* fix reflex_core -> reflex import
* Handle py3.11 compatible queue shutdown better
Join the queue that we None'd out to make sure all the tasks have flushed.
Fix some import issues being reported on my branch.
* state.js: pump the queue in processEvent
If there is another event to process, chain to processEvent
* AppHarness: pre-register SharedState so it's available in the base RegistrationContext
* BaseStateEventProcessor: emit deltas before enqueuing events
* set RegistrationContext in ASGI middleware
Ensure that the RegistrationContext associated with the App is set in the task
that handles an ASGI request.
* EventFuture: tracks execution of chained events
roll up chained events for use with enqueue_stream_deltas for HTTP invocation
of events.
* Add BaseState to reflex_core.event namespace for docgen
EventHandler has a BaseState field, so it needs this reference to generate the
proper documentation (and basically not crash)
* EventProcessor.enqueue only accepts a single Event
Add new `enqueue_many` function for queuing multiple events.
This allows each enqueued event to get its own EventContext/txid and
Future/Task tracking.
Update tests to wait for Future returned by `enqueue` to ensure processing has
completed before making any assertions.
* attach the registration_context_middleware in App.__call__
We don't want to set it from a lifespan task, because once the middleware is
added, it cannot be readded again. So if the same ASGI gets started and
stopped, it will throw an error.
Does this happen much in real life? No. But one AppHarness test was hitting it,
and this is technically more correct.
* test_connection_banner: use CDP to simulate network offline
Instead of killing and restarting the server, which is slower and more
annoying, just temporarily restrict network traffic in the browser.
* reflex_core.event: provide BaseState as a namespace property
Avoid other weird circular import issues that occur when the `sys.modules`
record for the module is not actually the module namespace.
* Track EventFuture children
Ensure yielded event execution ordering, by waiting for sibling EventFuture for
non-background tasks before processing.
* use py3.11 compatible super() for dataclasses with slots
* Only process one non-backend event per token
* Fix event order assertions in test_event_chain
* py3.11 super() fix again
* Move _registration_context_middleware to top of asgi stack
And don't register it as middleware, because it's not middleware technically
(even though it worked the way it was).
* Move registration context middle to not quite the top level app.
The returned top level app should always continue to be a Starlette instance
for compatibility.
* Add cache_key and lock_key attributes to StateToken
* Use cache_key and lock_key in StateManagerDisk and StateManagerRedis
* update pyi_hashes
* make reflex_base.event a package
* Get rid of reflex_base._internal namespace
* test_upload: extend sleep before cancellation
increase chances of actually getting a single chunk processed before the cancel
takes place.
* re-add fix_events token param
* Add StateManager.state property as a compat shim
* deprecate StateUpdate.final (instead of removal)
* Support legacy token format in StateManager implementations
* add test case for StateManager legacy str tokens
* ignore QueueShutDown when shutting down queue
* optimize test_event_processing benchmark
avoid overhead in stop() and join() paths
* asyncio.QueueShutDown was only added in 3.13+
* Add deprecated typing hints for passing str token to StateManager
* Move overloads outside of if TYPE_CHECKING
---------
Co-authored-by: greptile-apps[bot] <165735046+greptile-apps[bot]@users.noreply.github.com>1 parent 8edcb7b commit ea90a03
File tree
71 files changed
+6501
-2758
lines changed- packages
- reflex-base/src/reflex_base
- .templates/web/utils
- components
- constants
- context
- event
- processor
- plugins
- utils
- reflex-components-core/src/reflex_components_core/core
- reflex
- app_mixins
- istate
- manager
- utils
- tests
- benchmarks
- integration
- units
- istate
- manager
- middleware
- reflex_base
- context
- event
- processor
Some content is hidden
Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.
71 files changed
+6501
-2758
lines changedLines changed: 59 additions & 86 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
40 | 40 | | |
41 | 41 | | |
42 | 42 | | |
43 | | - | |
44 | | - | |
45 | 43 | | |
46 | 44 | | |
47 | 45 | | |
| |||
203 | 201 | | |
204 | 202 | | |
205 | 203 | | |
206 | | - | |
207 | | - | |
208 | 204 | | |
209 | 205 | | |
210 | 206 | | |
211 | 207 | | |
212 | 208 | | |
213 | | - | |
| 209 | + | |
214 | 210 | | |
215 | 211 | | |
216 | 212 | | |
217 | 213 | | |
218 | 214 | | |
219 | 215 | | |
220 | 216 | | |
221 | | - | |
| 217 | + | |
222 | 218 | | |
223 | 219 | | |
224 | 220 | | |
225 | 221 | | |
226 | 222 | | |
227 | 223 | | |
228 | 224 | | |
229 | | - | |
| 225 | + | |
230 | 226 | | |
231 | 227 | | |
232 | 228 | | |
| |||
236 | 232 | | |
237 | 233 | | |
238 | 234 | | |
239 | | - | |
| 235 | + | |
240 | 236 | | |
241 | 237 | | |
242 | 238 | | |
243 | 239 | | |
244 | 240 | | |
245 | | - | |
| 241 | + | |
246 | 242 | | |
247 | 243 | | |
248 | 244 | | |
249 | 245 | | |
250 | 246 | | |
251 | | - | |
| 247 | + | |
252 | 248 | | |
253 | 249 | | |
254 | 250 | | |
255 | 251 | | |
256 | 252 | | |
257 | | - | |
| 253 | + | |
258 | 254 | | |
259 | 255 | | |
260 | 256 | | |
261 | 257 | | |
262 | 258 | | |
263 | | - | |
| 259 | + | |
264 | 260 | | |
265 | 261 | | |
266 | 262 | | |
267 | 263 | | |
268 | 264 | | |
269 | | - | |
| 265 | + | |
270 | 266 | | |
271 | 267 | | |
272 | 268 | | |
| |||
285 | 281 | | |
286 | 282 | | |
287 | 283 | | |
288 | | - | |
| 284 | + | |
289 | 285 | | |
290 | 286 | | |
291 | 287 | | |
| |||
299 | 295 | | |
300 | 296 | | |
301 | 297 | | |
302 | | - | |
| 298 | + | |
303 | 299 | | |
304 | 300 | | |
305 | 301 | | |
| |||
313 | 309 | | |
314 | 310 | | |
315 | 311 | | |
316 | | - | |
| 312 | + | |
317 | 313 | | |
318 | 314 | | |
319 | 315 | | |
| |||
322 | 318 | | |
323 | 319 | | |
324 | 320 | | |
325 | | - | |
| 321 | + | |
326 | 322 | | |
327 | 323 | | |
328 | 324 | | |
| |||
348 | 344 | | |
349 | 345 | | |
350 | 346 | | |
351 | | - | |
| 347 | + | |
352 | 348 | | |
353 | 349 | | |
354 | 350 | | |
| |||
375 | 371 | | |
376 | 372 | | |
377 | 373 | | |
378 | | - | |
| 374 | + | |
379 | 375 | | |
380 | 376 | | |
381 | 377 | | |
382 | | - | |
383 | 378 | | |
384 | 379 | | |
385 | 380 | | |
386 | 381 | | |
387 | 382 | | |
388 | 383 | | |
389 | 384 | | |
390 | | - | |
391 | | - | |
392 | | - | |
393 | | - | |
394 | 385 | | |
395 | 386 | | |
396 | 387 | | |
397 | 388 | | |
398 | 389 | | |
| 390 | + | |
| 391 | + | |
| 392 | + | |
| 393 | + | |
| 394 | + | |
| 395 | + | |
| 396 | + | |
399 | 397 | | |
400 | 398 | | |
401 | 399 | | |
402 | 400 | | |
403 | 401 | | |
404 | | - | |
405 | 402 | | |
406 | | - | |
407 | | - | |
408 | 403 | | |
409 | 404 | | |
410 | 405 | | |
| |||
413 | 408 | | |
414 | 409 | | |
415 | 410 | | |
416 | | - | |
417 | | - | |
418 | 411 | | |
419 | 412 | | |
420 | | - | |
421 | 413 | | |
422 | 414 | | |
423 | 415 | | |
| |||
431 | 423 | | |
432 | 424 | | |
433 | 425 | | |
434 | | - | |
435 | 426 | | |
436 | | - | |
437 | 427 | | |
438 | 428 | | |
439 | 429 | | |
| |||
487 | 477 | | |
488 | 478 | | |
489 | 479 | | |
490 | | - | |
| 480 | + | |
491 | 481 | | |
492 | 482 | | |
493 | 483 | | |
494 | | - | |
495 | | - | |
496 | | - | |
497 | 484 | | |
498 | 485 | | |
499 | 486 | | |
500 | | - | |
501 | 487 | | |
502 | 488 | | |
503 | | - | |
| 489 | + | |
504 | 490 | | |
505 | | - | |
| 491 | + | |
506 | 492 | | |
507 | | - | |
508 | | - | |
509 | | - | |
510 | | - | |
511 | | - | |
| 493 | + | |
| 494 | + | |
512 | 495 | | |
513 | 496 | | |
514 | 497 | | |
| |||
621 | 604 | | |
622 | 605 | | |
623 | 606 | | |
624 | | - | |
625 | | - | |
626 | | - | |
627 | | - | |
628 | | - | |
629 | | - | |
630 | | - | |
| 607 | + | |
631 | 608 | | |
632 | 609 | | |
633 | | - | |
634 | | - | |
| 610 | + | |
| 611 | + | |
635 | 612 | | |
636 | 613 | | |
637 | 614 | | |
| |||
650 | 627 | | |
651 | 628 | | |
652 | 629 | | |
653 | | - | |
654 | 630 | | |
655 | 631 | | |
656 | 632 | | |
657 | 633 | | |
658 | | - | |
659 | 634 | | |
660 | 635 | | |
661 | 636 | | |
| |||
667 | 642 | | |
668 | 643 | | |
669 | 644 | | |
670 | | - | |
671 | | - | |
672 | | - | |
673 | | - | |
674 | | - | |
675 | | - | |
676 | | - | |
677 | | - | |
678 | | - | |
| 645 | + | |
| 646 | + | |
| 647 | + | |
| 648 | + | |
| 649 | + | |
| 650 | + | |
| 651 | + | |
| 652 | + | |
| 653 | + | |
| 654 | + | |
| 655 | + | |
679 | 656 | | |
| 657 | + | |
680 | 658 | | |
681 | | - | |
682 | | - | |
683 | | - | |
684 | | - | |
685 | | - | |
| 659 | + | |
686 | 660 | | |
687 | 661 | | |
688 | 662 | | |
689 | | - | |
690 | | - | |
691 | | - | |
692 | | - | |
693 | | - | |
694 | 663 | | |
695 | 664 | | |
696 | 665 | | |
| |||
713 | 682 | | |
714 | 683 | | |
715 | 684 | | |
716 | | - | |
| 685 | + | |
| 686 | + | |
| 687 | + | |
| 688 | + | |
| 689 | + | |
| 690 | + | |
| 691 | + | |
| 692 | + | |
| 693 | + | |
| 694 | + | |
| 695 | + | |
717 | 696 | | |
718 | 697 | | |
719 | 698 | | |
| |||
919 | 898 | | |
920 | 899 | | |
921 | 900 | | |
922 | | - | |
| 901 | + | |
923 | 902 | | |
924 | 903 | | |
925 | 904 | | |
| |||
947 | 926 | | |
948 | 927 | | |
949 | 928 | | |
950 | | - | |
| 929 | + | |
951 | 930 | | |
952 | 931 | | |
953 | 932 | | |
| |||
958 | 937 | | |
959 | 938 | | |
960 | 939 | | |
961 | | - | |
962 | | - | |
963 | | - | |
964 | | - | |
965 | | - | |
966 | | - | |
967 | | - | |
| 940 | + | |
968 | 941 | | |
969 | 942 | | |
970 | 943 | | |
| |||
1028 | 1001 | | |
1029 | 1002 | | |
1030 | 1003 | | |
1031 | | - | |
| 1004 | + | |
1032 | 1005 | | |
1033 | | - | |
| 1006 | + | |
1034 | 1007 | | |
1035 | 1008 | | |
1036 | 1009 | | |
| |||
0 commit comments