http - 如何将有状态客户端传递给 lambda 函数?

标签 http rust aws-lambda

我正在处理一个简单的 Lambda 函数,我想知道我是否可以将客户端(这次是 dynamodb)传递给处理程序,这样我们就不会为每个请求重新连接。

宏定义在这里:

https://docs.rs/lambda_http/0.1.1/lambda_http/macro.lambda.html 3

到目前为止我的功能:

fn main() -> Result<(), Box<dyn Error>> {
    simple_logger::init_with_level(log::Level::Debug)?;
    info!("Starting up...");
    let dynamodb_client = DynamoDbClient::new(Region::EuCentral1);
    lambda!(router);
    return Ok(());
}

fn router(req: Request, ctx: Context) -> Result<impl IntoResponse, HandlerError> {
let h_req = HReq {
    http_path: req.uri().path(),
    http_method: req.method(),
};

match h_req {
    HReq {
        http_path: "/login",
        http_method: &Method::POST,
    } => user_login(req, ctx),

    _ => {
        error!(
            "Not supported http method or path {}, {}",
            h_req.http_path, h_req.http_method
        );
        let mut resp = Response::default();
        *resp.status_mut() = StatusCode::METHOD_NOT_ALLOWED;
        Ok(resp)
    }
}

是否可以扩展此宏以提供第二个选项,以便我可以将客户端一直添加到实际与数据库对话的函数?

最佳答案

DynamoDB 是一种网络服务,对它的每个请求都被视为一个不同的 API 调用。

没有像使用常规数据库连接(例如 MySQL)那样保持客户端连接事件的功能。

我的 Rust 知识有点缺乏,所以我不知道 DynamoDBClient 是否默认设置了 http keepalive,但确保设置了 http keepalive 将有助于提高性能。

关于http - 如何将有状态客户端传递给 lambda 函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57372760/

相关文章:

javascript - AngularJs:嵌套状态不显示

http - 是否可以从 http Handle 函数写入 channel ?

python - 将文件从一个 s3 存储桶复制到另一个存储桶的最简单的 lambda 函数

http - Go 中 ResolveReference 时如何避免 URL 结尾斜杠被删除

http - 服务器端Dart中的跨域请求

multithreading - 在 Rust 中使用 Mutex 和 Condvar 进行缓冲

string - 预期&str发现有 rust 的炭?

rust - 如何修复 : value may contain references; add `' static` bound to `T`

python - 没有名为“cryptography.hazmat.bindings._padding”的模块

mysql - 无服务器在本地工作,但在部署时不工作