serve
fastly::http::serve
Support for reusable sandboxes.
Normally, each incoming HTTP request spins up a new Compute instance. This provides good reproducibility properties... but it means that if an instance has expensive initialization, that initialization must be repeated for each request. If a service has an expensive initialization routine, reusable instances can provide an efficiency improvement (and a cost improvement!)
Timeouts
A Compute instance is not a general-purpose server; it does not and should not indefinitely wait for new requests. Therefore, the time spent waiting for a new request is bounded in three different ways.
At the platform level, there is a maximum time over which a
RequestPromisecan be unfulfilled. The timer starts when theRequestPromiseis created. After this timeout,RequestPromise::waitreturnsNextRequestError::EndOfSession, and the promise is no longer valid.This timeout is not publicly disclosed, and may change.
When a
RequestPromiseis created, the promise-level timeout can be configured inNextRequestOptions::timeout. The effective promise-level timeout is the minimum of the provided value and the platform-level timeout.Separately from the promise-level timeout,
wait_timeoutcan be used to time out a particularwaitcall. Ifwait_timeouttimes out while the promise is still valid,wait_timeoutwill returnRequestPromise::Timeout, indicating the promise can be retried.
Billing
When a WASM instance waits on a RequestPromise, it continues to consume memory and wall-clock
time. The memory is only freed when the WASM instance exits, as usual.
If your service is billed by WASM memory * wall-clock time, you will be billed for the usage while
waiting for a RequestPromise, even if the RequestPromise does not resolve to a request
(i.e. times out). You can limit this usage by timing out the RequestPromise, as described
above.
A WASM instance that is blocked waiting on a RequestPromise does not consume vCPU time.
Examples
123456789101112fn handler(_req: Request) -> Result<Response, Error> { Ok(Response::from_body("hello") .with_header("hello", "world!") .with_status(200))}
fn main() -> Result<(), Error> { Serve::new() .with_max_requests(5) .run(handler) .into_result()}Structs
| NextRequestOptions | Settings to use when waiting for a second (third, fourth, ...) request. |
| RequestPromise | A promise of a future Request from a customer. |
| Serve | Support for processing multiple requests from a single sandbox. |
| ServeSummary | A summary of information from running Serve. |
Traits
| HandlerResult | Trait for supporting more flexible return types for the handler passed to Serve::run. |
Enums
| NextRequestError | Errors that can occur when asking for or resolving a RequestPromise. |