From bfc1d639b59ed3788bfbad696e1f35a79b3d7730 Mon Sep 17 00:00:00 2001 From: rikasai233 Date: Sat, 31 Aug 2024 13:02:09 +0800 Subject: [PATCH] feat: use db pool --- FasterRunner/settings/dev.py | 18 +++- FasterRunner/settings/docker.py | 12 ++- FasterRunner/settings/pro.py | 12 ++- poetry.lock | 147 +++++++++++++++++++++++++++++++- pyproject.toml | 1 + requirements.txt | 3 +- 6 files changed, 179 insertions(+), 14 deletions(-) diff --git a/FasterRunner/settings/dev.py b/FasterRunner/settings/dev.py index 88938b22..e64801dd 100644 --- a/FasterRunner/settings/dev.py +++ b/FasterRunner/settings/dev.py @@ -23,19 +23,31 @@ DATABASES = { "default": { - "ENGINE": "django.db.backends.mysql", + "ENGINE": "dj_db_conn_pool.backends.mysql", "NAME": "fast", # 新建数据库 # 'NAME': 'fast_mb4', # 新建数据库名 "HOST": "127.0.0.1", "USER": "root", # 数据库登录名 "PASSWORD": "root", # 数据库登录密码 - "OPTIONS": {"charset": "utf8mb4"}, - # 单元测试数据库 + 'OPTIONS': { + 'charset': 'utf8mb4', + }, + 'POOL_OPTIONS': { + 'POOL_SIZE': 20, + 'MAX_OVERFLOW': 20, + 'RECYCLE': 24 * 60 * 60, + 'PRE_PING': True, + 'ECHO': False, + 'TIMEOUT': 30, + }, "TEST": { "NAME": "test_fast_last", # 测试过程中会生成名字为test的数据库,测试结束后Django会自动删除该数据库 }, } } + + + # IM_REPORT_SETTING.update({'platform_name': '银河飞梭测试平台'}) BROKER_URL = "amqp://username:password@localhost:5672//" diff --git a/FasterRunner/settings/docker.py b/FasterRunner/settings/docker.py index 9f9d3453..9d0d8cdd 100644 --- a/FasterRunner/settings/docker.py +++ b/FasterRunner/settings/docker.py @@ -47,16 +47,20 @@ DATABASES = { "default": { - "ENGINE": "django.db.backends.mysql", + "ENGINE": "dj_db_conn_pool.backends.mysql", "HOST": DB_HOST, "PORT": DB_PORT, "NAME": DB_NAME, # 新建数据库名 "USER": DB_USER, # 数据库登录名 "PASSWORD": DB_PASSWORD, # 数据库登录密码 "OPTIONS": {"charset": "utf8mb4"}, - "TEST": { - # 'MIRROR': 'default', # 单元测试时,使用default的配置 - # 'DEPENDENCIES': ['default'] + 'POOL_OPTIONS': { + 'POOL_SIZE': 20, + 'MAX_OVERFLOW': 20, + 'RECYCLE': 24 * 60 * 60, + 'PRE_PING': True, + 'ECHO': False, + 'TIMEOUT': 30, }, } } diff --git a/FasterRunner/settings/pro.py b/FasterRunner/settings/pro.py index 1b68d81e..c1474e27 100644 --- a/FasterRunner/settings/pro.py +++ b/FasterRunner/settings/pro.py @@ -46,15 +46,19 @@ DATABASES = { "default": { - "ENGINE": "django.db.backends.mysql", + "ENGINE": "dj_db_conn_pool.backends.mysql", "HOST": MYSQL_HOST, "NAME": DB_NAME, # 新建数据库名 "USER": DB_USER, # 数据库登录名 "PASSWORD": DB_PASSWORD, # 数据库登录密码 "OPTIONS": {"charset": "utf8mb4"}, - "TEST": { - # 'MIRROR': 'default', # 单元测试时,使用default的配置 - # 'DEPENDENCIES': ['default'] + 'POOL_OPTIONS': { + 'POOL_SIZE': 20, + 'MAX_OVERFLOW': 20, + 'RECYCLE': 24 * 60 * 60, + 'PRE_PING': True, + 'ECHO': False, + 'TIMEOUT': 30, }, } } diff --git a/poetry.lock b/poetry.lock index aec00c0b..17bac8de 100644 --- a/poetry.lock +++ b/poetry.lock @@ -1,4 +1,4 @@ -# This file is automatically @generated by Poetry 1.8.1 and should not be changed by hand. +# This file is automatically @generated by Poetry 1.8.3 and should not be changed by hand. [[package]] name = "aiocontextvars" @@ -476,6 +476,33 @@ files = [ asgiref = ">=3.6" Django = ">=3.2" +[[package]] +name = "django-db-connection-pool" +version = "1.2.5" +description = "Database connection pool component library for Django" +optional = false +python-versions = ">=3.4" +files = [ + {file = "django-db-connection-pool-1.2.5.tar.gz", hash = "sha256:693d588c98689415b87a43aa2dc6fc66a2fca36519e889332811867a8579768b"}, + {file = "django_db_connection_pool-1.2.5-py3-none-any.whl", hash = "sha256:33ec059b8b81801ac23c11903827e89ade5c1383774b0848773ac08b72bc65e6"}, +] + +[package.dependencies] +Django = ">=2.0" +mysqlclient = {version = ">=1.3.0", optional = true, markers = "extra == \"mysql\""} +SQLAlchemy = ">=1.4.24" +sqlparams = ">=4.0.0" + +[package.extras] +all = ["Django (>=2.0)", "JPype1 (>=1.3.0)", "SQLAlchemy (>=1.4.24)", "cx-Oracle (>=6.4.1)", "mysqlclient (>=1.3.0)", "psycopg2 (>=2.8.6)", "pyodbc (>=4.0.34)", "sqlparams (>=3.0.0)"] +jdbc = ["JPype1 (>=1.3.0)"] +mysql = ["mysqlclient (>=1.3.0)"] +odbc = ["pyodbc (>=4.0.34)"] +oracle = ["cx-Oracle (>=6.4.1)"] +postgresql = ["psycopg2 (>=2.8.6)"] +psycopg2 = ["psycopg2 (>=2.8.6)"] +psycopg3 = ["psycopg (>=3)"] + [[package]] name = "django-filter" version = "2.4.0" @@ -1109,6 +1136,24 @@ files = [ {file = "MarkupSafe-1.1.1.tar.gz", hash = "sha256:29872e92839765e546828bb7754a68c418d927cd064fd4708fab9fe9c8bb116b"}, ] +[[package]] +name = "mysqlclient" +version = "2.2.4" +description = "Python interface to MySQL" +optional = false +python-versions = ">=3.8" +files = [ + {file = "mysqlclient-2.2.4-cp310-cp310-win_amd64.whl", hash = "sha256:ac44777eab0a66c14cb0d38965572f762e193ec2e5c0723bcd11319cc5b693c5"}, + {file = "mysqlclient-2.2.4-cp311-cp311-win_amd64.whl", hash = "sha256:329e4eec086a2336fe3541f1ce095d87a6f169d1cc8ba7b04ac68bcb234c9711"}, + {file = "mysqlclient-2.2.4-cp312-cp312-win_amd64.whl", hash = "sha256:e1ebe3f41d152d7cb7c265349fdb7f1eca86ccb0ca24a90036cde48e00ceb2ab"}, + {file = "mysqlclient-2.2.4-cp38-cp38-win_amd64.whl", hash = "sha256:3c318755e06df599338dad7625f884b8a71fcf322a9939ef78c9b3db93e1de7a"}, + {file = "mysqlclient-2.2.4-cp39-cp39-win_amd64.whl", hash = "sha256:9d4c015480c4a6b2b1602eccd9846103fc70606244788d04aa14b31c4bd1f0e2"}, + {file = "mysqlclient-2.2.4-pp310-pypy310_pp73-win_amd64.whl", hash = "sha256:d43987bb9626096a302ca6ddcdd81feaeca65ced1d5fe892a6a66b808326aa54"}, + {file = "mysqlclient-2.2.4-pp38-pypy38_pp73-win_amd64.whl", hash = "sha256:4e80dcad884dd6e14949ac6daf769123223a52a6805345608bf49cdaf7bc8b3a"}, + {file = "mysqlclient-2.2.4-pp39-pypy39_pp73-win_amd64.whl", hash = "sha256:9d3310295cb682232cadc28abd172f406c718b9ada41d2371259098ae37779d3"}, + {file = "mysqlclient-2.2.4.tar.gz", hash = "sha256:33bc9fb3464e7d7c10b1eaf7336c5ff8f2a3d3b88bab432116ad2490beb3bf41"}, +] + [[package]] name = "openapi-codec" version = "1.3.2" @@ -1582,6 +1627,104 @@ files = [ {file = "six-1.15.0.tar.gz", hash = "sha256:30639c035cdb23534cd4aa2dd52c3bf48f06e5f4a941509c8bafd8ce11080259"}, ] +[[package]] +name = "sqlalchemy" +version = "2.0.32" +description = "Database Abstraction Library" +optional = false +python-versions = ">=3.7" +files = [ + {file = "SQLAlchemy-2.0.32-cp310-cp310-macosx_10_9_x86_64.whl", hash = "sha256:0c9045ecc2e4db59bfc97b20516dfdf8e41d910ac6fb667ebd3a79ea54084619"}, + {file = "SQLAlchemy-2.0.32-cp310-cp310-macosx_11_0_arm64.whl", hash = "sha256:1467940318e4a860afd546ef61fefb98a14d935cd6817ed07a228c7f7c62f389"}, + {file = "SQLAlchemy-2.0.32-cp310-cp310-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:5954463675cb15db8d4b521f3566a017c8789222b8316b1e6934c811018ee08b"}, + {file = "SQLAlchemy-2.0.32-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:167e7497035c303ae50651b351c28dc22a40bb98fbdb8468cdc971821b1ae533"}, + {file = "SQLAlchemy-2.0.32-cp310-cp310-musllinux_1_2_aarch64.whl", hash = "sha256:b27dfb676ac02529fb6e343b3a482303f16e6bc3a4d868b73935b8792edb52d0"}, + {file = "SQLAlchemy-2.0.32-cp310-cp310-musllinux_1_2_x86_64.whl", hash = "sha256:bf2360a5e0f7bd75fa80431bf8ebcfb920c9f885e7956c7efde89031695cafb8"}, + {file = "SQLAlchemy-2.0.32-cp310-cp310-win32.whl", hash = "sha256:306fe44e754a91cd9d600a6b070c1f2fadbb4a1a257b8781ccf33c7067fd3e4d"}, + {file = "SQLAlchemy-2.0.32-cp310-cp310-win_amd64.whl", hash = "sha256:99db65e6f3ab42e06c318f15c98f59a436f1c78179e6a6f40f529c8cc7100b22"}, + {file = "SQLAlchemy-2.0.32-cp311-cp311-macosx_10_9_x86_64.whl", hash = "sha256:21b053be28a8a414f2ddd401f1be8361e41032d2ef5884b2f31d31cb723e559f"}, + {file = "SQLAlchemy-2.0.32-cp311-cp311-macosx_11_0_arm64.whl", hash = "sha256:b178e875a7a25b5938b53b006598ee7645172fccafe1c291a706e93f48499ff5"}, + {file = "SQLAlchemy-2.0.32-cp311-cp311-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:723a40ee2cc7ea653645bd4cf024326dea2076673fc9d3d33f20f6c81db83e1d"}, + {file = "SQLAlchemy-2.0.32-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:295ff8689544f7ee7e819529633d058bd458c1fd7f7e3eebd0f9268ebc56c2a0"}, + {file = "SQLAlchemy-2.0.32-cp311-cp311-musllinux_1_2_aarch64.whl", hash = "sha256:49496b68cd190a147118af585173ee624114dfb2e0297558c460ad7495f9dfe2"}, + {file = "SQLAlchemy-2.0.32-cp311-cp311-musllinux_1_2_x86_64.whl", hash = "sha256:acd9b73c5c15f0ec5ce18128b1fe9157ddd0044abc373e6ecd5ba376a7e5d961"}, + {file = "SQLAlchemy-2.0.32-cp311-cp311-win32.whl", hash = "sha256:9365a3da32dabd3e69e06b972b1ffb0c89668994c7e8e75ce21d3e5e69ddef28"}, + {file = "SQLAlchemy-2.0.32-cp311-cp311-win_amd64.whl", hash = "sha256:8bd63d051f4f313b102a2af1cbc8b80f061bf78f3d5bd0843ff70b5859e27924"}, + {file = "SQLAlchemy-2.0.32-cp312-cp312-macosx_10_9_x86_64.whl", hash = "sha256:6bab3db192a0c35e3c9d1560eb8332463e29e5507dbd822e29a0a3c48c0a8d92"}, + {file = "SQLAlchemy-2.0.32-cp312-cp312-macosx_11_0_arm64.whl", hash = "sha256:19d98f4f58b13900d8dec4ed09dd09ef292208ee44cc9c2fe01c1f0a2fe440e9"}, + {file = "SQLAlchemy-2.0.32-cp312-cp312-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:3cd33c61513cb1b7371fd40cf221256456d26a56284e7d19d1f0b9f1eb7dd7e8"}, + {file = "SQLAlchemy-2.0.32-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:7d6ba0497c1d066dd004e0f02a92426ca2df20fac08728d03f67f6960271feec"}, + {file = "SQLAlchemy-2.0.32-cp312-cp312-musllinux_1_2_aarch64.whl", hash = "sha256:2b6be53e4fde0065524f1a0a7929b10e9280987b320716c1509478b712a7688c"}, + {file = "SQLAlchemy-2.0.32-cp312-cp312-musllinux_1_2_x86_64.whl", hash = "sha256:916a798f62f410c0b80b63683c8061f5ebe237b0f4ad778739304253353bc1cb"}, + {file = "SQLAlchemy-2.0.32-cp312-cp312-win32.whl", hash = "sha256:31983018b74908ebc6c996a16ad3690301a23befb643093fcfe85efd292e384d"}, + {file = "SQLAlchemy-2.0.32-cp312-cp312-win_amd64.whl", hash = "sha256:4363ed245a6231f2e2957cccdda3c776265a75851f4753c60f3004b90e69bfeb"}, + {file = "SQLAlchemy-2.0.32-cp37-cp37m-macosx_10_9_x86_64.whl", hash = "sha256:b8afd5b26570bf41c35c0121801479958b4446751a3971fb9a480c1afd85558e"}, + {file = "SQLAlchemy-2.0.32-cp37-cp37m-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:c750987fc876813f27b60d619b987b057eb4896b81117f73bb8d9918c14f1cad"}, + {file = "SQLAlchemy-2.0.32-cp37-cp37m-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:ada0102afff4890f651ed91120c1120065663506b760da4e7823913ebd3258be"}, + {file = "SQLAlchemy-2.0.32-cp37-cp37m-musllinux_1_2_aarch64.whl", hash = "sha256:78c03d0f8a5ab4f3034c0e8482cfcc415a3ec6193491cfa1c643ed707d476f16"}, + {file = "SQLAlchemy-2.0.32-cp37-cp37m-musllinux_1_2_x86_64.whl", hash = "sha256:3bd1cae7519283ff525e64645ebd7a3e0283f3c038f461ecc1c7b040a0c932a1"}, + {file = "SQLAlchemy-2.0.32-cp37-cp37m-win32.whl", hash = "sha256:01438ebcdc566d58c93af0171c74ec28efe6a29184b773e378a385e6215389da"}, + {file = "SQLAlchemy-2.0.32-cp37-cp37m-win_amd64.whl", hash = "sha256:4979dc80fbbc9d2ef569e71e0896990bc94df2b9fdbd878290bd129b65ab579c"}, + {file = "SQLAlchemy-2.0.32-cp38-cp38-macosx_10_9_x86_64.whl", hash = "sha256:6c742be912f57586ac43af38b3848f7688863a403dfb220193a882ea60e1ec3a"}, + {file = "SQLAlchemy-2.0.32-cp38-cp38-macosx_11_0_arm64.whl", hash = "sha256:62e23d0ac103bcf1c5555b6c88c114089587bc64d048fef5bbdb58dfd26f96da"}, + {file = "SQLAlchemy-2.0.32-cp38-cp38-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:251f0d1108aab8ea7b9aadbd07fb47fb8e3a5838dde34aa95a3349876b5a1f1d"}, + {file = "SQLAlchemy-2.0.32-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:0ef18a84e5116340e38eca3e7f9eeaaef62738891422e7c2a0b80feab165905f"}, + {file = "SQLAlchemy-2.0.32-cp38-cp38-musllinux_1_2_aarch64.whl", hash = "sha256:3eb6a97a1d39976f360b10ff208c73afb6a4de86dd2a6212ddf65c4a6a2347d5"}, + {file = "SQLAlchemy-2.0.32-cp38-cp38-musllinux_1_2_x86_64.whl", hash = "sha256:0c1c9b673d21477cec17ab10bc4decb1322843ba35b481585facd88203754fc5"}, + {file = "SQLAlchemy-2.0.32-cp38-cp38-win32.whl", hash = "sha256:c41a2b9ca80ee555decc605bd3c4520cc6fef9abde8fd66b1cf65126a6922d65"}, + {file = "SQLAlchemy-2.0.32-cp38-cp38-win_amd64.whl", hash = "sha256:8a37e4d265033c897892279e8adf505c8b6b4075f2b40d77afb31f7185cd6ecd"}, + {file = "SQLAlchemy-2.0.32-cp39-cp39-macosx_10_9_x86_64.whl", hash = "sha256:52fec964fba2ef46476312a03ec8c425956b05c20220a1a03703537824b5e8e1"}, + {file = "SQLAlchemy-2.0.32-cp39-cp39-macosx_11_0_arm64.whl", hash = "sha256:328429aecaba2aee3d71e11f2477c14eec5990fb6d0e884107935f7fb6001632"}, + {file = "SQLAlchemy-2.0.32-cp39-cp39-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:85a01b5599e790e76ac3fe3aa2f26e1feba56270023d6afd5550ed63c68552b3"}, + {file = "SQLAlchemy-2.0.32-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:aaf04784797dcdf4c0aa952c8d234fa01974c4729db55c45732520ce12dd95b4"}, + {file = "SQLAlchemy-2.0.32-cp39-cp39-musllinux_1_2_aarch64.whl", hash = "sha256:4488120becf9b71b3ac718f4138269a6be99a42fe023ec457896ba4f80749525"}, + {file = "SQLAlchemy-2.0.32-cp39-cp39-musllinux_1_2_x86_64.whl", hash = "sha256:14e09e083a5796d513918a66f3d6aedbc131e39e80875afe81d98a03312889e6"}, + {file = "SQLAlchemy-2.0.32-cp39-cp39-win32.whl", hash = "sha256:0d322cc9c9b2154ba7e82f7bf25ecc7c36fbe2d82e2933b3642fc095a52cfc78"}, + {file = "SQLAlchemy-2.0.32-cp39-cp39-win_amd64.whl", hash = "sha256:7dd8583df2f98dea28b5cd53a1beac963f4f9d087888d75f22fcc93a07cf8d84"}, + {file = "SQLAlchemy-2.0.32-py3-none-any.whl", hash = "sha256:e567a8793a692451f706b363ccf3c45e056b67d90ead58c3bc9471af5d212202"}, + {file = "SQLAlchemy-2.0.32.tar.gz", hash = "sha256:c1b88cc8b02b6a5f0efb0345a03672d4c897dc7d92585176f88c67346f565ea8"}, +] + +[package.dependencies] +greenlet = {version = "!=0.4.17", markers = "python_version < \"3.13\" and (platform_machine == \"aarch64\" or platform_machine == \"ppc64le\" or platform_machine == \"x86_64\" or platform_machine == \"amd64\" or platform_machine == \"AMD64\" or platform_machine == \"win32\" or platform_machine == \"WIN32\")"} +typing-extensions = ">=4.6.0" + +[package.extras] +aiomysql = ["aiomysql (>=0.2.0)", "greenlet (!=0.4.17)"] +aioodbc = ["aioodbc", "greenlet (!=0.4.17)"] +aiosqlite = ["aiosqlite", "greenlet (!=0.4.17)", "typing_extensions (!=3.10.0.1)"] +asyncio = ["greenlet (!=0.4.17)"] +asyncmy = ["asyncmy (>=0.2.3,!=0.2.4,!=0.2.6)", "greenlet (!=0.4.17)"] +mariadb-connector = ["mariadb (>=1.0.1,!=1.1.2,!=1.1.5)"] +mssql = ["pyodbc"] +mssql-pymssql = ["pymssql"] +mssql-pyodbc = ["pyodbc"] +mypy = ["mypy (>=0.910)"] +mysql = ["mysqlclient (>=1.4.0)"] +mysql-connector = ["mysql-connector-python"] +oracle = ["cx_oracle (>=8)"] +oracle-oracledb = ["oracledb (>=1.0.1)"] +postgresql = ["psycopg2 (>=2.7)"] +postgresql-asyncpg = ["asyncpg", "greenlet (!=0.4.17)"] +postgresql-pg8000 = ["pg8000 (>=1.29.1)"] +postgresql-psycopg = ["psycopg (>=3.0.7)"] +postgresql-psycopg2binary = ["psycopg2-binary"] +postgresql-psycopg2cffi = ["psycopg2cffi"] +postgresql-psycopgbinary = ["psycopg[binary] (>=3.0.7)"] +pymysql = ["pymysql"] +sqlcipher = ["sqlcipher3_binary"] + +[[package]] +name = "sqlparams" +version = "6.1.0" +description = "Convert between various DB API 2.0 parameter styles." +optional = false +python-versions = ">=3.8" +files = [ + {file = "sqlparams-6.1.0-py3-none-any.whl", hash = "sha256:454beae170183836429013dfdfcca6eb168ec06168f332c8658cccaf6452de15"}, + {file = "sqlparams-6.1.0.tar.gz", hash = "sha256:a74d4d60f07a0bd06a6f4251db07ece512af512b363f718b72bfbd4883499a29"}, +] + [[package]] name = "sqlparse" version = "0.3.1" @@ -1794,4 +1937,4 @@ testing = ["coverage (>=5.0.3)", "zope.event", "zope.testing"] [metadata] lock-version = "2.0" python-versions = "^3.11" -content-hash = "8b4b2d4aba69e72ec2fbd998cacbaa63b0eb116ab16f099e959143df5658b689" +content-hash = "ac773d3f062fb44bb6b18e680aea727501510cf72afb14bd7f4aa4a29fdc1e67" diff --git a/pyproject.toml b/pyproject.toml index bb817c07..377a93a6 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -71,6 +71,7 @@ django-auth-ldap = "2.3.0" pymysql = "1.1.0" pytest = "^8.1.1" pytest-django = "^4.8.0" +django-db-connection-pool = {extras = ["mysql"], version = "^1.2.5"} [tool.poetry.group.dev.dependencies] diff --git a/requirements.txt b/requirements.txt index 5fc17a80..ebcb83db 100644 --- a/requirements.txt +++ b/requirements.txt @@ -61,4 +61,5 @@ django-filter==2.4.0,<2.5.0 #django-auth-ldap==2.3.0 pymysql==1.1.0 pytest==8.1.1 -pytest-django==4.8.0 \ No newline at end of file +pytest-django==4.8.0 +django-db-connection-pool[mysql]==1.2.5