Struct fastly::Backend

source ·
pub struct Backend { /* private fields */ }
Expand description

A named backend.

This represents a backend associated with a service that we can send requests to, potentially caching the responses received.

Backends come in one of two flavors:

  • Static Backends: These backends are created using the Fastly UI or API, and are predefined by the user. Static backends have short names (see the precise naming rules in Backend::from_name) that are usable across every session of a service.
  • Dynamic Backends: These backends are created programmatically using the experimental crate::experimental::BackendExt API. They are defined at runtime, and may or may not be shared across sessions depending on how they are configured.

To use a backend, pass it to the crate::Request::send mathod. Alternatively, the following values can be automatically coerced into a backend for you, without the need to explicitly create a Backend object, although they may all induce panics:

  • Any string type (&str, String, or &String) will be automatically turned into the static backend of the same name.

Using Static Backends

As stated at the top level, the following snippet is a minimal program that resends a request to a static backend named “example_backend”:

use fastly::{Error, Request, Response};

#[fastly::main]
fn main(ds_req: Request) -> Result<Response, Error> {
    Ok(ds_req.send("example_backend")?)
}

A safer alternative to this example would be the following:

use fastly::{Backend, Error, Request, Response};

#[fastly::main]
fn main(ds_req: Request) -> Result<Response, Error> {
    match Backend::from_name("example_backend") {
       Ok(backend) => Ok(ds_req.send(backend)?),
       Err(_) => {
            // custom backend failure response
            unimplemented!()
       }
    }
}

as this version allows you to handle backend errors more cleanly.

Validating Support for Dynamic Backends

Since dynamic backends are only enabled for some services, it may be particularly useful to ensure that dynamic backends are supported in your Compute@Edge service. The easiest way to do so is to try to create a dynamic backend, and then explicitly check for the crate::experimental::BackendCreationError::Disallowed code, as follows:

use fastly::{Backend, Error, Request, Response};
use fastly::experimental::{BackendExt, BackendCreationError};

#[fastly::main]
fn main(ds_req: Request) -> Result<Response, Error> {
    match Backend::builder("custom_backend", "example.org:993").finish() {
        Ok(backend) => Ok(ds_req.send(backend)?),
        Err(BackendCreationError::Disallowed) => {
           // custom code ofr handling when dynamic backends aren't supported
           unimplemented!()
        }
        Err(err) => {
           // more specific logging/handling for backend misconfigurations
           unimplemented!()
        }
    }
}

Implementations§

Get a backend by its name.

This function will return a BackendError if an invalid name was given.

Backend names:

  • cannot be empty
  • cannot be longer than 255 characters
  • cannot ASCII control characters such as '\n' or DELETE.
  • cannot contain special Unicode characters
  • should only contain visible ASCII characters or spaces

Future versions of this function may return an error if your service does not have a backend with this name.

Get the name of this backend.

Turn the backend into its name as a string.

Trait Implementations§

Create a new dynamic backend builder. Read more
Return the health of the backend if configured and currently known. Read more
Returns a copy of the value. Read more
Performs copy-assignment from source. Read more
Formats the value using the given formatter. Read more
Formats the value using the given formatter. Read more
The associated error which can be returned from parsing.
Parses a string s to return a value of this type. Read more
Feeds this value into the given Hasher. Read more
Feeds a slice of this type into the given Hasher. Read more
This method returns an Ordering between self and other. Read more
Compares and returns the maximum of two values. Read more
Compares and returns the minimum of two values. Read more
Restrict a value to a certain interval. Read more
This method tests for self and other values to be equal, and is used by ==.
This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
This method returns an ordering between self and other values if one exists. Read more
This method tests less than (for self and other) and is used by the < operator. Read more
This method tests less than or equal to (for self and other) and is used by the <= operator. Read more
This method tests greater than (for self and other) and is used by the > operator. Read more
This method tests greater than or equal to (for self and other) and is used by the >= operator. Read more

Auto Trait Implementations§

Blanket Implementations§

Gets the TypeId of self. Read more
Immutably borrows from an owned value. Read more
Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Should always be Self
The resulting type after obtaining ownership.
Creates owned data from borrowed data, usually by cloning. Read more
Uses borrowed data to replace owned data, usually by cloning. Read more
Converts the given value to a String. Read more
The type returned in the event of a conversion error.
Performs the conversion.
The type returned in the event of a conversion error.
Performs the conversion.