Repository navigation
Description
Context
the current design of the event loop is holding us back a bit. First, a small history of how it evolved:
- at the beginning, mio 0.5 required that we provide objects that implement a
Handlertrait, that mio's internal loop would call. It was a bit annoying to use, so mio 0.6 moved instead to aPollobject that we would call in a loop. So creating sessions was a bit easier, but the way sessions were working was still a bit based on thatHandlertrait - we had one thread(then one process) with one event loop per proxy type (HTTP, HTTPS or TCP) because it was a bit easier to write than everything in the same loop
- with merge all the event loops #144 the event loops were merged, by separating the token space in 3. The event loop holds a
http::Proxy, atcp::Proxy, and aHttpsProvider, an enum that contains either a rustls based proxy, or a rustls proxy and an openssl proxy, depending on theuse-opensslcargo feature. Each proxy was holding a map of token -> session (they were called "client" at the time) - in 45012b8 I introduced a
ProxyClienttrait that allowed storing all the clients in one slab. I could cast them to their real session type with methods likeas_http()oras_tcp(), to be able to call the corresponding proxy'sconnect_to_backendmethod with the session as argument - in 25fcaf9 I added a way for a proxy to listen on multiple ports (before that, it was possible for TCP, but not for HTTP and HTTPS). A listen socket was stored in the same slab as the sessions, with protocol marked as
HTTPListen,TCPListen, etc. That way we can call the corresponding proxy'saccept()method
So the current design might seem awkward, because it evolved through multiple different ideas and needs.
I think it is time to change it again, because we might want to add more proxy types (like the peek-sni one I'm working on in a branch), and also because it is hard to modify and causes a lot of code duplication without reducing coupling.
So here's where we are now:
- the
Serverstruct holds the event loop. It has the following members:sessions: Slab<Rc<RefCell<ProxySessionCast>>,SessionToken>, hiding sessions and listeners behind aProxySessionCasthttp: http::Proxy: HTTP proxytcp: tcp::Proxy: TCP proxyhttps: HttpsProvider: holds either a openssl or rustls based HTTPS proxy
Serverwill call theProxySessionCast::protocol()method to get the kind of session or listener, then cast it and pass it to the corresponding proxy (to callaccept()orconnect_to_backend)- a
ProxyimplementsProxyConfiguration(the remnant of the mio 0.5 trait), and holds a list ofListener, passes configuration information to them, and provides app_id to backend routing information - a
Listenerholds a listen socket, a request router (to get an app id from a request) and some proxy specific info (like certificates)
At some point I was worried about the cost of dynamic dispatch for sessions and listeners, but the current design is probably even slower than that, so I think we could get better results by abstracting things behind trait objects.
Proposal
- a
Proxykind would only be able to create aListener, and hold some configuration information common to all its listeners - a
Listenerwould hold a link to its proxy. Either by making theProxyan object with'staticlifetime (since all listeners would necessarily have a shorter life, and it can live for the whole application's run), or holding it in aRc - a
Listeneris stored in the event loop's slab along other sessions, and has aprocess_eventsmethod that can be called (no special case for listeners). Potential problem: the accept queue andcan_acceptbool are both managed by the shared event loop. But it might be better to manage an accept queue per listener. And instead of anaccept_readyqueue, we would put the listeners in the samereadyqueue as sessions - a
Listenerholds an access to themio::Pollobject. Storing it into aRc? - a
Listenercan create aSessionin itsprocess_eventsmethod - a
Listenerhas aconnect_to_backendmethod that access thePollobject and theProxy's configuration information - a
Sessionwill hold aRcof itsListener, and be able to call it directly, instead of returning an order that would be caught in the event loop'sinterpret_client_ordermethod and then redispatched to theProxy. That point will simplify the design a lot
So with the new design, here's how the event loop would work:
- deref the
Rc<RefCell<Poll>>to call itspoll()method and get a list of tokens and events - get the
Rc<RefCell<ProxySessionCast>>(maybe renameProxySessionCastto something else) corresponding to the token, call itsprocess_eventsthrough dynamic dispatch - the
process_eventscould deref theRc<RefCell<Poll>>if aListeneraccepts a connection, or if aSessiontries to connect to a backend, but there should be no conflict, since we do not hold it for long - this will add one more field per listener, but there will not be a large number of them (65535 at most), and one more field per session. Those are already pretty large, but they hold a
Weakref to the buffer pool, and we could replace that one (since theListenercould hold the buffer pool ref)
I'm still a bit worried that this will make a lot of pointer dereferences, but this is clearly not the biggest overhead there is in sozu right now, and a nicer design is really needed right now to ship more interesting features
Activity
Geal commentedon Jun 5, 2019
probably related to #444 and #448
Geal commentedon Jun 6, 2019
we cannot completely decouple the event loop from the proxys internal behaviour, because methods like
create_sessionandconnect_to_backendstill need to reserve a slot in the sessions slab. Should that slab be accessible from proxy instances as well?Geal commentedon Jun 13, 2019
the refactoring is now mostly done and apparently does not break anything (except certificates queries for now).