Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BUG] 在redis中配置 Redis::OPT_SERIALIZER => Redis::SERIALIZER_JSON 会导致限流组件(hyperf/rate-limit)失效,永远提示已被限流 #7003

Closed
luoyou opened this issue Aug 15, 2024 · 3 comments
Labels
bug Something isn't working

Comments

@luoyou
Copy link
Contributor

luoyou commented Aug 15, 2024

Description:

在config/autoload/redis.php 中配置 Redis::OPT_SERIALIZER => Redis::SERIALIZER_JSON,如下图:

<?php

declare(strict_types=1);

use function Hyperf\Support\env;

return [
    'default' => [
        'host' => env('REDIS_HOST', 'localhost'),
        'auth' => env('REDIS_AUTH', null),
        'port' => (int) env('REDIS_PORT', 6379),
        'db' => (int) env('REDIS_DB', 0),
        'pool' => [
            'min_connections' => 1,
            'max_connections' => 10,
            'connect_timeout' => 10.0,
            'wait_timeout' => 3.0,
            'heartbeat' => -1,
            'max_idle_time' => (float) env('REDIS_MAX_IDLE_TIME', 60),
        ],
        'options' => [
            Redis::OPT_SERIALIZER => Redis::SERIALIZER_JSON,   // 加入此行配置,就会存在问题
            Redis::OPT_READ_TIMEOUT => -1,
        ],
    ],
];

因为vendor/hyperf/rate-limit/src/Aspect/RateLimitAnnotationAspect.php文件中第67-73行进行了try catch并且没有对外抛错,导致错误一直正确的抛出来,调试了很久,建议此处也正确的将错误抛出来,就不用排查那么久了……

原因是vendor/hyperf/rate-limit/src/Storage/RedisStorage.php文件中第90-92行:$data = DoublePacker::pack($microtime);在pack之后的值存入redis中是空字符串导致的问题

@luoyou luoyou added the bug Something isn't working label Aug 15, 2024
@luoyou luoyou changed the title [BUG] 在redis中配置 Redis::OPT_SERIALIZER => Redis::SERIALIZER_JSON 会导致限流组件失效,永远提示已被限流 [BUG] 在redis中配置 Redis::OPT_SERIALIZER => Redis::SERIALIZER_JSON 会导致限流组件(hyperf/rate-limit)失效,永远提示已被限流 Aug 15, 2024
@xuanyanwow
Copy link
Member

你都已经定位好问题了,不考虑一下自己提交PR吗 (* ̄︶ ̄)

@luoyou
Copy link
Contributor Author

luoyou commented Aug 29, 2024

你都已经定位好问题了,不考虑一下自己提交PR吗 (* ̄︶ ̄)

我想改的,但是问题是在redis组件中,redis组件七拐八绕,没有设计文档,不敢轻易下手

@luoyou
Copy link
Contributor Author

luoyou commented Aug 29, 2024

你都已经定位好问题了,不考虑一下自己提交PR吗 (* ̄︶ ̄)

刚刚又研究了下,有解决方案,可以在redis的配置文件中新增一套配置给rate-limit用即可。
不过还是可以优化一下报错,检测到用户配置了序列化抛出正确的错误,不然下次遇到问题又要排查很久……

@luoyou luoyou closed this as completed Aug 29, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants