Files
greptimedb/src/servers/src/grpc/memory_limit.rs
jeremyhi 6a6b34c709 feat!: memory limiter unification write path (#7437)
* feat: remove option max_in_flight_write_bytes

Signed-off-by: jeremyhi <fengjiachun@gmail.com>

* feat: replace RequestMemoryLimiter

Signed-off-by: jeremyhi <fengjiachun@gmail.com>

* chore: add integration test

Signed-off-by: jeremyhi <fengjiachun@gmail.com>

* chore: fix test

Signed-off-by: jeremyhi <fengjiachun@gmail.com>

* fix: by AI comment

Signed-off-by: jeremyhi <fengjiachun@gmail.com>

* refactor: global permit pool on writing

Signed-off-by: jeremyhi <fengjiachun@gmail.com>

* chore: by ai comment

Signed-off-by: jeremyhi <fengjiachun@gmail.com>

---------

Signed-off-by: jeremyhi <fengjiachun@gmail.com>
2025-12-23 02:18:49 +00:00

73 lines
2.1 KiB
Rust

// Copyright 2023 Greptime Team
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
use std::task::{Context, Poll};
use futures::future::BoxFuture;
use tonic::server::NamedService;
use tower::{Layer, Service};
use crate::request_memory_limiter::ServerMemoryLimiter;
#[derive(Clone)]
pub struct MemoryLimiterExtensionLayer {
limiter: ServerMemoryLimiter,
}
impl MemoryLimiterExtensionLayer {
pub fn new(limiter: ServerMemoryLimiter) -> Self {
Self { limiter }
}
}
impl<S> Layer<S> for MemoryLimiterExtensionLayer {
type Service = MemoryLimiterExtensionService<S>;
fn layer(&self, service: S) -> Self::Service {
MemoryLimiterExtensionService {
inner: service,
limiter: self.limiter.clone(),
}
}
}
#[derive(Clone)]
pub struct MemoryLimiterExtensionService<S> {
inner: S,
limiter: ServerMemoryLimiter,
}
impl<S: NamedService> NamedService for MemoryLimiterExtensionService<S> {
const NAME: &'static str = S::NAME;
}
impl<S, ReqBody> Service<http::Request<ReqBody>> for MemoryLimiterExtensionService<S>
where
S: Service<http::Request<ReqBody>>,
S::Future: Send + 'static,
{
type Response = S::Response;
type Error = S::Error;
type Future = BoxFuture<'static, Result<Self::Response, Self::Error>>;
fn poll_ready(&mut self, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
self.inner.poll_ready(cx)
}
fn call(&mut self, mut req: http::Request<ReqBody>) -> Self::Future {
req.extensions_mut().insert(self.limiter.clone());
Box::pin(self.inner.call(req))
}
}