Crate statefun

Source
Expand description

An SDK for writing “stateful functions” in Rust. For use with Apache Flink Stateful Functions (Statefun).

§Examples

The following shows how to write a simple stateful function and serve it for use in a Statefun deployment.

use protobuf::well_known_types::StringValue;

use statefun::io::kafka;
use statefun::transport::hyper::HyperHttpTransport;
use statefun::transport::Transport;
use statefun::{Address, Context, Effects, EgressIdentifier, FunctionRegistry, FunctionType};

let mut function_registry = FunctionRegistry::new();

function_registry.register_fn(
    FunctionType::new("example", "function1"),
    |context, message: StringValue| {
        let mut effects = Effects::new();

        effects.send(
            Address::new(FunctionType::new("example", "function2"), "doctor"),
            message,
        );

        effects
    },
);

let hyper_transport = HyperHttpTransport::new("0.0.0.0:5000".parse()?);
hyper_transport.run(function_registry)?;

The program creates a FunctionRegistry, which can be used to register one or more functions. Then we register a closure as a stateful function. Finally, we need to create a Transport, in this case the HyperHttpTransport to serve our stateful function.

Not that you can also use a function instead of a closure when registering functions.

Refer to the Stateful Functions documentation to learn how to use this in a deployment. Especially the modules documentation is pertinent.

Modules§

io
A set of traits that allow sending egress messages to systems such as Kafka.
transport
Transports are used to serve stateful functions to make them invokable.

Structs§

Address
The unique identity of an individual stateful function.
Context
Context for a single invocation of a stateful function.
Effects
Effects (or side effects) of a stateful function invocation.
EgressIdentifier
A reference to an egress, consisting of a namespace and a name.
FunctionRegistry
Keeps a mapping from FunctionType to stateful functions. Use this together with a Transport to serve stateful functions.
FunctionType
A reference to a stateful function, consisting of a namespace and a name.