在 Yii 2.0 中,浏览器发现无响应,进而重复请求的排查分析
1、在 Yii 2.0 中无响应,进而导致在浏览器中重复请求了 2 次。第 1 次无响应,第 2 次报错(程序禁止重复请求)。如图1
2、查看第 1 次请求的详细信息,在 标头 – 常规中,无响应状态码。如图2
3、查看第 1 次请求的详细信息,在 响应 中,无法加载响应数据。如图3
4、查看 Log Messages,在 E:\wwwroot\channel-pub-api\common\services\WeiboWeiboConnectWebAppAccessTokenService.php:92 处就停止往下执行了,实则应该继续执行才是。如图4
5、查看代码,第 92 行:$weiboWeiboConnectWebAppUserAccessToken->save()
public function saveModel($data) { /* 基于微博的微连接的网页应用的用户ID查找状态为启用的单个资源 */ $weiboWeiboConnectWebAppUserAccessToken = WeiboWeiboConnectWebAppUserAccessToken::find()->where(['weibo_weibo_connect_web_app_user_id' => $data['weiboWeiboConnectWebAppUserId']])->isDeletedNo()->enabled()->one(); if (!isset($weiboWeiboConnectWebAppUserAccessToken)) { $weiboWeiboConnectWebAppUserAccessToken = new WeiboWeiboConnectWebAppUserAccessToken(); } // 设置访问令牌的有效截止时间 $time = time(); $accessTokenExpireAt = $time + $data['expires_in'] + Yii::$app->params['weiboAuth']['weiboConnectWebApp']['authPeriod'] - Yii::$app->params['refreshToken']['timeOut']; $weiboWeiboConnectWebAppUserAccessToken->attributes = [ 'weibo_weibo_connect_web_app_id' => $data['weiboWeiboConnectWebAppId'], 'channel_app_source_id' => $data['channelAppSourceId'], 'channel_app_source_uuid' => $data['channelAppSourceUuid'], 'weibo_weibo_connect_web_app_user_id' => $data['weiboWeiboConnectWebAppUserId'], 'weibo_weibo_connect_web_app_user_uuid' => $data['weiboWeiboConnectWebAppUserUuid'], 'grant_type' => $data['grantType'], 'access_token' => $data['access_token'], 'expires_in' => $data['expires_in'], 'expires_at' => $accessTokenExpireAt, 'scope' => isset($data['scope']) ? $data['scope'] : '', 'user_id' => $data['uid'], 'status' => WeiboWeiboConnectWebAppUserAccessToken::STATUS_ENABLED, ]; if ($weiboWeiboConnectWebAppUserAccessToken->save()) { return ['status' => true, 'data' => ['channel_app_source_uuid' => $data['channelAppSourceUuid']]]; } elseif ($weiboWeiboConnectWebAppUserAccessToken->hasErrors()) { $firstError = ''; foreach ($weiboWeiboConnectWebAppUserAccessToken->getFirstErrors() as $message) { $firstError = $message; break; } return ['status' => false, 'code' => 214010, 'message' => Yii::t('error', Yii::t('error', Yii::t('error', '214010'), ['first_error' => $firstError]))]; } elseif (!$weiboWeiboConnectWebAppUserAccessToken->hasErrors()) { throw new ServerErrorHttpException('Failed to insert/update the object (microblogging micro-connected web application user\'s access token) for unknown reason.'); } }
6、在第 92 行之前打印对象:$weiboWeiboConnectWebAppUserAccessToken。打印结果如下。且第 1 次请求响应 200。如图5
common\logics\WeiboWeiboConnectWebAppUserAccessToken Object ( [_attributes:yii\db\BaseActiveRecord:private] => Array ( [weibo_weibo_connect_web_app_id] => 1 [channel_app_source_id] => 39 [channel_app_source_uuid] => c28915a6a8cb11ebba6254ee75d2ebc1 [weibo_weibo_connect_web_app_user_id] => 6 [weibo_weibo_connect_web_app_user_uuid] => c28a3efea8cb11ebb69354ee75d2ebc1 [grant_type] => authorization_code [access_token] => 2.00bAKoqCV9i7fEee243e798c0zzui2 [expires_in] => 2626935 [expires_at] => 1622394001 [scope] => [user_id] => 2612590013 [status] => 1 ) )
7、编辑代码,第 92 行:$weiboWeiboConnectWebAppUserAccessToken->save(false),保存记录之前不做验证(数据是可信任的),以避免验证时间过长,进而导致响应时间过长,进而浏览器重复请求的情况。注:此前,此模型为 Redis AR 模型,后来调整为 MySQL AR 模型。在浏览器查看其时长为:2.57 秒。如图6、图7
public function saveModel($data) { /* 基于微博的微连接的网页应用的用户ID查找状态为启用的单个资源 */ $weiboWeiboConnectWebAppUserAccessToken = WeiboWeiboConnectWebAppUserAccessToken::find()->where(['weibo_weibo_connect_web_app_user_id' => $data['weiboWeiboConnectWebAppUserId']])->isDeletedNo()->enabled()->one(); if (!isset($weiboWeiboConnectWebAppUserAccessToken)) { $weiboWeiboConnectWebAppUserAccessToken = new WeiboWeiboConnectWebAppUserAccessToken(); } // 设置访问令牌的有效截止时间 $time = time(); $accessTokenExpireAt = $time + $data['expires_in'] + Yii::$app->params['weiboAuth']['weiboConnectWebApp']['authPeriod'] - Yii::$app->params['refreshToken']['timeOut']; $weiboWeiboConnectWebAppUserAccessToken->attributes = [ 'weibo_weibo_connect_web_app_id' => $data['weiboWeiboConnectWebAppId'], 'channel_app_source_id' => $data['channelAppSourceId'], 'channel_app_source_uuid' => $data['channelAppSourceUuid'], 'weibo_weibo_connect_web_app_user_id' => $data['weiboWeiboConnectWebAppUserId'], 'weibo_weibo_connect_web_app_user_uuid' => $data['weiboWeiboConnectWebAppUserUuid'], 'grant_type' => $data['grantType'], 'access_token' => $data['access_token'], 'expires_in' => $data['expires_in'], 'expires_at' => $accessTokenExpireAt, 'scope' => isset($data['scope']) ? $data['scope'] : '', 'user_id' => $data['uid'], 'status' => WeiboWeiboConnectWebAppUserAccessToken::STATUS_ENABLED, ]; if ($weiboWeiboConnectWebAppUserAccessToken->save(false)) { return ['status' => true, 'data' => ['channel_app_source_uuid' => $data['channelAppSourceUuid']]]; } elseif ($weiboWeiboConnectWebAppUserAccessToken->hasErrors()) { $firstError = ''; foreach ($weiboWeiboConnectWebAppUserAccessToken->getFirstErrors() as $message) { $firstError = $message; break; } return ['status' => false, 'code' => 214010, 'message' => Yii::t('error', Yii::t('error', Yii::t('error', '214010'), ['first_error' => $firstError]))]; } elseif (!$weiboWeiboConnectWebAppUserAccessToken->hasErrors()) { throw new ServerErrorHttpException('Failed to insert/update the object (microblogging micro-connected web application user\'s access token) for unknown reason.'); } }
8、查看重复请求 2 次时的第 1 次请求的响应时间长度,皆在 2500 ms 以上。如图8
9、还原代码,第 92 行:$weiboWeiboConnectWebAppUserAccessToken->save()。当环境设置为:Production 时,也不会响应时间过长。在浏览器查看其时长为:1.56 秒。由此得出结论:验证与否固然会影响响应时长,但是其根本还是在于环境设置(Development)所导致。如图9
近期评论