pub struct RelayService(/* private fields */);server only.Expand description
The hyper Service that serves the actual relay endpoints.
This service can be used standalone or embedded into an existing HTTP server.
Implementations§
Source§impl RelayService
impl RelayService
Sourcepub fn new(
handlers: Handlers,
headers: HeaderMap,
rate_limit: Option<ClientRateLimit>,
key_cache: KeyCache,
access: Arc<dyn DynAccessControl>,
metrics: Arc<Metrics>,
) -> Self
pub fn new( handlers: Handlers, headers: HeaderMap, rate_limit: Option<ClientRateLimit>, key_cache: KeyCache, access: Arc<dyn DynAccessControl>, metrics: Arc<Metrics>, ) -> Self
Creates a new RelayService.
This allows embedding the relay service into an existing HTTP server.
Sourcepub fn set_client_rate_limit(&self, rate_limit: Option<ClientRateLimit>)
pub fn set_client_rate_limit(&self, rate_limit: Option<ClientRateLimit>)
Updates the per-client receive rate limit.
The new rate limit will be applied to all current and future client connections.
Passing None will remove rate limiting from all connections.
Sourcepub fn clients(&self) -> &Clients
pub fn clients(&self) -> &Clients
Returns a reference to the registry of currently connected clients.
The returned Clients handle can be used at runtime to disconnect a
connected endpoint via Clients::disconnect.
Sourcepub async fn handle_connection(
self,
stream: TcpStream,
tls_config: Option<TlsConfig>,
establish_timeout: Duration,
)
pub async fn handle_connection( self, stream: TcpStream, tls_config: Option<TlsConfig>, establish_timeout: Duration, )
Handle the incoming connection.
If a tls_config is given, will serve the connection using HTTPS, otherwise HTTP.
TLS and HTTP establishment are bounded by establish_timeout. An upgrade request starts
a separate timeout of the same duration for the WebSocket upgrade, authentication, and
authorization. The connection is aborted if either timeout expires.
§Example
// Create a relay service
let handlers = Handlers::default();
let headers = HeaderMap::new();
let key_cache = KeyCache::new(1024);
let metrics = Arc::new(Metrics::default());
let relay_service = RelayService::new(
handlers,
headers,
None, // No rate limiting
key_cache,
Arc::new(AllowAll),
metrics,
);
// Generate a self-signed certificate for HTTPS
let cert = rcgen::generate_simple_self_signed(vec!["localhost".to_string()])?;
let cert_der = cert.cert.der().to_vec();
let private_key_der = cert.signing_key.serialize_der();
let cert_chain = vec![CertificateDer::from(cert_der)];
let private_key = PrivateKeyDer::try_from(private_key_der)?;
// Serve with HTTPS
let server_config = Arc::new(
rustls::ServerConfig::builder()
.with_no_client_auth()
.with_single_cert(cert_chain, private_key)?,
);
let tls_config = TlsConfig::new(server_config);
relay_service
.clone()
.handle_connection(stream, Some(tls_config), Duration::from_secs(30))
.await;
// Or serve with plain HTTP
relay_service
.handle_connection(stream, None, Duration::from_secs(30))
.await;Trait Implementations§
Source§impl Clone for RelayService
impl Clone for RelayService
Source§fn clone(&self) -> RelayService
fn clone(&self) -> RelayService
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreAuto Trait Implementations§
impl Freeze for RelayService
impl !RefUnwindSafe for RelayService
impl Send for RelayService
impl Sync for RelayService
impl Unpin for RelayService
impl !UnwindSafe for RelayService
Blanket Implementations§
§impl<'a, T, E> AsTaggedExplicit<'a, E> for Twhere
T: 'a,
impl<'a, T, E> AsTaggedExplicit<'a, E> for Twhere
T: 'a,
§impl<'a, T, E> AsTaggedImplicit<'a, E> for Twhere
T: 'a,
impl<'a, T, E> AsTaggedImplicit<'a, E> for Twhere
T: 'a,
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
§impl<T> Instrument for T
impl<T> Instrument for T
§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
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 moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
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