-
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: source flow with extractor and transformer (#131)
- Loading branch information
Showing
25 changed files
with
584 additions
and
427 deletions.
There are no files selected for viewing
This file contains 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 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 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 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 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 |
---|---|---|
@@ -1,2 +1,2 @@ | ||
[toolchain] | ||
channel = "1.80.1" | ||
channel = "1.81.0" |
This file contains 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 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,20 @@ | ||
use super::Pipe; | ||
use crate::errors::Result; | ||
use std::marker::PhantomData; | ||
|
||
pub(crate) struct Processor<I> { | ||
input_type: PhantomData<I>, | ||
} | ||
|
||
impl<I> Processor<I> { | ||
pub(crate) fn new() -> Self { | ||
Self { input_type: PhantomData } | ||
} | ||
} | ||
|
||
impl<I> Pipe for Processor<I> { | ||
type Input = I; | ||
fn send(&mut self, _input: Self::Input) -> Result<()> { | ||
Ok(()) | ||
} | ||
} |
This file contains 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,38 @@ | ||
use super::Pipe; | ||
use crate::errors::Result; | ||
use serde::{Deserialize, Serialize}; | ||
use std::fmt::Debug; | ||
use std::marker::PhantomData; | ||
|
||
#[derive(Clone, Debug, Deserialize, Serialize)] | ||
pub(crate) struct Config { | ||
target: String, | ||
} | ||
|
||
pub(crate) struct Processor<I, N> { | ||
target: String, | ||
next: N, | ||
input_type: PhantomData<I>, | ||
} | ||
|
||
impl<I, N> Processor<I, N> { | ||
pub(crate) fn new(target: String, next: N) -> Self { | ||
Self { target, next, input_type: PhantomData } | ||
} | ||
|
||
pub(crate) fn try_from(config: &Config, next: N) -> Result<Self> { | ||
Ok(Self::new(config.target.clone(), next)) | ||
} | ||
} | ||
|
||
impl<I, N> Pipe for Processor<I, N> | ||
where | ||
I: Debug, | ||
N: Pipe<Input = I>, | ||
{ | ||
type Input = I; | ||
fn send(&mut self, input: Self::Input) -> Result<()> { | ||
tracing::info!(target=self.target, input=?input); | ||
self.next.send(input) | ||
} | ||
} |
This file contains 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,39 @@ | ||
use crate::errors::Result; | ||
pub mod discard_all; | ||
pub mod log; | ||
pub mod passthrough; | ||
|
||
/// A pipe is an interface to implement processor for inputs. | ||
/// The implementations can: | ||
/// | ||
/// - discard / drop all inputs | ||
/// - filter | ||
/// - transform | ||
/// - split | ||
/// - retry | ||
/// - timeout | ||
/// - ... | ||
/// | ||
/// The composition of Pipes to create pipeline could be done by configuration, | ||
/// and the behavior of the pipe should be internal, | ||
/// so chaining of pipes should not depends of method `map`, `fold`, `filter`, | ||
/// `filter_map`, `drop`,... like for `Iterator`, `Stream`, `RxRust`. | ||
/// Also being able to return Error to the sender could help the Sender to ease handling (vs `Stream`) | ||
/// like retry, buffering, forward to its caller... | ||
/// | ||
/// The approach and goal is similar to middleware used in some webframework | ||
/// or in [tower](https://crates.io/crates/tower), Except it's not async. | ||
/// Maybe if we need to support async, `Pipe` will become a specialization of tower's middleware, | ||
/// like [axum](https://crates.io/crates/axum), [warp](https://crates.io/crates/warp), [tonic](https://crates.io/crates/tonic),... do. | ||
pub trait Pipe { | ||
type Input; | ||
|
||
fn send(&mut self, input: Self::Input) -> Result<()>; | ||
} | ||
|
||
impl<I, T: Pipe<Input = I> + ?Sized> Pipe for Box<T> { | ||
type Input = I; | ||
fn send(&mut self, input: Self::Input) -> Result<()> { | ||
T::send(self, input) | ||
} | ||
} |
This file contains 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,30 @@ | ||
use super::Pipe; | ||
use crate::errors::Result; | ||
use serde::{Deserialize, Serialize}; | ||
use std::fmt::Debug; | ||
use std::marker::PhantomData; | ||
|
||
#[derive(Debug, Deserialize, Serialize, Default)] | ||
pub(crate) struct Config {} | ||
|
||
pub(crate) struct Processor<I, N> { | ||
next: N, | ||
input_type: PhantomData<I>, | ||
} | ||
|
||
impl<I, N> Processor<I, N> { | ||
pub(crate) fn new(next: N) -> Self { | ||
Self { next, input_type: PhantomData } | ||
} | ||
} | ||
|
||
impl<I, N> Pipe for Processor<I, N> | ||
where | ||
I: Debug, | ||
N: Pipe<Input = I>, | ||
{ | ||
type Input = I; | ||
fn send(&mut self, input: Self::Input) -> Result<()> { | ||
self.next.send(input) | ||
} | ||
} |
This file contains 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 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 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 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 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,43 @@ | ||
use super::{http, opendal, EventSourcePipe, Extractor}; | ||
use crate::errors::Result; | ||
use serde::{Deserialize, Serialize}; | ||
|
||
#[derive(Debug, Clone, Deserialize, Serialize, Default)] | ||
#[serde(tag = "type")] | ||
pub(crate) enum Config { | ||
#[serde(alias = "noop")] | ||
#[default] | ||
Sleep, | ||
#[cfg(feature = "source_http")] | ||
#[serde(alias = "http")] | ||
Http(http::Config), | ||
#[cfg(feature = "source_opendal")] | ||
#[serde(alias = "opendal")] | ||
Opendal(opendal::Config), | ||
} | ||
|
||
impl Config { | ||
pub(crate) fn make_extractor(&self, next: EventSourcePipe) -> Result<Box<dyn Extractor>> { | ||
let out: Box<dyn Extractor> = match self { | ||
Config::Sleep => Box::new(SleepExtractor {}), | ||
#[cfg(feature = "source_http")] | ||
Config::Http(config) => Box::new(http::HttpExtractor::try_from(config, next)?), | ||
#[cfg(feature = "source_opendal")] | ||
Config::Opendal(config) => Box::new(opendal::OpendalExtractor::try_from(config, next)?), | ||
}; | ||
Ok(out) | ||
} | ||
} | ||
|
||
struct SleepExtractor {} | ||
|
||
#[async_trait::async_trait] | ||
impl Extractor for SleepExtractor { | ||
async fn run(&mut self) -> Result<()> { | ||
use std::future; | ||
|
||
let future = future::pending(); | ||
let () = future.await; | ||
unreachable!() | ||
} | ||
} |
This file contains 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,28 @@ | ||
use super::{EventSource, EventSourcePipe}; | ||
use crate::errors::Result; | ||
use crate::pipes::Pipe; | ||
use handlebars::Handlebars; | ||
|
||
pub(crate) struct Processor { | ||
next: EventSourcePipe, | ||
hbs: Handlebars<'static>, | ||
} | ||
|
||
impl Processor { | ||
pub(crate) fn new(template: &str, next: EventSourcePipe) -> Result<Self> { | ||
let mut hbs = Handlebars::new(); | ||
hbs.set_dev_mode(false); | ||
hbs.set_strict_mode(true); | ||
hbs.register_template_string("tpl", template)?; | ||
Ok(Self { next, hbs }) | ||
} | ||
} | ||
|
||
impl Pipe for Processor { | ||
type Input = EventSource; | ||
fn send(&mut self, input: Self::Input) -> Result<()> { | ||
let res = self.hbs.render("tpl", &input)?; | ||
let output: EventSource = serde_json::from_str(&res)?; | ||
self.next.send(output) | ||
} | ||
} |
Oops, something went wrong.