-
Notifications
You must be signed in to change notification settings - Fork 45
/
apis.rs
105 lines (94 loc) 路 3.29 KB
/
apis.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
use crate::errors::Error;
use bytes::Bytes;
use clap::ArgEnum;
use futures_core::stream::Stream;
use std::pin::Pin;
// Flow Capture operations defined in
// https://github.com/estuary/flow/blob/master/go/protocols/capture/capture.proto
#[derive(Debug, strum_macros::Display, ArgEnum, Clone)]
#[strum(serialize_all = "kebab_case")]
pub enum FlowCaptureOperation {
Spec,
Discover,
Validate,
ApplyUpsert,
ApplyDelete,
Pull,
}
// Flow Materialize operations defined in
// https://github.com/estuary/flow/blob/master/go/protocols/materialize/materialize.proto
#[derive(Debug, strum_macros::Display, ArgEnum, Clone)]
#[strum(serialize_all = "kebab_case")]
pub enum FlowMaterializeOperation {
Spec,
Validate,
ApplyUpsert,
ApplyDelete,
Transactions,
}
// To be used as a trait bound for interceptors.
pub trait FlowOperation {}
impl FlowOperation for FlowCaptureOperation {}
impl FlowOperation for FlowMaterializeOperation {}
// An interceptor modifies the request/response streams between Flow runtime and the connector.
// InterceptorStream defines the type of input and output streams handled by interceptors.
pub type InterceptorStream = Pin<Box<dyn Stream<Item = std::io::Result<Bytes>> + Send + Sync>>;
pub trait Interceptor<T: FlowOperation> {
fn convert_command_args(&mut self, _op: &T, args: Vec<String>) -> Result<Vec<String>, Error> {
Ok(args)
}
fn convert_request(
&mut self,
// The pid of the connector process, which is in stopped state at first, waiting for SIGCONT signals from interceptors.
pid: Option<u32>,
op: &T,
stream: InterceptorStream,
) -> Result<InterceptorStream, Error> {
Ok(stream)
}
fn convert_response(
&mut self,
op: &T,
stream: InterceptorStream,
) -> Result<InterceptorStream, Error> {
Ok(stream)
}
}
struct ComposedInterceptor<T: 'static + FlowOperation> {
a: Box<dyn Interceptor<T>>,
b: Box<dyn Interceptor<T>>,
}
impl<T: 'static + FlowOperation> Interceptor<T> for ComposedInterceptor<T> {
fn convert_command_args(&mut self, op: &T, args: Vec<String>) -> Result<Vec<String>, Error> {
self.a
.convert_command_args(op, self.b.convert_command_args(op, args)?)
}
fn convert_request(
&mut self,
pid: Option<u32>,
op: &T,
stream: InterceptorStream,
) -> Result<InterceptorStream, Error> {
// Suppressing pid for interceptor b to ensure that only the first interceptor
// in the chain is responsible to start the connector.
// This satisfy the current requirements, and we can extend it with
// more complex connector triggering logic that involves multiple interceptors.
self.a
.convert_request(pid, op, self.b.convert_request(None, op, stream)?)
}
fn convert_response(
&mut self,
op: &T,
stream: InterceptorStream,
) -> Result<InterceptorStream, Error> {
self.b
.convert_response(op, self.a.convert_response(op, stream)?)
}
}
// Two interceptors can be composed together to form a new interceptor.
pub fn compose<T: 'static + FlowOperation>(
a: Box<dyn Interceptor<T>>,
b: Box<dyn Interceptor<T>>,
) -> Box<dyn Interceptor<T>> {
Box::new(ComposedInterceptor { a, b })
}