Background:
The Stateful function described in this article was implemented based on the Serverless Reactor.
Serverless Reactor provides developers with the quick launch experience of flying book robots. Simply write the logic of how to generate the output from the input (the message sent to the robot) into a code function and upload the function to the Serverless Reactor. You can easily launch a robot application.
The Serverless Reactor supports writing such a function in Rust, the most popular language of the moment. We can build a robot using the simplest Rust syntax. You can easily learn Rust programming while playing with a flying book robot!
Stateful function: ejbateful function
From the above, you have found this robot application can realize continuous calculation, namely Stateful function. You send the formula 1+1 to the robot, and the robot returns 2, and you can send it to the robot plus 2, and the robot returns 4.
The Serverless Reactor was designed to implement a stateful function.
As a comparison, earlier we shared a very simple robot application with only eight lines of code.
// Simple computation robot use WASm_bindgen :: Prelude ::*; use meval; #[wasm_bindgen] pub fn text_received(msg: String, _user_info: String, _step_data: String) -> String { let x = meval::eval_str(&msg).unwrap(); return format! ("{}", x); }Copy the code
The robot application that realized the continuous calculation used the same Rust library as the simple robot mentioned above, but used the Step function encapsulated by the Serverless Reactor.
// Use wasm_bindgen:: Prelude ::*; use meval; #[wasm_bindgen] pub fn text_received(msg: String, _user_info: String, step_data: String) -> String { if msg == "#" { return format! (r#"{{"new_step": true}}"#); } else { let exp = match step_data == ""{ true => msg, _ => format! ("({}){}", step_data, msg) }; let x = meval::eval_str(&exp).unwrap(); return format! (r#"{{"result": "{}", "step": "{}"}}"#, x, exp); }}Copy the code
From the comparison of these two codes, it can be seen that there are many more steps in the Rust code of the continuous multi-step computing robot. Step can save the state of any step applied. The calculator program here saves the formula of the previous step, (1+1), enclosed in parentheses. When the same user enters *2 again, the program computes (1+1)*2 and then computes the result.
The Serverless Reactor back-end uses the WebAssembly (Wasm) VIRTUAL machine WasmEdge. WebAssembly programs themselves are stateless, so Serverless Reactor designed STEP to help save the state of previous functions.
Each time the Wasm function is called by the Serverless Reactor, the function returns a step value. If the application needs to save the state of the previous step, the Reactor saves the step and passes it back to the Wasm program the next time the function is called. So Wasm was designed to save function state, which was stateful.
A common example of a computing robot is only a question and answer, without saving the calculation results or calculation formulas, and without step.
The serial robot example needs to save the formula of the previous step, so {“new_step”: true}} is used to tell the Reactor to empty its memory, remember each returned step, and use the previous returned step in subsequent function calls.
Stateful functions were created serverless to write a program that could interact with a robot several times. The above mentioned multi-step calculator, manipulating images, randomly generating names based on gender and birth date, weather forecast app (location and date).
Launch your own robot
Like other applications for Reactor, once Rust code is written, it is compiled into a Wasm file using RustwasMC and uploaded to the Serverless Reactor platform. And submit the generated service URL to the database for details, please refer to our starter document, Quick Start Serverless Reactor.
If you also want to build a continuous multi-step calculator application, the full code is here.