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§
source§impl Backend
impl Backend
sourcepub fn from_name(s: &str) -> Result<Self, BackendError>
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'
orDELETE
. - 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.
sourcepub fn into_string(self) -> String
pub fn into_string(self) -> String
Turn the backend into its name as a string.
Trait Implementations§
source§impl BackendExt for Backend
impl BackendExt for Backend
source§fn builder(name: impl ToString, target: impl ToString) -> BackendBuilder
fn builder(name: impl ToString, target: impl ToString) -> BackendBuilder
source§fn is_healthy(&self) -> Result<BackendHealth, Error>
fn is_healthy(&self) -> Result<BackendHealth, Error>
source§impl Ord for Backend
impl Ord for Backend
source§impl PartialEq<Backend> for Backend
impl PartialEq<Backend> for Backend
source§impl PartialOrd<Backend> for Backend
impl PartialOrd<Backend> for Backend
1.0.0 · source§fn le(&self, other: &Rhs) -> bool
fn le(&self, other: &Rhs) -> bool
self
and other
) and is used by the <=
operator. Read more