Skip to content

Commit a66a992

Browse files
committed
[wip, feat] Action dispatcher state machine
Signed-off-by: dd di cesare <[email protected]>
1 parent ed8eec1 commit a66a992

File tree

2 files changed

+126
-0
lines changed

2 files changed

+126
-0
lines changed

src/action_dispatcher.rs

+125
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
enum State {
2+
Pending,
3+
Waiting,
4+
Done,
5+
}
6+
7+
impl State {
8+
fn as_str(&self) -> &'static str {
9+
match self {
10+
State::Pending => "pending",
11+
State::Waiting => "waiting",
12+
State::Done => "done",
13+
}
14+
}
15+
fn next(&mut self) {
16+
match self {
17+
State::Pending => *self = State::Waiting,
18+
State::Waiting => *self = State::Done,
19+
_ => {}
20+
}
21+
}
22+
}
23+
24+
enum Action {
25+
Auth { state: State },
26+
RateLimit { state: State },
27+
}
28+
29+
impl Action {
30+
fn trigger(&mut self) {
31+
match self {
32+
Action::Auth { .. } => self.auth(),
33+
Action::RateLimit { .. } => self.rate_limit(),
34+
}
35+
}
36+
37+
fn get_state(&self) -> &State {
38+
match self {
39+
Action::Auth { state } => state,
40+
Action::RateLimit { state } => state,
41+
}
42+
}
43+
44+
fn rate_limit(&mut self) {
45+
// Specifics for RL, returning State
46+
if let Action::RateLimit { state } = self {
47+
match state {
48+
State::Pending => {
49+
println!("Trigger the request and return State::Waiting");
50+
state.next();
51+
}
52+
State::Waiting => {
53+
println!(
54+
"When got on_grpc_response, process RL response and return State::Done"
55+
);
56+
state.next();
57+
}
58+
State::Done => {
59+
println!("Done for RL... calling next action (?)");
60+
}
61+
}
62+
}
63+
}
64+
65+
fn auth(&mut self) {
66+
// Specifics for Auth, returning State
67+
if let Action::Auth { state } = self {
68+
match state {
69+
State::Pending => {
70+
println!("Trigger the request and return State::Waiting");
71+
state.next();
72+
}
73+
State::Waiting => {
74+
println!(
75+
"When got on_grpc_response, process Auth response and return State::Done"
76+
);
77+
state.next();
78+
}
79+
State::Done => {
80+
println!("Done for Auth... calling next action (?)");
81+
}
82+
}
83+
}
84+
}
85+
}
86+
87+
struct ActionDispatcher {
88+
actions: Vec<Action>,
89+
}
90+
91+
impl ActionDispatcher {
92+
fn default() -> ActionDispatcher {
93+
ActionDispatcher { actions: vec![] }
94+
}
95+
fn new(/*vec of PluginConfig actions*/) -> ActionDispatcher {
96+
ActionDispatcher {
97+
// hardcoded for now
98+
actions: vec![
99+
Action::Auth {
100+
state: State::Pending,
101+
},
102+
Action::RateLimit {
103+
state: State::Pending,
104+
},
105+
],
106+
}
107+
}
108+
}
109+
110+
#[cfg(test)]
111+
mod tests {
112+
use super::*;
113+
114+
#[test]
115+
fn action_transition() {
116+
let mut action = Action::Auth {
117+
state: State::Pending,
118+
};
119+
assert_eq!(action.get_state().as_str(), "pending");
120+
action.trigger();
121+
assert_eq!(action.get_state().as_str(), "waiting");
122+
action.trigger();
123+
assert_eq!(action.get_state().as_str(), "done");
124+
}
125+
}

src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
mod action_dispatcher;
12
mod attribute;
23
mod configuration;
34
mod envoy;

0 commit comments

Comments
 (0)