-
Notifications
You must be signed in to change notification settings - Fork 27
Rework relay control plane interface #99
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
Draft
englishm-cloudflare
wants to merge
5
commits into
main
Choose a base branch
from
me/control-plane-trait
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from 2 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,37 @@ | ||
| use anyhow::Result; | ||
| use async_trait::async_trait; | ||
| use url::Url; | ||
|
|
||
| /// Origin information for routing | ||
| #[derive(Clone, Debug, PartialEq, Eq)] | ||
| pub struct Origin { | ||
| pub url: Url, | ||
| } | ||
|
|
||
| /// Trait for control plane operations that enable cross-relay routing and state sharing | ||
| #[async_trait] | ||
| pub trait ControlPlane: Send + Sync + Clone + Default + 'static { | ||
| /// Get the origin URL for a given namespace | ||
| async fn get_origin(&self, namespace: &str) -> Result<Option<Origin>>; | ||
|
|
||
| /// Set/register the origin for a given namespace | ||
| async fn set_origin(&self, namespace: &str, origin: Origin) -> Result<()>; | ||
|
|
||
| /// Delete/unregister the origin for a given namespace | ||
| async fn delete_origin(&self, namespace: &str) -> Result<()>; | ||
|
|
||
| /// Create a refresher that periodically updates the origin registration | ||
| /// Returns a future that runs the refresh loop | ||
| fn create_refresher( | ||
| &self, | ||
| namespace: String, | ||
| origin: Origin, | ||
| ) -> Box<dyn ControlPlaneRefresher>; | ||
| } | ||
|
|
||
| /// Trait for periodically refreshing origin registrations | ||
| #[async_trait] | ||
| pub trait ControlPlaneRefresher: Send + 'static { | ||
| /// Run the refresh loop (should run indefinitely until dropped) | ||
| async fn run(&mut self) -> Result<()>; | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,127 @@ | ||
| use anyhow::Result; | ||
| use async_trait::async_trait; | ||
| use url::Url; | ||
|
|
||
| use crate::control_plane::{ControlPlane, ControlPlaneRefresher, Origin}; | ||
|
|
||
| /// HTTP-based control plane implementation using moq-api | ||
| #[derive(Clone)] | ||
| pub struct HttpControlPlane { | ||
| client: moq_api::Client, | ||
| node: Url, | ||
| } | ||
|
|
||
| impl HttpControlPlane { | ||
| pub fn new(api_url: Url, node_url: Url) -> Self { | ||
| let client = moq_api::Client::new(api_url); | ||
| Self { | ||
| client, | ||
| node: node_url, | ||
| } | ||
| } | ||
|
|
||
| pub fn node_url(&self) -> &Url { | ||
| &self.node | ||
| } | ||
| } | ||
|
|
||
| impl Default for HttpControlPlane { | ||
| fn default() -> Self { | ||
| // This is a stub implementation - in practice you'd need valid URLs | ||
| // The actual instance should be created via `new()` with proper config | ||
| panic!("HttpControlPlane requires API URL and node URL - use HttpControlPlane::new() instead") | ||
| } | ||
| } | ||
|
|
||
| #[async_trait] | ||
| impl ControlPlane for HttpControlPlane { | ||
| async fn get_origin(&self, namespace: &str) -> Result<Option<Origin>> { | ||
| match self.client.get_origin(namespace).await? { | ||
| Some(origin) => Ok(Some(Origin { url: origin.url })), | ||
| None => Ok(None), | ||
| } | ||
| } | ||
|
|
||
| async fn set_origin(&self, namespace: &str, origin: Origin) -> Result<()> { | ||
| let moq_origin = moq_api::Origin { url: origin.url }; | ||
| self.client.set_origin(namespace, moq_origin).await?; | ||
| Ok(()) | ||
| } | ||
|
|
||
| async fn delete_origin(&self, namespace: &str) -> Result<()> { | ||
| self.client.delete_origin(namespace).await?; | ||
| Ok(()) | ||
| } | ||
|
|
||
| fn create_refresher( | ||
| &self, | ||
| namespace: String, | ||
| origin: Origin, | ||
| ) -> Box<dyn ControlPlaneRefresher> { | ||
| Box::new(HttpRefresher::new( | ||
| self.client.clone(), | ||
| namespace, | ||
| origin, | ||
| )) | ||
| } | ||
| } | ||
|
|
||
| /// Periodically refreshes the origin registration via HTTP | ||
| pub struct HttpRefresher { | ||
| client: moq_api::Client, | ||
| namespace: String, | ||
| origin: Origin, | ||
| refresh: tokio::time::Interval, | ||
| } | ||
|
|
||
| impl HttpRefresher { | ||
| fn new(client: moq_api::Client, namespace: String, origin: Origin) -> Self { | ||
| // Refresh every 5 minutes | ||
| let duration = tokio::time::Duration::from_secs(300); | ||
| let mut refresh = tokio::time::interval(duration); | ||
| refresh.reset_after(duration); // skip the first tick | ||
|
|
||
| Self { | ||
| client, | ||
| namespace, | ||
| origin, | ||
| refresh, | ||
| } | ||
| } | ||
|
|
||
| async fn update(&self) -> Result<()> { | ||
| log::debug!( | ||
| "registering origin: namespace={} url={}", | ||
| self.namespace, | ||
| self.origin.url | ||
| ); | ||
| let moq_origin = moq_api::Origin { | ||
| url: self.origin.url.clone(), | ||
| }; | ||
| self.client | ||
| .set_origin(&self.namespace, moq_origin) | ||
| .await?; | ||
| Ok(()) | ||
| } | ||
| } | ||
|
|
||
| #[async_trait] | ||
| impl ControlPlaneRefresher for HttpRefresher { | ||
| async fn run(&mut self) -> Result<()> { | ||
| loop { | ||
| self.refresh.tick().await; | ||
| self.update().await?; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| impl Drop for HttpRefresher { | ||
| fn drop(&mut self) { | ||
| let namespace = self.namespace.clone(); | ||
| let client = self.client.clone(); | ||
| log::debug!("removing origin: namespace={}", namespace); | ||
| tokio::spawn(async move { | ||
| let _ = client.delete_origin(&namespace).await; | ||
| }); | ||
| } | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The
Defaultimplementation forHttpControlPlanepanics at runtime, which is problematic sinceControlPlanetrait requiresDefault. This creates a trait implementation that violates the principle of least surprise and will cause runtime panics. Consider either removing theDefaultrequirement from theControlPlanetrait, or providing a safe default implementation (e.g., with placeholder URLs or anOption-wrapped state).