Skip to main content

Backend

Struct 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 Backend::builder 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 method. 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 service. The easiest way to do so is to try to create a dynamic backend, and then explicitly check for the crate::backend::BackendCreationError::Disallowed code, as follows:

use fastly::{Backend, Error, Request, Response};
use fastly::backend::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§

Source§

impl Backend

Source

pub fn from_name(s: &str) -> Result<Self, BackendError>

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.

Source

pub fn builder(name: impl ToString, target: impl ToString) -> BackendBuilder

Create a new dynamic backend builder.

The arguments are the name of the new backend to use, along with a string describing the backend host. The latter can be of the form:

  • "<ip address>"
  • "<hostname>"
  • "<ip address>:<port>"
  • "<hostname>:<port>"

The name can be whatever you would like, as long as it does not match the name of any of the static service backends nor match any other dynamic backends built during this session. (Names can overlap between different sessions of the same service – they will be treated as completely separate entities and will not be pooled – but you cannot, for example, declare a dynamic backend named “dynamic-backend” twice in the same session.)

The builder will start with default values for all other possible fields for the backend, which can be overridden using the other methods provided. Call finish() to complete the construction of the dynamic backend.

Dynamic backends must be enabled for this Compute service. You can determine whether or not dynamic backends have been allowed for the current service by using this builder, and then checking for the BackendCreationError::Disallowed error result. This error only arises when attempting to use dynamic backends with a service that has not had dynamic backends enabled, or dynamic backends have been administratively prohibited for the node in response to an ongoing incident.

Source

pub fn name(&self) -> &str

Get the name of this backend.

Source

pub fn into_string(self) -> String

Turn the backend into its name as a string.

Source

pub fn exists(&self) -> bool

Returns true if a backend with this name exists.

Source

pub fn is_dynamic(&self) -> bool

Returns true if this is a dynamic backend.

§Panics

If the name() of this backend does not match any of the backends associated with this service, this function will panic. Use exists() to explicitly check that this backend exists.

Source

pub fn get_host(&self) -> String

Returns the hostname for this backend.

§Panics

If the name() of this backend does not match any of the backends associated with this service, this function will panic. Use exists() to explicitly check that this backend exists.

Source

pub fn get_host_override(&self) -> Option<HeaderValue>

Returns the host header override when contacting this backend.

This method returns None if no host header override is configured for this backend.

This is used to change the Host header sent to the backend. For more information, see the Fastly documentation on host overrides here: https://docs.fastly.com/en/guides/specifying-an-override-host

Use BackendBuilder::override_host to set this for a dynamic backend.

§Panics

If the name() of this backend does not match any of the backends associated with this service, this function will panic. Use exists() to explicitly check that this backend exists.

Source

pub fn get_port(&self) -> u16

Get the port number of the backend’s address.

§Panics

If the name() of this backend does not match any of the backends associated with this service, this function will panic. Use exists() to explicitly check that this backend exists.

Source

pub fn get_connect_timeout(&self) -> Duration

Returns the connection timeout for this backend.

Use BackendBuilder::connect_timeout to set this for a dynamic backend.

§Panics

If the name() of this backend does not match any of the backends associated with this service, this function will panic. Use exists() to explicitly check that this backend exists.

Source

pub fn get_first_byte_timeout(&self) -> Duration

Returns the “first byte” timeout for this backend.

This timeout applies between the time of connection and the time we get the first byte back.

Use BackendBuilder::first_byte_timeout to set this for a dynamic backend.

§Panics

If the name() of this backend does not match any of the backends associated with this service, this function will panic. Use exists() to explicitly check that this backend exists.

Source

pub fn get_between_bytes_timeout(&self) -> Duration

Returns the “between bytes” timeout for this backend.

This timeout applies between any two bytes we receive across the wire.

Use BackendBuilder::between_bytes_timeout to set this for a dynamic backend.

§Panics

If the name() of this backend does not match any of the backends associated with this service, this function will panic. Use exists() to explicitly check that this backend exists.

Source

pub fn get_http_keepalive_time(&self) -> Duration

Returns the time to hold onto an idle HTTP keepalive connection after it was last used before closing it.

Use BackendBuilder::http_keepalive_time to set this for a dynamic backend.

§Panics

If the name() of this backend does not match any of the backends associated with this service, this function will panic. Use exists() to explicitly check that this backend exists.

Source

pub fn get_tcp_keepalive_enable(&self) -> bool

Returns true if TCP keepalives have been enabled for this backend.

Use BackendBuilder::tcp_keepalive_enable to set this for a dynamic backend.

§Panics

If the name() of this backend does not match any of the backends associated with this service, this function will panic. Use exists() to explicitly check that this backend exists.

Source

pub fn get_tcp_keepalive_interval(&self) -> Duration

Returns the time to wait in between sending each TCP keepalive probe to this backend.

Use BackendBuilder::tcp_keepalive_interval_secs to set this for a dynamic backend.

§Panics

If the name() of this backend does not match any of the backends associated with this service, this function will panic. Use exists() to explicitly check that this backend exists.

Source

pub fn get_tcp_keepalive_probes(&self) -> u32

Returns the time to wait after the last data was sent before starting to send TCP keepalive probes to this backend.

Use BackendBuilder::tcp_keepalive_probes to set this for a dynamic backend.

§Panics

If the name() of this backend does not match any of the backends associated with this service, this function will panic. Use exists() to explicitly check that this backend exists.

Source

pub fn get_tcp_keepalive_time(&self) -> Duration

Returns the time to wait after the last data was sent before starting to send TCP keepalive probes to this backend.

Use BackendBuilder::tcp_keepalive_time_secs to set this for a dynamic backend.

§Panics

If the name() of this backend does not match any of the backends associated with this service, this function will panic. Use exists() to explicitly check that this backend exists.

