自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

官方限流組件的兩種異常正確處理方式

開發(fā) 前端
我們想要的HTTP狀態(tài)碼是429?,響應(yīng)信息是每個手機(jī)號一天最多5條短信?。而這里是500,對應(yīng)的錯誤信息是Internal Server Error。

概述

官方限流組件webman限流器,支持注解限流。支持apcu、redis、memory驅(qū)動。

文檔:https://www.workerman.net/doc/webman/components/rate-limiter.html

接口限流器

參考如下代碼

class IndexController
{
    /**
     * @param Request $request
     * @return Response
     */
    public function sendSms(Request $request): Response
    {
        $mobile = $request->get('mobile', '1388888888');
        Limiter::check($mobile, 5, 24*60*60, '每個手機(jī)號一天最多5條短信');
        return response_json('短信發(fā)送成功');
    }
}

成功響應(yīng)

HTTP/1.1 200 OK
Content-Type: application/json;charset=UTF-8

{
    "code": 0,
    "message": "短信發(fā)送成功",
    "data": []
}

異常響應(yīng)

HTTP/1.1 200 OK
Content-Type: application/json;charset=UTF-8

每個手機(jī)號一天最多5條短信

可以看出,限流器會拋出異常,返回的響應(yīng)信息就是異常的message。并不是我們想要的統(tǒng)一的響應(yīng)格式。

這里我們使用異常插件:https://www.workerman.net/plugin/16 接管框架的默認(rèn)異常處理,保證響應(yīng)格式統(tǒng)一。

修改配置文件config/exception.php

return [
    // 這里配置異常處理類
    '' => \Tinywan\ExceptionHandler\Handler::class,
];

再次請求接口,可以看到異常信息已經(jīng)被統(tǒng)一處理了。

HTTP/1.1 500 Error
Content-Type: application/json;charset=UTF-8

{
 "code": 0,
 "msg": "Internal Server Error",
 "data": {}
}

但是還不是我們想要的限流異常信息,我們想要的HTTP狀態(tài)碼是429,響應(yīng)信息是每個手機(jī)號一天最多5條短信。而這里是500,對應(yīng)的錯誤信息是Internal Server Error。

接著繼續(xù)改造代碼。通過try-catch捕獲異常,然后返回自定義的響應(yīng)信息。

use Tinywan\ExceptionHandler\Exception\TooManyRequestsHttpException;

class IndexController
{
    /**
     * @param Request $request
     * @return Response
     * @throws TooManyRequestsHttpException
     */
    public function sendSms(Request $request): Response
    {
        $mobile = $request->get('mobile', '1388888888');
        try {
            Limiter::check($mobile, 5, 24*60*60, '每個手機(jī)號一天最多5條短信');
        } catch (\Throwable $throwable) {
            throw new TooManyRequestsHttpException($throwable->getMessage());
        }
        return response_json('短信發(fā)送成功');
    }
}

再次請求接口,可以看到是我們想要的結(jié)果信息了。HTTP狀態(tài)碼是429,響應(yīng)信息是每個手機(jī)號一天最多5條短信。

HTTP/1.1 429 Too Many Requests
Content-Type: application/json;charset=UTF-8

{
 "code": 0,
 "msg": "每個手機(jī)號一天最多5條短信",
 "data": {}
}

注解限流器

注解限流器使用起來更加簡單,只需要在控制器方法上添加注解即可。

use Webman\RateLimiter\Annotation\RateLimiter;

class IndexController
{
    /**
     * @param Request $request
     * @return Response
     */
    #[RateLimiter(3, 60, [IndexController::class, 'getMobile'], '每個手機(jī)號一天最多5條短信!')]
    public function sendSms(Request $request): Response
    {
        return response_json('短信發(fā)送成功');
    }

    /**
     * @desc 自定義key,獲取手機(jī)號,必須是靜態(tài)方法
     * @return string
     */
    public static function getMobile(): string
    {
        return request()->get('mobile','1388888888');
    }
}

請求接口,可以看到異常信息已經(jīng)被統(tǒng)一處理了。

但不是我們想要的限流異常信息,我們想要的HTTP狀態(tài)碼是429,響應(yīng)信息是每個手機(jī)號一天最多5條短信。而這里是500,對應(yīng)的錯誤信息是Internal Server Error。

HTTP/1.1 500 Error
Content-Type: application/json;charset=UTF-8

{
 "code": 0,
 "msg": "Internal Server Error",
 "data": {}
}

繼續(xù)改造代碼。通過自定義異常類限流器的異常,然后返回自定義的響應(yīng)信息。

這里修改注解的第五個參數(shù),指定異常類為自定義的異常類 Tinywan\ExceptionHandler\Exception\TooManyRequestsHttpException:class

/**
 * @param Request $request
 * @return Response
 */
#[RateLimiter(3, 60, [IndexController::class, 'getMobile'], '每個手機(jī)號一天最多5條短信!', TooManyRequestsHttpException::class)]
public function sendSms(Request $request): Response
{
   return response_json('短信發(fā)送成功');
}

再次請求接口,可以看到是我們想要的結(jié)果信息了。HTTP狀態(tài)碼是429,響應(yīng)信息是每個手機(jī)號一天最多5條短信。

HTTP/1.1 429 Too Many Requests
Content-Type: application/json;charset=UTF-8

{
 "code": 0,
 "msg": "每個手機(jī)號一天最多5條短信",
 "data": {}
}
責(zé)任編輯:武曉燕 來源: 開源技術(shù)小棧
相關(guān)推薦

2009-08-17 17:28:23

C#轉(zhuǎn)義字符

2009-08-19 17:30:38

C#轉(zhuǎn)義字符

2017-10-10 15:30:20

JavaScript

2011-07-08 10:57:24

主域控制器額外域控制器AD

2023-07-10 08:00:13

架構(gòu)Rest返回值

2021-03-31 09:11:27

URLErrorHTTPError

2010-11-12 11:48:15

2010-08-31 09:31:58

Silverlight

2011-04-06 12:41:41

Java異常

2011-03-03 10:26:04

Pureftpd

2010-11-24 08:54:33

2021-05-27 10:57:01

TCP定時器網(wǎng)絡(luò)協(xié)議

2009-06-25 13:43:00

Buffalo AJA

2010-10-21 16:24:18

sql server升

2010-08-06 09:38:11

Flex讀取XML

2023-03-29 13:06:36

2010-09-07 11:09:59

2023-11-09 08:14:07

時間窗口限流

2009-12-02 09:49:43

PHP Ajax亂碼

2010-07-27 15:03:37

Flex ArrayC
點贊
收藏

51CTO技術(shù)棧公眾號