Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement component traits on ComponentCtx #421

Merged
merged 1 commit into from
Aug 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions lib/src/component/async_io.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
use {
super::fastly::api::{async_io, types},
crate::{session::Session, wiggle_abi},
crate::{linking::ComponentCtx, wiggle_abi},
futures::FutureExt,
std::time::Duration,
};

#[async_trait::async_trait]
impl async_io::Host for Session {
impl async_io::Host for ComponentCtx {
async fn select(
&mut self,
hs: Vec<async_io::Handle>,
Expand All @@ -16,7 +16,7 @@ impl async_io::Host for Session {
return Err(types::Error::InvalidArgument.into());
}

let select_fut = self.select_impl(
let select_fut = self.session.select_impl(
hs.iter()
.copied()
.map(|i| wiggle_abi::types::AsyncItemHandle::from(i).into()),
Expand Down Expand Up @@ -44,6 +44,7 @@ impl async_io::Host for Session {
async fn is_ready(&mut self, handle: async_io::Handle) -> Result<bool, types::Error> {
let handle = wiggle_abi::types::AsyncItemHandle::from(handle);
Ok(self
.session
.async_item_mut(handle.into())?
.await_ready()
.now_or_never()
Expand Down
60 changes: 45 additions & 15 deletions lib/src/component/backend.rs
Original file line number Diff line number Diff line change
@@ -1,35 +1,41 @@
use {
super::fastly::api::{backend, http_types, types},
crate::{error::Error, session::Session},
crate::{error::Error, linking::ComponentCtx},
};

#[async_trait::async_trait]
impl backend::Host for Session {
impl backend::Host for ComponentCtx {
async fn exists(&mut self, backend: String) -> Result<bool, types::Error> {
Ok(self.backend(&backend).is_some())
Ok(self.session.backend(&backend).is_some())
}

async fn is_healthy(
&mut self,
backend: String,
) -> Result<backend::BackendHealth, types::Error> {
// just doing this to get a different error if the backend doesn't exist
let _ = self.backend(&backend).ok_or(Error::InvalidArgument)?;
let _ = self
.session
.backend(&backend)
.ok_or(Error::InvalidArgument)?;
Ok(backend::BackendHealth::Unknown)
}

async fn is_dynamic(&mut self, backend: String) -> Result<bool, types::Error> {
if self.dynamic_backend(&backend).is_some() {
if self.session.dynamic_backend(&backend).is_some() {
Ok(true)
} else if self.backend(&backend).is_some() {
} else if self.session.backend(&backend).is_some() {
Ok(false)
} else {
Err(Error::InvalidArgument.into())
}
}

async fn get_host(&mut self, backend: String, max_len: u64) -> Result<String, types::Error> {
let backend = self.backend(&backend).ok_or(Error::InvalidArgument)?;
let backend = self
.session
.backend(&backend)
.ok_or(Error::InvalidArgument)?;

let host = backend.uri.host().expect("backend uri has host");

Expand All @@ -49,7 +55,10 @@ impl backend::Host for Session {
backend: String,
max_len: u64,
) -> Result<Option<Vec<u8>>, types::Error> {
let backend = self.backend(&backend).ok_or(Error::InvalidArgument)?;
let backend = self
.session
.backend(&backend)
.ok_or(Error::InvalidArgument)?;
if let Some(host) = backend.override_host.as_ref() {
let host = host.to_str()?;

Expand All @@ -68,7 +77,10 @@ impl backend::Host for Session {
}

async fn get_port(&mut self, backend: String) -> Result<u16, types::Error> {
let backend = self.backend(&backend).ok_or(Error::InvalidArgument)?;
let backend = self
.session
.backend(&backend)
.ok_or(Error::InvalidArgument)?;
match backend.uri.port_u16() {
Some(port) => Ok(port),
None => {
Expand All @@ -83,7 +95,10 @@ impl backend::Host for Session {

async fn get_connect_timeout_ms(&mut self, backend: String) -> Result<u32, types::Error> {
// just doing this to get a different error if the backend doesn't exist
let _ = self.backend(&backend).ok_or(Error::InvalidArgument)?;
let _ = self
.session
.backend(&backend)
.ok_or(Error::InvalidArgument)?;
Err(Error::Unsupported {
msg: "connection timing is not actually supported in Viceroy",
}
Expand All @@ -92,7 +107,10 @@ impl backend::Host for Session {

async fn get_first_byte_timeout_ms(&mut self, backend: String) -> Result<u32, types::Error> {
// just doing this to get a different error if the backend doesn't exist
let _ = self.backend(&backend).ok_or(Error::InvalidArgument)?;
let _ = self
.session
.backend(&backend)
.ok_or(Error::InvalidArgument)?;
Err(Error::Unsupported {
msg: "connection timing is not actually supported in Viceroy",
}
Expand All @@ -101,15 +119,21 @@ impl backend::Host for Session {

async fn get_between_bytes_timeout_ms(&mut self, backend: String) -> Result<u32, types::Error> {
// just doing this to get a different error if the backend doesn't exist
let _ = self.backend(&backend).ok_or(Error::InvalidArgument)?;
let _ = self
.session
.backend(&backend)
.ok_or(Error::InvalidArgument)?;
Err(Error::Unsupported {
msg: "connection timing is not actually supported in Viceroy",
}
.into())
}

async fn is_ssl(&mut self, backend: String) -> Result<bool, types::Error> {
let backend = self.backend(&backend).ok_or(Error::InvalidArgument)?;
let backend = self
.session
.backend(&backend)
.ok_or(Error::InvalidArgument)?;
Ok(backend.uri.scheme() == Some(&http::uri::Scheme::HTTPS))
}

Expand All @@ -118,7 +142,10 @@ impl backend::Host for Session {
backend: String,
) -> Result<Option<http_types::TlsVersion>, types::Error> {
// just doing this to get a different error if the backend doesn't exist
let _ = self.backend(&backend).ok_or(Error::InvalidArgument)?;
let _ = self
.session
.backend(&backend)
.ok_or(Error::InvalidArgument)?;
// health checks are not enabled in Viceroy :(
Err(Error::Unsupported {
msg: "ssl version flags are not supported in Viceroy",
Expand All @@ -131,7 +158,10 @@ impl backend::Host for Session {
backend: String,
) -> Result<Option<http_types::TlsVersion>, types::Error> {
// just doing this to get a different error if the backend doesn't exist
let _ = self.backend(&backend).ok_or(Error::InvalidArgument)?;
let _ = self
.session
.backend(&backend)
.ok_or(Error::InvalidArgument)?;
// health checks are not enabled in Viceroy :(
Err(Error::Unsupported {
msg: "ssl version flags are not supported in Viceroy",
Expand Down
4 changes: 2 additions & 2 deletions lib/src/component/cache.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
use {
super::fastly::api::{cache, http_types, types},
crate::{error::Error, session::Session},
crate::{error::Error, linking::ComponentCtx},
};

#[async_trait::async_trait]
impl cache::Host for Session {
impl cache::Host for ComponentCtx {
async fn lookup(
&mut self,
_key: String,
Expand Down
6 changes: 3 additions & 3 deletions lib/src/component/compute_runtime.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
use super::fastly::api::{compute_runtime, types};
use crate::session::Session;
use crate::linking::ComponentCtx;
use std::sync::atomic::Ordering;

#[async_trait::async_trait]
impl compute_runtime::Host for Session {
impl compute_runtime::Host for ComponentCtx {
async fn get_vcpu_ms(&mut self) -> Result<u64, types::Error> {
Ok(self.active_cpu_time_us.load(Ordering::SeqCst) / 1000)
Ok(self.session.active_cpu_time_us.load(Ordering::SeqCst) / 1000)
}
}
8 changes: 4 additions & 4 deletions lib/src/component/config_store.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
use {
super::fastly::api::{config_store, types},
crate::session::Session,
crate::linking::ComponentCtx,
};

#[async_trait::async_trait]
impl config_store::Host for Session {
impl config_store::Host for ComponentCtx {
async fn open(&mut self, name: String) -> Result<config_store::Handle, types::Error> {
let handle = self.dictionary_handle(name.as_str())?;
let handle = self.session.dictionary_handle(name.as_str())?;
Ok(handle.into())
}

Expand All @@ -16,7 +16,7 @@ impl config_store::Host for Session {
name: String,
max_len: u64,
) -> Result<Option<Vec<u8>>, types::Error> {
let dict = &self.dictionary(store.into())?.contents;
let dict = &self.session.dictionary(store.into())?.contents;

let item = if let Some(item) = dict.get(&name) {
item
Expand Down
6 changes: 3 additions & 3 deletions lib/src/component/device_detection.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
use {
super::fastly::api::{device_detection, types},
crate::session::Session,
crate::linking::ComponentCtx,
};

#[async_trait::async_trait]
impl device_detection::Host for Session {
impl device_detection::Host for ComponentCtx {
async fn lookup(
&mut self,
user_agent: String,
max_len: u64,
) -> Result<Option<Vec<u8>>, types::Error> {
if let Some(result) = self.device_detection_lookup(&user_agent) {
if let Some(result) = self.session.device_detection_lookup(&user_agent) {
if result.len() > max_len as usize {
return Err(types::Error::BufferLen(
u64::try_from(result.len()).unwrap_or(0),
Expand Down
8 changes: 4 additions & 4 deletions lib/src/component/dictionary.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
use {
super::fastly::api::{dictionary, types},
crate::session::Session,
crate::linking::ComponentCtx,
};

#[async_trait::async_trait]
impl dictionary::Host for Session {
impl dictionary::Host for ComponentCtx {
async fn open(&mut self, name: String) -> Result<dictionary::Handle, types::Error> {
let handle = self.dictionary_handle(name.as_str())?;
let handle = self.session.dictionary_handle(name.as_str())?;
Ok(handle.into())
}

Expand All @@ -16,7 +16,7 @@ impl dictionary::Host for Session {
key: String,
max_len: u64,
) -> Result<Option<Vec<u8>>, types::Error> {
let dict = &self.dictionary(h.into())?.contents;
let dict = &self.session.dictionary(h.into())?.contents;

let item = if let Some(item) = dict.get(&key) {
item
Expand Down
4 changes: 2 additions & 2 deletions lib/src/component/erl.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
use {
super::fastly::api::{erl, types},
crate::session::Session,
crate::linking::ComponentCtx,
};

#[async_trait::async_trait]
impl erl::Host for Session {
impl erl::Host for ComponentCtx {
async fn check_rate(
&mut self,
_rc: String,
Expand Down
5 changes: 3 additions & 2 deletions lib/src/component/geo.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
use {
super::fastly::api::{geo, types},
crate::{error, session::Session},
crate::{error, linking::ComponentCtx},
std::net::{IpAddr, Ipv4Addr, Ipv6Addr},
};

#[async_trait::async_trait]
impl geo::Host for Session {
impl geo::Host for ComponentCtx {
async fn lookup(&mut self, octets: Vec<u8>, max_len: u64) -> Result<Vec<u8>, types::Error> {
let ip_addr: IpAddr = match octets.len() {
4 => IpAddr::V4(Ipv4Addr::from(
Expand All @@ -18,6 +18,7 @@ impl geo::Host for Session {
};

let json = self
.session
.geolocation_lookup(&ip_addr)
.ok_or(geo::Error::UnknownError)?;

Expand Down
Loading
Loading