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

API discoverability; align ourselves with the wider ecosystem #412

Merged
merged 7 commits into from Sep 28, 2021

Conversation

HippoBaro
Copy link
Member

What does this PR do?

  • Spawning tasks via a member function of Task<T> is not great from an
    API discovery point of view but also means we often need to use the
    starfish syntax such as Task::<()>::local(...). A free function makes
    it simpler for the type system to infer the return type of the Task to
    create. This makes our spawning API similar to that of Tokio for
    instance.
  • Functions such as create_task_queue intuitively relate to the executor
    and not to a Task. To make things easier, we define a free function
    executor() that is now included in the prelude. It returns a proxy
    struct to the LocalExecutor. This is where all executor functions
    accessible to the user will be located moving forward.

Once Rust gets better thread-local-storage we will remove this proxy
entirely and return a direct reference to the LocalExecutor itself. Alas,
we can't do that yet.

Motivation

What inspired you to submit this pull request?

Related issues

A list of issues either fixed, containing architectural discussions, otherwise relevant
for this Pull Request.

Additional Notes

Anything else we should know when reviewing?

Checklist

[] I have added unit tests to the code I am submitting
[] My unit tests cover both failure and success scenarios
[] If applicable, I have discussed my architecture

@glommer
Copy link
Collaborator

glommer commented Sep 16, 2021

On your first point, Local exists exactly to avoid the starfish notation. At this point I think Local is widespread enough that I would like to try and ping some known users like @thirstycrow and @bryandmc to make sure they are okay with any changes there (and for those API changes in general).

I agree about the second point about some of those functions naturally belonging to the executor. However is probably best to do with_local_executor, which is the same API as TLS uses.

I'll take a deeper look soon

examples/cooperative_preempt.rs Outdated Show resolved Hide resolved
examples/cooperative_preempt.rs Outdated Show resolved Hide resolved
@bryandmc
Copy link
Collaborator

I just wanna mention that I am gonna make some more extensive comments later today after work, but that I do have some comments.

@HippoBaro
Copy link
Member Author

@bryandmc Are you still planning to comment on this PR?

glommio/src/executor/mod.rs Outdated Show resolved Hide resolved
glommio/src/executor/mod.rs Show resolved Hide resolved
glommio/src/executor/mod.rs Show resolved Hide resolved
glommio/src/executor/mod.rs Outdated Show resolved Hide resolved
examples/cooperative_preempt.rs Outdated Show resolved Hide resolved
@HippoBaro HippoBaro force-pushed the task_refactor branch 3 times, most recently from ba73dd6 to 2ffbfac Compare September 27, 2021 19:14
@HippoBaro
Copy link
Member Author

@bryandmc for some reason the Re-request review github button won't work, so I'm writing this message to let you know I've made changes since your last comments. Let me know what you think!

// * glommio::executor().yield_task_queue_now(), works like yield_if_needed()
// but yields unconditionally.
//
// * glommio::executor().yield_now(), which unconditional yield the current
Copy link
Collaborator

Choose a reason for hiding this comment

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

isn't this the same as the one in futures_lite ? Why go through the executor ?

Copy link
Member Author

Choose a reason for hiding this comment

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

Just to be consistent with the other ones really

Copy link
Collaborator

Choose a reason for hiding this comment

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

A reader will automatically ask themselves "but what is the difference between that and futures::yield_now()`? So it's good to mention that it is the same, just under a different interface.

And while we are at it, let's add doc links

Copy link
Member Author

Choose a reason for hiding this comment

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

Alright, I did just that and made lots of corrections to our documentation throughout the crate while at it. We now have better, more uniform comments and documentation with more links as appropriate.

Spawning tasks via a member function of `Task<T>` is not great from an
API discovery point of view but also mean we often need to use the
starfish syntax such as `Task::<()>::local(...)`. A free function makes
it simpler for the type system to infer the return type of the `Task` to
create.

On a side node, it aligns us with tokio.
Rename them to `scoped_local` and `scoped_local_into`.
Functions such as `create_task_queue` intuitively relate to the executor
and not to a `Task`. To make things more easy, we define a free function
`executor()` that is now included in the prelude. It returns a proxy
struct to the `LocalExecutor`. This is where all executor functions
accessible to the user will be located moving forward.

Once Rust gets its act together wrt thread-local-storage we will remove
this proxy entirely and return a direct reference to the `LocalExecutor`
itself. Alas, we can't do that yet.
Also add a free function `yield_if_needed` since it's one of the most
used in Glommio apps.
Fixed a bunch of typos, grammar errors, capitalisation issues, etc.
Also promoted some comments to actual documentation where appropriate.
Added links to actual symbols too when it makes sense.
@HippoBaro HippoBaro merged commit fc0af8c into DataDog:master Sep 28, 2021
@HippoBaro HippoBaro deleted the task_refactor branch September 29, 2021 14:19
@bryandmc
Copy link
Collaborator

Glad to see you merged because I agree that it looks good!

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.

None yet

3 participants