-
Notifications
You must be signed in to change notification settings - Fork 1
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
Conversation
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.
There was a problem hiding this 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> |
There was a problem hiding this comment.
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)
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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()
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
thanks
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
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.