SimonDanisch / Bonito.jl

Serving JS to the browser
MIT License
204 stars 29 forks source link

user-defined CLEANUP_TIME for sessions that are NOT soft-closed #234

Closed bjarthur closed 1 week ago

bjarthur commented 2 months ago

currently it is hard-coded at 20 seconds, which is a problem for an app i have which takes longer than that to render. it would be nice to have a similar mechanism as exists for soft-closed sessions for the user to redefine this limit. happy to submit a PR which replaces 20 with CLEANUP_TIME[], or should another observable be created and used for non-soft-closed sessions instead?

bjarthur commented 1 month ago

so for my use case, i can avoid the 404 error with Bonito.set_cleanup_time!.

for reasons i don't understand, this "soft" closes the websockets, which means that the session and route are not closed immediately, but rather at some subsequent time. it's not clear to me why two different code paths are needed here.

since i have a workaround, i've closed my attempt to simplify it. still curious what the use cases are for hard vs soft closing.

SimonDanisch commented 1 month ago

I am not sure what your problem is. If you don't use set_cleanup_time, you should not run into this at all. Soft closed is (mainly) used for VSCode, to switch between plots in the plotpane, which closes the connection to the session, which closes the whole thing for good - reconnecting will create a completely new session. With set_cleanup_time(1) it waits for one hour before closing the session, to allow the plotpane to reconnect when navigating back to an old plot, with any old state being intact. It's not documented since it's not 100% thought through and was added in a hurry. If you have problems with connections closing before Julia is done compiling or whatever, you may need to look into your websocket setup from the server. There are quite a few proxy's / servers, which have very low time limit before they close the websocket as unresponsive (10-20s sometimes). If your browser/server/proxy closes the websocket connection before Julia had a chance to reply, the session will indeed be lost before it's being displayed, which can be fixed by a soft close, but I'd say you should rather figure out why the websocket closes so quickly.

elseif !isopen(session)
    # if the session is not SOFT_CLOSED, closing time means creation time
    creation_time = session.closing_time
    # close unopend sessions after 20 seconds
    if time() - creation_time > 20
        push!(remove, handler)
    end
end

This part is for when the browser never connects to a session - taking more than 20s for this is really unusual, but I guess we should still not hard code it and could give it more slack ;)

bjarthur commented 1 month ago

sorry, should've explained my problem in more detail. i serve a WGLMakie App with Bonito. there are a couple dozen blocks and plots and the data displayed therein takes about a minute to compute, even when i've used PackageCompiler.

google chrome returns a 404 error with stock bonito. but if i simply change that 20 to a 60, all is good. using set_cleanup_time also works as i just discovered recently too.

i don't know how to change a browser's timeout limit on a websocket session. even if that's configurable, i don't want to have to instruct my users to do that.

so are you saying that soft close is for when you want to delay closing a session that has been successfully established, and hard close is for when it was never established? i still don't understand the difference at all, am content with the set_cleanup_time workaround i'm now using, but would welcome a way to configure the hard-coded 20 if that's the better way to do it.

SimonDanisch commented 1 month ago

Oh maybe we measure the 20s incorrectly from session creation, and not from when the session got rendered and is ready to connect...Then 20s would indeed be too little and compile time etc would count into that!