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

add atomic unsafe response type. enable async handler #449

Closed
wants to merge 33 commits into from

Conversation

fakeshadow
Copy link
Contributor

@fakeshadow fakeshadow commented Dec 1, 2020

PR Type

Feature

PR Checklist

Check your PR fulfills the following:

  • Tests for the changes have been added / updated.
  • Documentation comments have been added / updated.
  • A changelog entry has been made for the appropriate packages.
  • Format code with the latest stable rustfmt

Overview

AtomicResponse actually could enable borrowing Actor and it's Context in async/await.
This PR just use it with some unsafe code to enable async/await.

It's also possible to immutably borrowing Actor and it's Context in async/await without atomic,
by grouping shared reference responses and block actor on them.
It's basically a batched version of AtomicResponse.

Closes #308 #378 #438

@codecov

This comment has been minimized.

@fakeshadow
Copy link
Contributor Author

fakeshadow commented Dec 1, 2020

I'm not yet decided how to guard the input.
Introducing async_trait for a future proof of Handler trait is what I prefer but it could mean harm to SyncActor usage.
Use an extra closure would not break but with more boilerplate.

Edit: Use a closure for guarding input for now.

@robjtede
Copy link
Member

So what's the path forward for this PR. Is it ready for review and if so which parts should be focussed on.

A note after skimming over a couple files: a few fns are documented with "do not expose to public api". Are these fns better as unsafe? Also consider documenting the invariants that must be upheld regardless.

@fakeshadow
Copy link
Contributor Author

Hard to say it's ready as it's pretty hard to test the unsafes added. It would need some really edge cases to see if it holds up.

Context::wait_concurrent is just a method to push 'static lifetime future to actor's context. It's like actix_rt::spawn on an actor and they are safe. It' just not suggested to be used in public API.

The unsafe code only affect people who actually call ResponseAsync in handler. For people avoid using it there is no actual change to the code base other than a few more Vec::len check against an empty vector.

@fakeshadow
Copy link
Contributor Author

Making this a draft for now until proper tests are added.

@fakeshadow fakeshadow marked this pull request as draft January 1, 2021 05:05
@fakeshadow fakeshadow marked this pull request as ready for review March 1, 2021 23:54
@fakeshadow
Copy link
Contributor Author

fakeshadow commented Mar 1, 2021

I have reverted most changes related to actor context. Making AsyncResponse an AtomicResponse similar feature that uses unsafe to permit borrowing actor and context in async block.
It's supposed to work exactly like AtomicResponse but async/await friendly.

@robjtede
Copy link
Member

robjtede commented Mar 2, 2021

definitely excited about this but i think we should do a release without it first

@fakeshadow
Copy link
Contributor Author

Agreed. I'm not confident in this either.

There are still small issues to this approach so it's more like an experiment for the Concurrent variant AsyncResponse.

@fakeshadow
Copy link
Contributor Author

fakeshadow commented Mar 4, 2021

After some additional tests I find there is no way to actively go into stopping state during an async response actor future.

@robjtede
Copy link
Member

lmk if you want a review of this now

@fakeshadow
Copy link
Contributor Author

@robjtede I think this is ready for review.

@robjtede robjtede self-requested a review March 23, 2021 12:39
@fakeshadow
Copy link
Contributor Author

I don't have time to keep this PR up to date anymore. It's welcomed to step in and take charge of it.

@fakeshadow fakeshadow marked this pull request as draft May 29, 2021 12:21
@fakeshadow fakeshadow closed this May 29, 2021
@fakeshadow fakeshadow deleted the atomic-unsafe branch May 29, 2021 22:23
fn handle(self, ctx: &mut A::Context, tx: Option<OneshotSender<M::Result>>) {
match self {
AsyncResponse::Atomic(fut) => {
ctx.wait(fut.map(move |res, _, _| tx.send(res)));
Copy link
Member

Choose a reason for hiding this comment

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

Sorry for reviving this....

Isn't this unsound already ? You have two &mut A::Context alive at the same time, one given to the method and the other inside the future.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Use of actor reference on Async Actor handle
3 participants