Browse Source

update: rollback traffic fetch

tokumeikoi 3 years ago
parent
commit
3600c9a166
1 changed files with 6 additions and 8 deletions
  1. 6 8
      app/Services/UserService.php

+ 6 - 8
app/Services/UserService.php

@@ -82,17 +82,15 @@ class UserService
 
     public function trafficFetch(int $u, int $d, int $userId, object $server, string $protocol):bool
     {
-        $user = User::find($userId);
+        $user = User::lockForUpdate()
+            ->find($userId);
         if (!$user) {
             return true;
         }
-        try {
-            $user->update([
-                't' => time(),
-                'u' => DB::raw("u + {$u}"),
-                'd' => DB::raw("d + {$d}")
-            ]);
-        } catch (\Exception $e) {
+        $user->t = time();
+        $user->u = $user->u + $u;
+        $user->d = $user->d + $d;
+        if (!$user->save()) {
             return false;
         }
         $mailService = new MailService();