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

Rethink the hook API #364

Open
bikeshedder opened this issue Nov 11, 2024 · 0 comments
Open

Rethink the hook API #364

bikeshedder opened this issue Nov 11, 2024 · 0 comments
Labels
A-core Area: Core / deadpool enhancement New feature or request quality of life Improvements to the API resulting in a better quality of life for the users of deadpool.

Comments

@bikeshedder
Copy link
Owner

Working with hooks is somewhat annoying. It gets worse when mixing it with sync code. See:

#363 (reply in thread)

Rather than using a closure API it might actually make sense to move to a trait API like that:

trait AsyncHook<T> {
    fn hook(
        &self,
        obj: &mut Self::Type,
        metrics: &Metrics,
    ) -> impl Future<Output = Result<(), HookError> + Send;
}

That would also allow writing reuseable hooks in a more concise way:

struct MaxAgeHook {
   max_age: Duration
}

impl MaxAgeHook {
    pub fn new(max_age: Duration) -> Self {
        Self { max_age }
    }
}

impl AsyncHook<T> for MaxAgeHook<T> {
    async fn hook(&self, _: T, metrics: Metrics) -> Result<(), HookError> {
       if metrics.age() > self.max_age {
            return Err(HookError::message("Discard object that reached its maximum age");
       }
       Ok(())
    }
}

Also the separation between async_fn and sync_fn was done to allow for some optimizations that makes it possible to skip any extra allocations needed because of boxed futures. As it turns out sync_fn sounds a lot like it allows blocking code to be written while it's actually just meant for code that doesn't need to call await.

@bikeshedder bikeshedder added enhancement New feature or request A-core Area: Core / deadpool quality of life Improvements to the API resulting in a better quality of life for the users of deadpool. labels Nov 11, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-core Area: Core / deadpool enhancement New feature or request quality of life Improvements to the API resulting in a better quality of life for the users of deadpool.
Projects
None yet
Development

No branches or pull requests

1 participant