-
Notifications
You must be signed in to change notification settings - Fork 271
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Reimplement the Lock middleware with tokio::sync
As described in tokio-rs/tokio#2237, the `tokio::sync::Semaphore` can hold unbounded memory, especially when the semaphor is being contested and consumers drop interest. Unfortunately, this use case is common in the proxy, especially when a destination service is unavailable and the proxy is timing out requests. This change reimplements the Lock middleware without using `tokio::sync::Semaphore`. This implementation is in some ways more naive and inefficient, but it appears to be better suited for the proxy's needs. Specifically, waiters are stored in a LIFO stack, which optimizes for minimizing latency. Under certain high-load scenarios, this Lock could be forced to grow its waiters set without cleaning up expired watchers. If this becomes a more serious concern, we could change the implementation to use a FIFO queue of waiters.
- Loading branch information
Showing
8 changed files
with
713 additions
and
396 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
pub use linkerd2_error::Error; | ||
use std::sync::Arc; | ||
|
||
#[derive(Debug)] | ||
pub struct Poisoned(()); | ||
|
||
#[derive(Debug)] | ||
pub struct ServiceError(Arc<Error>); | ||
|
||
// === impl POisoned === | ||
|
||
impl Poisoned { | ||
pub fn new() -> Self { | ||
Poisoned(()) | ||
} | ||
} | ||
|
||
impl std::fmt::Display for Poisoned { | ||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { | ||
write!(f, "poisoned") | ||
} | ||
} | ||
|
||
impl std::error::Error for Poisoned {} | ||
|
||
// === impl ServiceError === | ||
|
||
impl ServiceError { | ||
pub(crate) fn new(e: Arc<Error>) -> Self { | ||
ServiceError(e) | ||
} | ||
|
||
pub fn inner(&self) -> &Error { | ||
self.0.as_ref() | ||
} | ||
} | ||
|
||
impl std::fmt::Display for ServiceError { | ||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { | ||
self.0.fmt(f) | ||
} | ||
} | ||
|
||
impl std::error::Error for ServiceError {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
use super::Lock; | ||
|
||
#[derive(Clone, Debug, Default)] | ||
pub struct LockLayer(()); | ||
|
||
// === impl Layer === | ||
|
||
impl LockLayer { | ||
pub fn new() -> Self { | ||
LockLayer(()) | ||
} | ||
} | ||
|
||
impl<S> tower::layer::Layer<S> for LockLayer { | ||
type Service = Lock<S>; | ||
|
||
fn layer(&self, inner: S) -> Self::Service { | ||
Self::Service::new(inner) | ||
} | ||
} |
Oops, something went wrong.