【问题标题】:Flask, gunicorn, psycopg2 - OperationalError SSL error: decryption failed or bad record macFlask、gunicorn、psycopg2 - OperationalError SSL 错误:解密失败或坏记录 mac
【发布时间】:2020-08-05 14:29:37
【问题描述】:

我有 gunicorn 使用类中的静态方法运行带有 ThreadedConnectionPool 作为单例属性的同步工作者。我正在使用 apscheduler 的 BackgroundScheduler 在 master 中运行一些后台作业。当一个作业在主服务器上运行并且一个工作人员超时(由于大量的数据库调用),我开始在新工作人员的一些后续请求中收到 500 个错误,这些请求调用数据库,错误为psycopg2.OperationalError: SSL error: decryption failed or bad record Mac

这是我的代码。

class WriterDbConnectionPool:
    __pool = None

    @staticmethod
    def get_db_connection_pool():
        if not WriterDbConnectionPool.__pool:
            logger.info("WriterDbConnectionPool initialising connection pool")
            db_config = get_db_configs()
            WriterDbConnectionPool.__pool = ThreadedConnectionPool(1, 10,
                             database=xxx,
                             user=xxx,
                             password=xxx,
                             host=xxx,
                             port=xxx
                             )

        return WriterDbConnectionPool.__pool

    @staticmethod
    def close():
        if WriterDbConnectionPool.__pool:
            logger.info("WriterDbConnectionPool closing connection pool")
            WriterDbConnectionPool.__pool.closeall()
            WriterDbConnectionPool.__pool = None

我正在使用以下选项运行 gunicorn

-k sync
--workers 3
--timeout 120
--bind 0.0.0.0:5000
--config gunicorn_config.py
--access-logfile -
--access-logformat
"%(p)s %(h)s %(l)s %(r)s %(s)s %(b)s %(f)s %(a)s %(L)s"
app:app

调用 DbConnectionPool 调用的代码如下

@contextmanager
def get_db_connection():
    try:
        connection = DbConnectionPool.get_db_connection_pool().getconn()
        yield connection
    finally:
        DbConnectionPool.get_db_connection_pool().putconn(connection)

我该如何解决这个问题?我应该使用不同的工人阶级吗?

我的 psycopg2 版本是 2.7.5,flask 是 0.12.2,gunicorn 是 20.0.4

【问题讨论】:

  • 我好像没有在连接上设置任何 SSL 选项?
  • @snakecharmerb 根据 postgres 文档默认是首选 ssl - postgresql.org/docs/9.6/…

标签: python flask gunicorn psycopg2


【解决方案1】:

我所做的是将 apscheduler 移到一个单独的 python 进程中,并使用 BlockingScheduler 运行我的后台作业。

【讨论】:

    猜你喜欢
    • 2017-12-26
    • 1970-01-01
    • 1970-01-01
    • 2014-05-10
    • 2013-07-05
    • 1970-01-01
    • 2021-03-30
    • 2015-06-13
    • 2021-11-19
    相关资源
    最近更新 更多