AWS RDS代理无法自动关闭数据库连接。

huangapple go评论89阅读模式
英文:

AWS RDS Proxy not closing DB Connections on its own

问题

使用pgxpool作为客户端池的导入,还使用RDS代理进行池化。

文件夹结构

  • database
    > pghelper.go(在此处使用单例pgxPool.ConnectConfig(ctx, config)并将连接实例返回给所有处理程序)

  • handler
    > 在处理程序内调用连接池实例来运行DB查询

注意:根据Stackoverflow上的一些文档,在处理程序内或代码的任何地方都没有关闭实例。

这样做正确吗?

尽管在RDS代理中接收到超时,但客户端连接关闭了,但数据库连接仍未关闭。

设置的超时时间:

  1. Lambda:1分钟
  2. RDS代理:5分钟

我应该如何使池化按预期工作?我觉得池化没有发生,超时后数据库连接也没有关闭。

英文:

Using pgxpool as an import for Client Side Pooling, also using RDS Proxy for pooling

Folder Structure

  • database
    > pghelper.go (here used singleton pgxPool.ConnectConfig(ctx, config) and returning connect instance to all the handlers

  • handler
    > inside handler calling the connection Pool instance to run the DB query

Note: Not closing the instance inside the handler or anywhere in the code, based on few docs in the Stackoverflow

Is this the correct way?

As the ClientConnections are closing but the Database connections are not closing even after the timeouts are recieved in RDS Proxy

Timeouts set

  1. Lambda : 1 min
  2. RDS Proxy : 5 min

How should I make pooling work as expected as I feel pooling is not happening and neither the DB connections are not closing after timeouts?

答案1

得分: 6

有两种与RDS代理的连接方式:

  1. 应用程序与RDS代理之间的连接。
  2. RDS代理与RDS集群/实例之间的连接。

您在代理配置中设置的超时时间是针对第一种连接类型的。即使应用程序没有连接,代理仍会维护一组与实际数据库实例的连接。

> 超时后数据库连接没有关闭

似乎没有办法从AWS控制台更改此行为。但是,您可以使用AWS CLI中的modify-db-proxy-target-group命令来更改代理维护的空闲数据库连接的最大数量。

aws rds modify-db-proxy-target-group \
--target-group-name default \
--db-proxy-name <name-of-your-proxy> \
--connection-pool-config MaxConnectionsPercent=80,MaxIdleConnectionsPercent=20

这将限制对数据库的连接数为max_connections的80%,并将空闲连接限制为20%。

文档中提到,这些值默认为对数据库的连接限制为100%,对空闲连接的限制为50%。降低空闲连接的限制后,您应该会看到代理维护较少的空闲连接。

英文:

There 2 types of connections you have with RDS proxy

  1. Your application to RDS proxy
  2. RDS proxy to your RDS cluster/instance

The timeout you set in the proxy configuration is for the 1st type of connections. The proxy maintains a bunch of connections to the actual DB instance even if there are no connections from an application.

> DB connections are not closing after timeouts

There doesn't seem to be a way to change this behaviour from AWS console. But you can use the modify-db-proxy-target-group command in AWS CLI to change the max amount of Idle DatabaseConnections that the proxy maintains.

aws rds modify-db-proxy-target-group \
--target-group-name default \
--db-proxy-name &lt;name-of-your-proxy&gt; \
--connection-pool-config MaxConnectionsPercent=80,MaxIdleConnectionsPercent=20

This will limit the connections to Database to 80% of max_connections and limit idle connections to 20%

The documentation mentions that these values default to 100% as the limit for connections to DB and 50% for idle connections to the DB. After lowering the limit for idle connections you should start seeing the proxy maintain fewer idle connections

huangapple
  • 本文由 发表于 2021年12月24日 21:16:52
  • 转载请务必保留本文链接:https://go.coder-hub.com/70473309.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定