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

Make Service async fn-compatible #176

Merged
merged 19 commits into from
Jan 3, 2024
Merged

Make Service async fn-compatible #176

merged 19 commits into from
Jan 3, 2024

Conversation

sfackler
Copy link
Member

Before this PR

The Service trait required that the call method's future be nameable and not borrow anything from self. This required lots of Arc cloning and manual future implementations.

After this PR

==COMMIT_MSG==
The Service trait now supports async fn call.
==COMMIT_MSG==

Using the new support added in Rust 1.75 (https://blog.rust-lang.org/2023/12/21/async-fn-rpit-in-traits.html), we can change Service's signature to allow the call method to be written as a normal async fn.

This significantly simplifies the internal service layers. Manual future implementations are now reserved for low level operations that can't be expressed with async/await.

Possible downsides?

Due to limitations of the initial implementation in the compiler, we currently require that the future returned by call is Send. In practice this isn't really a limitation since the future needs to be Send to work in a multithreaded runtime.

@changelog-app
Copy link

changelog-app bot commented Dec 29, 2023

Generate changelog in changelog/@unreleased

What do the change types mean?
  • feature: A new feature of the service.
  • improvement: An incremental improvement in the functionality or operation of the service.
  • fix: Remedies the incorrect behaviour of a component of the service in a backwards-compatible way.
  • break: Has the potential to break consumers of this service's API, inclusive of both Palantir services
    and external consumers of the service's API (e.g. customer-written software or integrations).
  • deprecation: Advertises the intention to remove service functionality without any change to the
    operation of the service itself.
  • manualTask: Requires the possibility of manual intervention (running a script, eyeballing configuration,
    performing database surgery, ...) at the time of upgrade for it to succeed.
  • migration: A fully automatic upgrade migration task with no engineer input required.

Note: only one type should be chosen.

How are new versions calculated?
  • ❗The break and manual task changelog types will result in a major release!
  • 🐛 The fix changelog type will result in a minor release in most cases, and a patch release version for patch branches. This behaviour is configurable in autorelease.
  • ✨ All others will result in a minor version release.

Type

  • Feature
  • Improvement
  • Fix
  • Break
  • Deprecation
  • Manual task
  • Migration

Description

The Service trait now supports async fn call.

Check the box to generate changelog(s)

  • Generate changelog entry


fn call(&self, req: R) -> Self::Future {
fn call(&self, req: R) -> impl Future<Output = Result<T::Response, T::Error>> {

Choose a reason for hiding this comment

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

Why can't we do

async fn call(&self), req: R) -> Result<T::Response, T::Error>

Copy link
Member Author

Choose a reason for hiding this comment

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

We could, but that would add an extra layer of indirection - it would end up making a wrapper future that looked something like

enum WrapperFuture<F> {
    Init,
    Awaiting(F),
}


return Poll::Ready(Err(error));
if log_body {

Choose a reason for hiding this comment

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

nit: I think we can avoid the mut on error

let error = if log_body { error.with_unsafe_param(....) } else { error }

Copy link
Member Author

Choose a reason for hiding this comment

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

That's true, but IMO it's a bit more confusing.

@sfackler sfackler merged commit 5b9fb2f into develop Jan 3, 2024
4 checks passed
@sfackler sfackler deleted the async-service branch January 3, 2024 13:55
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.

3 participants