Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introduces persist_event_if #142

Merged
merged 1 commit into from
Feb 1, 2024
Merged

Introduces persist_event_if #142

merged 1 commit into from
Feb 1, 2024

Conversation

huntc
Copy link
Contributor

@huntc huntc commented Jan 27, 2024

I introduced a new persist_event_if effect function to be used where a potential async side effect needs to be performed so that we may construct a persistent event.

In my particular situation, I have a behavior that maintains a synchronized counter and a private key for all edge-based gateways. This counter is required to be incremented and then encrypted along with an entity's stable MAC address, which is a field of the entity state. The result is a value that can be used in a "public address" which does not reveal an underlying stable address.

For more background on how to determine this type of address: https://en.wikipedia.org/wiki/IPv6_address#Stable_privacy_addresses.

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature.
I introduced a new `persist_event_if` function to be used where a potentially async side effect needs to be performed so that we may construct a persistent event.

In my particular situation, I have a behaviour that maintains a synchronised counter and a private key for all edge-based gateways. This counter is required to be incremented and then encrypted along with a single entity's stable MAC address, which is a field of the entity state. The result is a value that can be used in a "public address" which does not reveal an underlying stable address.

For more info on this type of address: https://en.wikipedia.org/wiki/IPv6_address#Stable_privacy_addresses.
@huntc huntc requested a review from patriknw January 27, 2024 06:12
@huntc huntc self-assigned this Jan 27, 2024
@huntc huntc added the enhancement New feature or request label Jan 27, 2024
Copy link
Member

@patriknw patriknw left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

looking good, having support for a command handler that makes an async validation before persisting is something we have talked several times about for Akka's EventSourcedBehavior, and I think we are getting closing to actually adding it

/// latest state given any previous effect having persisted an event,
/// or else the state at the outset of the effects being applied,
/// is also available.
pub fn persist_event_if<B, F, R>(f: F) -> ThenPersistEvent<B, F, R>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is it possible to combine persist_event_if with a reply, so that you can reply with different things depending on the outcome of the async _if ? (I think so, but want to be sure)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, that’s possible. I’ve got an example of this which I’ll share when I’m back at my desk tomorrow.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here's my example:

let entity_id = context.entity_id.clone();
persist_event_if(move |b: &Self, _, _| {
    let registered = entity_id.parse().ok().map(|mac_address| {
        Event::Registered(Registered {
            client_ip: into_eui(&mac_address),
            client_pub_key,
            server_host: b.server_host.clone(),
            server_pub_key: b.server_pub_key.clone(),
        })
    });

    future::ready(Ok(registered))
})
.then_reply(move |s| {
    s.and_then(|s| {
        match (
            s.client_ip,
            &s.client_pub_key,
            &s.server_host,
            &s.server_pub_key,
        ) {
            (
                Some(client_ip),
                Some(client_pub_key),
                Some(server_host),
                Some(server_pub_key),
            ) => Some((
                reply_to,
                Registered {
                    client_ip,
                    client_pub_key: client_pub_key.clone(),
                    server_host: server_host.clone(),
                    server_pub_key: server_pub_key.clone(),
                },
            )),
            _ => None,
        }
    })
})
.boxed()

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks

@huntc huntc marked this pull request as ready for review January 30, 2024 00:59
Copy link
Member

@patriknw patriknw left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@patriknw patriknw merged commit ad0a353 into main Feb 1, 2024
2 checks passed
@patriknw patriknw deleted the persist-event-if branch February 1, 2024 07:35
@patriknw patriknw added this to the 0.8.0 milestone Feb 1, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants