Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

[BUG] 数据库 toArray 方法导致 cpu 密集协程长时间无法切换 #7557

Open
Labels
bugSomething isn't working
@liangchenwsl

Description

@liangchenwsl

Execute the command and paste the result below.

Command:uname -a && php -v && composer info | grep hyperf && php --ri swoole

Darwin MacBook-Pro-2.local 25.0.0 Darwin Kernel Version 25.0.0: Wed Sep 17 21:41:26 PDT 2025; root:xnu-12377.1.9~141/RELEASE_ARM64_T6041 arm64PHP 8.4.11 (cli) (built: Jul 29 2025 15:30:21) (NTS)Copyright (c) The PHP GroupBuilt by HomebrewZend Engine v4.4.11, Copyright (c) Zend Technologies    with Zend OPcache v8.4.11, Copyright (c), by Zend TechnologiesWarning: You should avoid overwriting already defined auth settingsfor github.com.96qbhy/hyperf-auth                      3.1.2              hyperf 的 auth 组件hyperf/amqp                             3.1.58             A amqplibfor hyperf.hyperf/async-queue                      3.1.57             A async queue componentfor hyperf.hyperf/cache                            3.1.57             A cache componentfor hyperf.hyperf/code-parser                      3.1.52             A code parser componentfor Hyperf.hyperf/codec                            3.1.61             A codec componentfor Hyperf.hyperf/collection                       3.1.61             Hyperf Collection package which come from illuminate/collectionshyperf/command                          3.1.56             Commandfor hyperfhyperf/conditionable                    3.1.42             Hyperf Macroable package which come from illuminate/conditionablehyperf/config                           3.1.57             An independent component that provides configuration container.hyperf/constants                        3.1.57             A constants componentfor hyperf.hyperf/context                          3.1.42             A coroutine/application context library.hyperf/contract                         3.1.42             The contracts of Hyperf.hyperf/coordinator                      3.1.42             Hyperf Coordinatorhyperf/coroutine                        3.1.54             Hyperf Coroutinehyperf/crontab                          3.1.58             A crontab componentfor Hyperf.hyperf/database                         3.1.61             A flexible database library.hyperf/db                               3.1.57            hyperf/db-connection                    3.1.57             A hyperf db connection handlerfor hyperf/database.hyperf/devtool                          3.1.57             A Devtoolfor Hyperf.hyperf/di                               3.1.61             A DIfor Hyperf.hyperf/dispatcher                       3.1.42             A HTTP Serverfor Hyperf.hyperf/engine-contract                  1.13.0             Contractfor Coroutine Enginehyperf/engine-swow                      2.14.0             Coroutine engine provided by swow.hyperf/event                            3.1.42             an event manager that implements PSR-14.hyperf/exception-handler                3.1.57             Exception handlerfor hyperfhyperf/framework                        3.1.42             A coroutine framework that focuses on hyperspeed and flexible, specifically usefor build microservices and middlewares.hyperf/guzzle                           3.1.58             Swoole coroutine handlerfor guzzlehyperf/http-message                     3.1.57             microservice framework base on swoolehyperf/http-server                      3.1.59             A HTTP Serverfor Hyperf.hyperf/laminas-mime                     3.0.0              Create and parse MIME messages and partshyperf/logger                           3.1.57             A logger componentfor hyperf.hyperf/macroable                        3.1.61             Hyperf Macroable package which come from illuminate/macroablehyperf/migration-generator              3.1.60             Migration generatorfor Hyperf.hyperf/model-cache                      3.1.57             A model cache componentfor hyperf.hyperf/model-listener                   3.1.57             A model listenerfor Hyperf.hyperf/odin                             1.1.58            hyperf/phar                             3.1.42             A component that supports pack Hyperf project as a Phar stand-alone package.hyperf/pipeline                         3.1.61             Hyperf Macroable package which come from illuminate/pipelinehyperf/pool                             3.1.58             An independent universal connection pool component.hyperf/process                          3.1.58             A process componentfor hyperf.hyperf/qdrant-client                    0.0.3             hyperf/rate-limit                       3.1.57             A rate limiter implementedfor Hyperf or other coroutine frameworkhyperf/redis                            3.1.59             A redis componentfor hyperf.hyperf/retry                            3.1.57             A retry componentfor hyperf.hyperf/serializer                       3.1.42             A serializer componentfor Hyperf.hyperf/server                           3.1.57             A base server libraryfor Hyperf.hyperf/snowflake                        3.1.57             A snowflake libraryhyperf/socketio-server                  3.1.58             Socket.io implementationfor hyperfhyperf/stdlib                           3.1.42             A stdlib componentfor Hyperf.hyperf/stringable                       3.1.60             Hyperf Stringable package which come from illuminate/supporthyperf/support                          3.1.61             A support componentfor Hyperf.hyperf/tappable                         3.1.42             Hyperf Macroable package which come from illuminate/tappablehyperf/testing                          3.1.57             Testingfor hyperfhyperf/token-bucket                     2.0.0              Implementation of the Token Bucket algorithm.hyperf/translation                      3.1.57             An independent translation component, forked by illuminate/translation.hyperf/utils                            3.1.42             A tools package that couldhelp developer solved the problem quickly.hyperf/validation                       3.1.60             hyperf validationhyperf/watcher                          3.1.60             Hot reload watcherfor Hyperfhyperf/websocket-client                 3.1.57             A websocket client libraryfor Hyperf.hyperf/websocket-server                 3.1.58             A websocket server libraryfor Hyperf.SwowStatus => enabledAuthor => Swow TeamLink => https://github.com/swow/swowContact => Twosee<twosee@php.net>Version => 1.6.1 ( NTS RELEASE )Built => Aug 13 2025 19:38:36Context => boost-contextScheduler => libuv-eventDebugLog => enabledOptional dependencies => Version InfoSanitizers => noneSSL => OpenSSL 3.5.2 5 Aug 2025 (built with OpenSSL 3.5.0 8 Apr 2025)cURL => libcurl/8.15.0 OpenSSL/3.5.2 zlib/1.2.12 brotli/1.1.0 zstd/1.5.7 AppleIDN libssh2/1.11.1 nghttp2/1.66.0 ngtcp2/1.15.0 nghttp3/1.11.0 librtmp/2.3 (built with 8.7.1)PostgreSQL => noneDirective => Local Value => Master Valueswow.enable => On => Onswow.async_threads => 0 => 0swow.async_file => On => Onswow.async_tty => On => Onswow.hook_pdo_pgsql => On => Oncurl.cainfo => no value => no value

