日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學無先后,達者為師

網站首頁 編程語言 正文

Laravel解決高并發問題的思路

作者:王中陽 更新時間: 2022-09-22 編程語言

問題復現

客戶端并發請求扣費接口,導致重復扣費;
服務端的加鎖邏輯不正確,鎖加到了非核心邏輯上,即加到了消費記錄表,而不是加到了核心業務表。

有問題的代碼

  1. 首次進入聊天房則扣費,再次進入不重復扣費
  2. 這個思路無法規避并發問題
    public function agoraToken(Request $request)
    {
        .
        .
        .
            try {
                DB::connection('footprint')->beginTransaction();
                if ($this->_userid == $appointmentInfo->userid) {
                    //如果已經存在則不重復扣費 但是并發問題會導致重復扣費
                    if (!AppointmentAction::existAction($this->_userid, $request->appointmentId, AppointmentAction::TYPE_ACTION_ENTER)) {
                        $propCount = UserConsume::getAppointmentSendConsumeCouponCount($this->_userid, $request->otherUserid);
                        $userConsume = new UserConsume($this->_userid);
                        Log::error("營業中約會 \n消費啤酒個數:" . $propCount . " \n用戶id:" . $this->_userid . " \n對方id:" . $request->otherUserid . " \n時間戳:" . time());
                        $userConsume->consumeCommon(CouponInfo::PROP_COUPON_CHAMPAGNE_ID, PropInfo::PROP_CHAMPAGNE_ID, $propCount);
                        DB::connection('footprint')->commit();
                    }
                }
                
                //記錄進入房間
                AppointmentAction::record($this->_userid, $request->appointmentId, AppointmentAction::TYPE_ACTION_ENTER);
            } catch (\Exception $exception) {
                Log::error("扣費失敗:" . $exception);
                DB::connection('footprint')->rollBack();
            }
        .
        .
        .
    }

優化后的代碼

  1. 引入事務,引入try catch
  2. 查詢約會數據加鎖,當未扣費時扣費,已扣費則修改扣費狀態
  3. 修改扣費狀態成功提交事務,釋放鎖
  4. 扣費失敗回滾事務
    public function agoraToken(Request $request)
    {
        .
        .
        .
        //try catch 捕獲異常,避免崩潰
            try {
                //開啟事務 因為只是事務中的鎖才生效 鎖包括lockForUpdate()和sharedLock()
                DB::connection('footprint')->beginTransaction();
                //校驗獲取token的合法性
                $appointmentInfo = AppointmentInfo::query()->selectRaw('id,userid,"inviteeUserid","prepareId",status,"isConsume"')
                    ->where('id', $request->appointmentId)->lockForUpdate()->first();
                if (empty($appointmentInfo) || !in_array($this->_userid, [$appointmentInfo->inviteeUserid, $appointmentInfo->userid])) {
                    return ErrorCode::TYPE_ILLEGAL_REQUEST;
                }
   
                //這里是關鍵 根據isConsume標記是否扣費了
                if ($appointmentInfo->isConsume == AppointmentInfo::TYPE_IS_NOT_CONSUME) {
                    $propCount = UserConsume::getAppointmentSendConsumeCouponCount($this->_userid, $request->otherUserid);
                    $userConsume = new UserConsume($this->_userid);
                    Log::error("營業中約會,個數:" . $propCount . " 用戶id:" . $this->_userid . ' 對方id:' . $request->otherUserid);
                    $userConsume->consumeCommon(CouponInfo::PROP_COUPON_CHAMPAGNE_ID, PropInfo::PROP_CHAMPAGNE_ID, $propCount);
                    //消費成功后修改isConsume的值,提交事務
                    $appointmentInfo->isConsume = AppointmentInfo::TYPE_IS_CONSUME;
                    $appointmentInfo->save();
                    DB::connection('footprint')->commit();
                }
                .
                .
                .
            } catch (\Exception $exception) {
                //異常情況打印log 回滾事務
                Log::error("約會扣費異常:" . \GuzzleHttp\json_encode($exception));
                DB::connection('footprint')->rollBack();
            }
        .
        .
        .
    }

期間的嘗試和思考

  1. 要給核心邏輯加鎖,進入聊天房只是一種記錄,而不是核心邏輯,不能作為鎖定條件

原文鏈接:https://blog.csdn.net/w425772719/article/details/120237037

欄目分類
最近更新