fix: memory leak of workspace middleware (#337)

This commit is contained in:
Nathan.fooo 2024-02-21 13:15:19 +08:00 committed by GitHub
parent cbcf43081f
commit df258c6ed3
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 11 additions and 2 deletions

View File

@ -10,6 +10,8 @@ use async_trait::async_trait;
use futures_util::future::LocalBoxFuture;
use actix_web::web::Data;
use dashmap::DashMap;
use once_cell::sync::Lazy;
use std::collections::HashMap;
use std::future::{ready, Ready};
use std::ops::{Deref, DerefMut};
@ -20,6 +22,8 @@ use crate::state::AppState;
use app_error::AppError;
use uuid::Uuid;
static RESOURCE_DEF_CACHE: Lazy<DashMap<String, ResourceDef>> = Lazy::new(DashMap::new);
#[derive(Debug, Clone, Eq, PartialEq, Hash)]
pub enum AccessResource {
Workspace,
@ -176,11 +180,16 @@ where
fn call(&self, mut req: ServiceRequest) -> Self::Future {
let path = req.match_pattern().map(|pattern| {
let resource_ref = ResourceDef::new(pattern);
// Create ResourceDef will cause memory leak, so we use the cache to store the ResourceDef
let mut path = req.match_info().clone();
resource_ref.capture_match_info(&mut path);
RESOURCE_DEF_CACHE
.entry(pattern.to_owned())
.or_insert_with(|| ResourceDef::new(pattern))
.value()
.capture_match_info(&mut path);
path
});
match path {
None => {
let fut = self.service.call(req);