panel icon indicating copy to clipboard operation
panel copied to clipboard

Implement ability to reuse session for initial render

Open philippjfr opened this issue 1 year ago • 1 comments

This is an experiment to see if we can improve the speed of handling the initial request. The basic idea behind this is that in most cases the initial page served to the user is identical, i.e. the template does not differ between sessions. As long as that holds true we can simply cache the first session for each endpoint and reuse that to render the template served to the user. In the background we can then still create a session for each user which the rendered template connects to.

The way this works is that we simply modify the token which tells the frontend which session it should connect to. So the steps are:

  1. User visits application
  2. The DocHandler uses a cached session to render the template
  3. We swap out the session_id in the token so that the frontend does not re-connect to the existing session
  4. When the frontend establishes the Websocket connection a new session is created

This will significantly reduce time to first render but also comes with some caveats and drawbacks:

  • Even though the initial render is faster establishing the websocket connection will be slower because we have simply postponed when the session is created
  • This will not work if the initial page render is dependent on some state, e.g. a query parameter determines the theme or a header/cookie is used to render the user into the header.

philippjfr avatar Jul 09 '22 15:07 philippjfr

Codecov Report

Merging #3679 (90a37e0) into main (0df3be5) will increase coverage by 0.03%. The diff coverage is 90.51%.

@@            Coverage Diff             @@
##             main    #3679      +/-   ##
==========================================
+ Coverage   83.17%   83.20%   +0.03%     
==========================================
  Files         263      264       +1     
  Lines       37234    37350     +116     
==========================================
+ Hits        30968    31076     +108     
- Misses       6266     6274       +8     
Flag Coverage Δ
ui-tests 39.65% <38.79%> (-0.01%) :arrow_down:
unitexamples-tests 73.26% <75.86%> (+0.01%) :arrow_up:

Flags with carried forward coverage won't be shown. Click here to find out more.

Impacted Files Coverage Δ
panel/tests/ui/io/test_server.py 73.52% <73.52%> (ø)
panel/io/server.py 76.63% <96.87%> (+1.17%) :arrow_up:
panel/tests/test_server.py 99.34% <97.29%> (-0.14%) :arrow_down:
panel/command/serve.py 35.95% <100.00%> (+0.26%) :arrow_up:
panel/config.py 68.11% <100.00%> (+0.83%) :arrow_up:
panel/io/state.py 72.72% <100.00%> (+0.09%) :arrow_up:
panel/tests/conftest.py 90.03% <100.00%> (+0.32%) :arrow_up:

... and 1 file with indirect coverage changes

:mega: We’re building smart automated test selection to slash your CI/CD build times. Learn more

codecov[bot] avatar Jul 09 '22 15:07 codecov[bot]