Skip to content

How to set Windows to return the memory occupied to the system immediately? #147

Description

@cjon123

Read the blob field from the database and output the image through the Axum web library. After the content is output, the memory occupied is not returned to the system.

use axum::{
extract::Path,
body::Body,
http::{header, StatusCode,Response},
response::IntoResponse,
routing::get,
Router,
};
use futures::StreamExt;
use sqlx::mysql::MySqlPoolOptions;
use sqlx::Row;

use mimalloc::MiMalloc;

#[global_allocator]
static GLOBAL: MiMalloc = MiMalloc;

#[tokio::main]
async fn main() -> anyhow::Result<()> {
let pool = MySqlPoolOptions::new()
.max_connections(5)
.connect("mysql://chating:123456@192.168.2.230:3306/jp8")
.await?;

let app = Router::new()
    .route("/image/{waiter}", get(get_image))
    .with_state(pool);

println!("Server running on http://127.0.0.1:3000");
axum::serve(
    tokio::net::TcpListener::bind("127.0.0.1:3000").await?,
    app,
)
.await?;

Ok(())

}

async fn get_image(
Path(id): Path,
state: axum::extract::Statesqlx::MySqlPool,
) -> impl IntoResponse {
// search MEDIUMBLOB field
let mut rows = sqlx::query("SELECT MIMG FROM chat_message WHERE id = ?")
.bind(id)
.fetch(&state.0);

if let Some(Ok(row)) = rows.next().await {
    let data: Vec<u8> = row.get("MIMG");
    let _body = Body::from(data);

    Response::builder()
        .header(header::CONTENT_TYPE, "image/jpeg")
        .body(_body)
        .unwrap()
} else {
    (StatusCode::NOT_FOUND, "Image not found").into_response()
}

}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions