rabin před 4 dny
rodič
revize
32ebf1b0b8

+ 22 - 1
src/dai/seller/app/Lib/Channel.php

@@ -181,6 +181,7 @@ class Channel
                 'id'    => $id,
                 'used'  => (int)$redis->get("channel_usage_counter:$id"),
                 'limit_sec' => $c['limit_sec'],
+                'last_used' => (int)$redis->get("channel_last_used:$id"),
                 'channel_goods_id' => $c['channel_goods_id'] ?? null,
                 'discount' => $c['discount'] ?? null,
             ];
@@ -190,9 +191,12 @@ class Channel
             return false;
         }
 
-        // 优先选择使用次数最少的渠道,次数相同则优先限速更快的渠道
+        // 优先选择使用次数最少的渠道,次数相同则优先限速更快、最近最少使用的渠道
         usort($data, function ($a, $b) {
             if ($a['used'] === $b['used']) {
+                if ($a['limit_sec'] === $b['limit_sec']) {
+                    return $a['last_used'] <=> $b['last_used'];
+                }
                 return $a['limit_sec'] <=> $b['limit_sec'];
             }
             return $a['used'] <=> $b['used'];
@@ -202,6 +206,7 @@ class Channel
 
         // 计数 +1
         $redis->incr("channel_usage_counter:{$selected['id']}");
+        $redis->set("channel_last_used:{$selected['id']}", time());
 
         return [
             'channel_id' => $selected['id'],
@@ -210,6 +215,22 @@ class Channel
         ];
     }
 
+    /**
+     * 启动 Get 服务时重置渠道计数,避免历史状态影响调度
+     */
+    public function resetUsageCounters(): void
+    {
+        $channels = Dever::db('channel/info')->select(['status' => 1]) ?: [];
+        if (!$channels) {
+            return;
+        }
+        $redis = Redis::connect();
+        foreach ($channels as $channel) {
+            $redis->del("channel_usage_counter:{$channel['id']}");
+            $redis->del("channel_last_used:{$channel['id']}");
+        }
+    }
+
     /**
      * 渠道主动拉单
      * /api/channel/pull

+ 3 - 0
src/dai/seller/app/Lib/Get.php

@@ -19,6 +19,9 @@ class Get
                 echo "创建消费组失败:" . $e->getMessage() . "\n";
             }
         }
+
+        // 每次拉起 Worker 都重置渠道计数,确保调度公平
+        Dever::load(Channel::class)->resetUsageCounters();
         
         // 开始循环消费
         Timer::add(0.001, function() use ($workerId) {

+ 29 - 0
src/dai/seller/workerman.log

@@ -14617,3 +14617,32 @@ Stack trace:
 2025-11-18 17:26:59 pid:390049 Workerman[push.php] stopping
 2025-11-18 17:27:01 pid:390049 worker[none:390050] exit with status 9
 2025-11-18 17:27:01 pid:390049 Workerman[push.php] has been stopped
+2025-11-18 17:28:28 pid:396175 Workerman[get.php] start in DEBUG mode
+2025-11-18 17:29:37 pid:396452 Workerman[push.php] start in DEBUG mode
+2025-11-18 17:33:27 pid:396175 Workerman[get.php] received signal SIGINT
+2025-11-18 17:33:27 pid:396175 Workerman[get.php] stopping
+2025-11-18 17:33:28 pid:396175 Workerman[get.php] has been stopped
+2025-11-18 17:33:29 pid:397329 Workerman[get.php] start in DEBUG mode
+2025-11-18 17:33:59 pid:397329 Workerman[get.php] received signal SIGINT
+2025-11-18 17:33:59 pid:397329 Workerman[get.php] stopping
+2025-11-18 17:34:00 pid:397329 Workerman[get.php] has been stopped
+2025-11-18 17:34:01 pid:397448 Workerman[server.php] restart
+2025-11-18 17:34:01 pid:397448 Workerman[server.php] is stopping ...
+2025-11-18 17:34:01 pid:388254 Workerman[server.php] received signal SIGINT
+2025-11-18 17:34:01 pid:388254 Workerman[server.php] stopping
+2025-11-18 17:34:01 pid:388254 Workerman[server.php] has been stopped
+2025-11-18 17:34:01 pid:397448 Workerman[server.php] stop success
+2025-11-18 17:34:04 pid:397474 Workerman[get.php] start in DEBUG mode
+2025-11-18 17:34:06 pid:396452 Workerman[push.php] received signal SIGINT
+2025-11-18 17:34:06 pid:396452 Workerman[push.php] stopping
+2025-11-18 17:34:08 pid:396452 worker[none:396453] exit with status 9
+2025-11-18 17:34:08 pid:396452 Workerman[push.php] has been stopped
+2025-11-18 17:34:09 pid:397498 Workerman[push.php] start in DEBUG mode
+2025-11-18 17:35:47 pid:397474 Workerman[get.php] received signal SIGINT
+2025-11-18 17:35:47 pid:397474 Workerman[get.php] stopping
+2025-11-18 17:35:48 pid:397474 Workerman[get.php] has been stopped
+2025-11-18 17:35:49 pid:397880 Workerman[get.php] start in DEBUG mode
+2025-11-18 17:37:40 pid:397498 Workerman[push.php] received signal SIGINT
+2025-11-18 17:37:40 pid:397498 Workerman[push.php] stopping
+2025-11-18 17:37:42 pid:397498 worker[none:397499] exit with status 9
+2025-11-18 17:37:42 pid:397498 Workerman[push.php] has been stopped

+ 1 - 1
src/dai/seller/workerman.server.php.pid

@@ -1 +1 @@
-388254
+397450