Description:

数据库 orm 操作的 toarray 方法,在调用Hyperf\Database\Model\Model->attributesToArray()时会导致协程阻塞。100 条数据大约 cpu 密集 100ms,造成接口 卡顿,协程无法切换,系统整体承压能力下降。

以下是 trace:

Carbon\Carbon::{closure:Carbon\Traits\Comparison::matchFormatPattern():1083}(Array) |#3 /opt/www
/vendor/nesbot/carbon/src/Carbon/Traits/Comparison.php(1081): preg_replace_callback('/(?asDateTime('2025-10-06 15:5...') |#7 /opt/www/vendor/hyperf/databas
e/src/Model/Concerns/HasAttributes.php(122): Hyperf\Database\Model\Model->addDateAttributesToArray(Array) |#8 /opt/www/vendor/hyperf/database/src/Model/Mod
el.php(881): Hyperf\Database\Model\Model->attributesToArray() |#9 /opt/www/vendor/hyperf/collection/src/Traits/EnumeratesValues.php(939): Hyperf\Database\M
odel\Model->toArray() |#10 /opt/www/vendor/hyperf/collection/src/Collection.php(647): Hyperf\Collection\Collection->{closure:Hyperf\Collection\Traits\Enume
ratesValues::toArray():939}(Object(Repository\Model\TaskModel), 3659) |#11 /opt/www/vendor/hyperf/database/src/Model/
Collection.php(377): Hyperf\Collection\Collection->map(Object(Closure)) |#12 /opt/www/vendor/hyperf/collection/src/Traits/EnumeratesValues.php(939): Hyperf
\Database\Model\Collection->map(Object(Closure))

Steps To Reproduce:

 return $this->query()->get()->toArray();

目前我自测有 2 种不完美的方式来避免:1. 使用 Db::select($builder->toSql(), $builder->getBindings()); 2.重写getDates

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions


      [8]ページ先頭

      ©2009-2025 Movatter.jp