Skip to content

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

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

Explicit lightweight clone #170

Closed
akiradeveloper opened this issue Feb 22, 2021 · 1 comment
Closed

Explicit lightweight clone #170

akiradeveloper opened this issue Feb 22, 2021 · 1 comment
Labels

Comments

@akiradeveloper
Copy link
Owner

lol uses Arc everywhere to share an object in low cost. Bytes is also reference counted and lol uses it to zero copy buffers. Some other classes are cheap to clone too.

This is documented but should be clear by code.

The idea is like discussed here

rust-lang/rfcs#2588 (comment)

We define FastClone trait for these classes to make it clear the cloning is cheap.

@akiradeveloper
Copy link
Owner Author

akiradeveloper commented Feb 22, 2021

Let's list the types that is cheap to clone

  • Arc
  • Bytes
  • tonic::Channel (see below)
/// # Multiplexing requests
///
/// Sending a request on a channel requires a `&mut self` and thus can only send
/// one request in flight. This is intentional and is required to follow the `Service`
/// contract from the `tower` library which this channel implementation is built on
/// top of.
///
/// `tower` itself has a concept of `poll_ready` which is the main mechanism to apply
/// back pressure. `poll_ready` takes a `&mut self` and when it returns `Poll::Ready`
/// we know the `Service` is able to accept only one request before we must `poll_ready`
/// again. Due to this fact any `async fn` that wants to poll for readiness and submit
/// the request must have a `&mut self` reference.
///
/// To work around this and to ease the use of the channel, `Channel` provides a
/// `Clone` implementation that is _cheap_. This is because at the very top level
/// the channel is backed by a `tower_buffer::Buffer` which runs the connection
/// in a background task and provides a `mpsc` channel interface. Due to this
/// cloning the `Channel` type is cheap and encouraged.
#[derive(Clone)]
pub struct Channel {
    svc: Buffer<Svc, Request<BoxBody>>,
}

Repository owner locked and limited conversation to collaborators Sep 9, 2021

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

Labels
Projects
None yet
Development

No branches or pull requests

1 participant