Servo currently relies on synchronous networking throughout the engine. This design is based around asynchronous Sender and synchronous Receiver objects; each network request occurs in a separate thread, and it sends the response to the consumer in a series of messages. In theory the consumer could use non-blocking reading (ie. try_recv), but in practice all consumers are written as follows:

loop {
    match input_port.recv() {
        ProgressMsg::Payload(data) => {
            //...do something with data
        }
        ProgressMsg::Done(..) => break,
    }
}

Since we recently gained the ability to safely hold references to DOM objects across async operations, the time is ripe to rip out all unnecessary synchronous consumers and replace them with something better.

There are two ways we can go about this - we could continue with our entirely poll-based system, or switch to a push-based model like Gecko.


1) Poll-based

This model is quite simple, and requires the fewest changes. After a consumer initiates a network request, instead of immediately sitting in a recv() call, it dispatches a runnable which looks like this:

struct HTMLScriptElementLoader {
    element: Trusted<HTMLScriptElement>,
}

impl Runnable for HTMLScriptElementLoader {
    fn handler(self: Box<HTMLScriptElementLoader>) {
        let element = element.to_temporary().root();
        element.r().try_remote_response();
    }
}

impl<'a> HTMLScriptElementHelpers for JSRef<'a, HTMLScriptElement> {
    fn try_remote_response(self) {
        match self.remote_load.try_recv() {
            Ok(msg) => //process available data/completion notification
Err(TryRecvError::Empty) => //redispatch runnable to try again later
            Err(_) => //handle network error
        }
    }
}

There are changes here that could make this more effective (ie. have a push notification for header availability, then switch to polling); the obvious downside is that a tab's event loop will be cluttered with constant attempts to poll for incomplete network responses during page load.


2) Push-based

I propose a model analogous to Gecko's nsIRequestObserver/nsIStreamListener interfaces that will be implemented by consumers of network responses - this provides asynchronous notifications for response header availability, incremental response payloads, and response completion. There are three different ways of implementing this:


2.1) Abandon channels; send buffers between tasks via runnables

Consumer JSRef<T> initiates a network request and sends a Trusted<T>. JSRef<T> also implements the AsyncReceiver trait, consisting of:
- headers_available(Metadata)
- data_available(Vec<u8>)
- response_complete(Result<(), ()>)

Consumers must process or store each argument to any data_available AsyncReceiver method calls. This implementation is basically the same as Gecko's design.


2.2) Select-alike

Consumer JSRef<T> initiates a network request and sends a Trusted<T>. JSRef<T> also implements the AsyncReceiver trait, consisting of:
- headers_available(LoadResponse)
* consumer stores LoadResponse.progress_port (Receiver<ProgressMsg>) for response payload
- data_available()
* consumer calls recv() on stored progress_port - this notification indicates that a corresponding send() has already occurred, so the operation will not block. * an optimization possible is to include a shared count of sends that have occurred while the notification runnable was waiting to be processed. This ensures only one runnable is enqueued for a given network response at any given time.
- response_complete(Result<(), ()>)

This design is similar to the select operation - the data will not be lost if the notification is ignored, as Sender/Receiver pairs buffer messages. Consumers are given the ability to choose optimal reading patterns; synchronous, blocking reading is still possible.


2.3) Shared response buffers

Consumer JSRef<T> initiates a network request and sends a Trusted<T> and an Arc<Mutex<Option<Response>>, where Response is:

enum Reponse {
    HeadersOnly(Metadata),
    Complete(Metadata, Vec<u8>),
    Error(Metadata, Vec<u8>)
}

The consumer also implements the AsyncReceiver trait, consisting of:
- headers_available()
- data_available(u64, u64)
- response_complete()

The network request task updates the shared memory in a safe manner (due to the Arc and Mutex guards) every time something happens, and dispatches a notification to the consumer so it's clear that an update has occurred. This makes writing consumers that do not care about progressive updates very simple.


This is the extent of the possibilities that I've come up with, and I don't have a clear sense of the optimal one here. I know that Gecko has had trouble with networking-related runnables flooding the event loop in the past, which could suggest that a scheme that minimizes runnables would be worth investing in. I'd appreciate your thoughts!

Cheers,
Josh
_______________________________________________
dev-servo mailing list
[email protected]
https://lists.mozilla.org/listinfo/dev-servo

Reply via email to