Source

pub fn is_ssl(&self) -> bool

Returns true if SSL/TLS is used to connect to the backend.

Use BackendBuilder::enable_ssl or BackendBuilder::disable_ssl to set this for a dynamic backend.

§Panics

If the name() of this backend does not match any of the backends associated with this service, this function will panic. Use exists() to explicitly check that this backend exists.

Source

pub fn get_ssl_min_version(&self) -> Option<SslVersion>

Returns the minimum TLS version for connecting to the backend.

This method returns None if SSL/TLS is not enabled for this backend.

Use BackendBuilder::set_min_tls_version to set this for a dynamic backend.

§Panics

If the name() of this backend does not match any of the backends associated with this service, this function will panic. Use exists() to explicitly check that this backend exists.

Source

pub fn get_ssl_max_version(&self) -> Option<SslVersion>

Returns the maximum TLS version for connecting to the backend.

This method returns None if SSL/TLS is not enabled for this backend.

Use BackendBuilder::set_max_tls_version to set this for a dynamic backend.

§Panics

If the name() of this backend does not match any of the backends associated with this service, this function will panic. Use exists() to explicitly check that this backend exists.

Source§

impl Backend

Source

pub fn healthcheck_builder(host: impl ToString) -> HealthcheckBuilder

Create a new health check builder for a dynamic backend.

The argument is a string describing the backend host on which to check health checks on. This can be of the form:

  • "<ip address>"
  • "<hostname>"
  • "<ip address>:<port>"
  • "<hostname>:<port>"

The builder will start with default values for all other possible fields for the health check, which can be overridden using the other methods provided. Use BackendBuilder::healthcheck to add the health check to a specific Dynamic Backend and call BackendBuilder::finish() to complete the construction of the dynamic backend.

Values for health checks have strict limits to prevent ill-use. If you create a health check with a value that breaks these limits, finish() will error with a BackendCreationError:InvalidHealthcheckValues

Trait Implementations§

Source§

impl BackendExt for Backend

Source§

fn is_healthy(&self) -> Result<BackendHealth, Error>

Return the health of the backend if configured and currently known. Read more
Source§

impl Clone for Backend

Source§

fn clone(&self) -> Backend

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Backend

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Display for Backend

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Eq for Backend

Source§

impl FromStr for Backend

Source§

type Err = BackendError

The associated error which can be returned from parsing.
Source§

fn from_str(s: &str) -> Result<Self, Self::Err>

Parses a string s to return a value of this type. Read more
Source§

impl Hash for Backend

Source§

fn hash<__H: Hasher>(&self, state: &mut __H)

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl Ord for Backend

Source§

fn cmp(&self, other: &Backend) -> Ordering

This method returns an Ordering between self and other. Read more
1.21.0 (const: unstable) · Source§

fn max(self, other: Self) -> Self
where Self: Sized,

Compares and returns the maximum of two values. Read more
1.21.0 (const: unstable) · Source§

fn min(self, other: Self) -> Self
where Self: Sized,

Compares and returns the minimum of two values. Read more
1.50.0 (const: unstable) · Source§

fn clamp(self, min: Self, max: Self) -> Self
where Self: Sized,

Restrict a value to a certain interval. Read more
Source§

impl PartialEq for Backend

Source§

fn eq(&self, other: &Backend) -> bool

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Inequality operator !=. Read more
Source§

impl PartialOrd for Backend

Source§

fn partial_cmp(&self, other: &Backend) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 (const: unstable) · Source§

fn lt(&self, other: &Rhs) -> bool

Tests less than (for self and other) and is used by the < operator. Read more
1.0.0 (const: unstable) · Source§

fn le(&self, other: &Rhs) -> bool

Tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 (const: unstable) · Source§

fn gt(&self, other: &Rhs) -> bool

Tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 (const: unstable) · Source§

fn ge(&self, other: &Rhs) -> bool

Tests greater than or equal to (for self and other) and is used by the >= operator. Read more
Source§

impl StructuralPartialEq for Backend

Source§

impl ToBackend for Backend

Source§

impl<'a> ToBackend for &'a Backend

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<Q, K> Comparable<K> for Q
where Q: Ord + ?Sized, K: Borrow<Q> + ?Sized,

Source§

fn compare(&self, key: &K) -> Ordering

Compare self to key and return their ordering.
Source§

impl<T> Downcast for T
where T: Any,

Source§

fn into_any(self: Box<T>) -> Box<dyn Any>

Convert Box<dyn Trait> (where Trait: Downcast) to Box<dyn Any>. Box<dyn Any> can then be further downcast into Box<ConcreteType> where ConcreteType implements Trait.
Source§

fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>

Convert Rc<Trait> (where Trait: Downcast) to Rc<Any>. Rc<Any> can then be further downcast into Rc<ConcreteType> where ConcreteType implements Trait.
Source§

fn as_any(&self) -> &(dyn Any + 'static)

Convert &Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot generate &Any’s vtable from &Trait’s.
Source§

fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)

Convert &mut Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot generate &mut Any’s vtable from &mut Trait’s.
Source§

impl<T> DowncastSync for T
where T: Any + Send + Sync,

Source§

fn into_any_arc(self: Arc<T>) -> Arc<dyn Any + Send + Sync>

Convert Arc<Trait> (where Trait: Downcast) to Arc<Any>. Arc<Any> can then be further downcast into Arc<ConcreteType> where ConcreteType implements Trait.
Source§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

Source§

fn equivalent(&self, key: &K) -> bool

Checks if this value is equivalent to the given key. Read more
Source§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

Source§

fn equivalent(&self, key: &K) -> bool

Compare self to key and return true if they are equal.
Source§

impl<T> ErasedDestructor for T
where T: 'static,

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

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

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.