为什么我的Heroku应用程序的PostgreSQL数据库上有打开的连接?我该如何关闭它们?

时间:2021-05-15 07:34:35

My Heroku app is www.inflationtrends.com.

我的Heroku应用程序是www.inflationtrends.com。

Usually, when I run "pg:info" in Git Bash to see how many connections there are, that number is zero.

通常,当我在Git Bash中运行“pg:info”以查看有多少连接时,该数字为零。

Recently, I've seen a spike in traffic -- not much, only a little over 1,000 in the past 48 hours -- and when I ran "pg:info" this morning (around 11 a.m. Eastern time), the result shows that there are 4 or 5 open connections.

最近,我看到交通量激增 - 不多,过去48小时只有一点点 - 当我今天早上(东部时间上午11点左右)跑“pg:info”时,结果显示有4或5个开放连接。

My app is run using the Ruby gem Sinatra. In the Sinatra file, I have the following code:

我的应用程序使用Ruby gem Sinatra运行。在Sinatra文件中,我有以下代码:

after do
    DB.disconnect
end

The "after do" loop disconnects from the PostgreSQL database after a page is loaded.

加载页面后,“after do”循环与PostgreSQL数据库断开连接。

The variable "DB" has the connection info for my PostgreSQL database (username, password, host, port number, SSL mode requirement):

变量“DB”具有我的PostgreSQL数据库的连接信息(用户名,密码,主机,端口号,SSL模式要求):

DB = Sequel.postgres(
        db_name,
        :user=>user,
        :password=>password,
        :host=>host,
        :port=>port,
        :sslmode=>sslmode
    )

Is there some reason that there are open connections? Are there ways to close these connections? Are there more efficient ways to handle this situation?

是否存在开放连接的原因?有没有办法关闭这些连接?有更有效的方法来处理这种情况吗?

3 个解决方案

#1


3  

An alternate way to check the number of open connections on Heroku is to type this into your console/terminal and replace "myapp" with your app's name:

检查Heroku上打开连接数的另一种方法是在控制台/终端中键入此内容,并将“myapp”替换为您的应用程序名称:

heroku pg:info -a myapp

heroku pg:info -a myapp

#2


0  

Have you considered that perhaps your site is getting traffic? When people visit your site and use your application connections will be opened.

您是否认为您的网站可能正在增加流量?当人们访问您的站点并使用您的应用程序时,将打开连接。

Try adding some tracking code (such as Google Analytics) to your web pages, then check if the number of recorded visitors matches the number of open connections.

尝试将一些跟踪代码(例如Google Analytics)添加到您的网页,然后检查记录的访问者数量是否与打开的连接数相匹配。

It is also possible that the database has connections opened by various maintenance tasks, such as backing up.

数据库也可能通过各种维护任务(例如备份)打开连接。

#3


0  

I grabbed the following toolbelt add-on which worked perfectly.

我抓住了以下工具带附件,它完美无缺。

https://github.com/heroku/heroku-pg-extras#usage

heroku pg:killall --app xyz

heroku pg:killall --app xyz

#1


3  

An alternate way to check the number of open connections on Heroku is to type this into your console/terminal and replace "myapp" with your app's name:

检查Heroku上打开连接数的另一种方法是在控制台/终端中键入此内容,并将“myapp”替换为您的应用程序名称:

heroku pg:info -a myapp

heroku pg:info -a myapp

#2


0  

Have you considered that perhaps your site is getting traffic? When people visit your site and use your application connections will be opened.

您是否认为您的网站可能正在增加流量?当人们访问您的站点并使用您的应用程序时,将打开连接。

Try adding some tracking code (such as Google Analytics) to your web pages, then check if the number of recorded visitors matches the number of open connections.

尝试将一些跟踪代码(例如Google Analytics)添加到您的网页,然后检查记录的访问者数量是否与打开的连接数相匹配。

It is also possible that the database has connections opened by various maintenance tasks, such as backing up.

数据库也可能通过各种维护任务(例如备份)打开连接。

#3


0  

I grabbed the following toolbelt add-on which worked perfectly.

我抓住了以下工具带附件,它完美无缺。

https://github.com/heroku/heroku-pg-extras#usage

heroku pg:killall --app xyz

heroku pg:killall --app xyz