Compare commits

...

2 Commits

Author SHA1 Message Date
zhouwei e87d180063 回收机器人 2026-02-27 19:19:12 +08:00
zhouwei 115a607adc 修改机器人并发问题 2026-02-27 19:04:53 +08:00
3 changed files with 144 additions and 46 deletions

View File

@ -562,35 +562,65 @@ public class UpdatePlayRoomJob implements Job{
public int getRobot(int maxPlayers)
{
long freeRobotNum = Redis.use("group1_db1").scard("free_robot");
if (freeRobotNum < maxPlayers)
{
long useRobotNum = Redis.use("group1_db1").scard("used_robot");
if (useRobotNum >= 100)
{
logger.warn("robot not enough, cur used robot " + useRobotNum);
Jedis jedis = Redis.use("group1_db1").getJedis();
String lockValue = null;
try {
//按房间类型区分
String lockKey = "robot_allocation_lock_" + maxPlayers;
lockValue = System.currentTimeMillis() + "_" + Thread.currentThread().getId();
boolean locked = false;
for (int i = 0; i < 50; i++) {
String result = jedis.set(lockKey, lockValue, "NX", "EX", 15);
if ("OK".equals(result)) {
locked = true;
break;
}
Thread.sleep(50);
}
try {
createRobot();
freeRobotNum = Redis.use("group1_db1").scard("free_robot");
if (freeRobotNum < maxPlayers)
if (!locked) {
logger.warn("获取机器人分配锁超时maxPlayers:" + maxPlayers + ", 返回0");
return 0;
}
//检查是否有足够的机器人
long freeRobotNum = jedis.scard("free_robot");
long usedRobotNum = jedis.scard("used_robot");
logger.info("机器人资源状态 - 可用:" + freeRobotNum + ", 已使用:" + usedRobotNum + ", 需要:" + maxPlayers);
if (freeRobotNum < maxPlayers)
{
long useRobotNum = jedis.scard("used_robot");
logger.warn("机器人数量不足,当前可用:" + freeRobotNum + ", 已使用:" + useRobotNum + ", 需要:" + maxPlayers);
//如果机器人总数接近上限
if (useRobotNum + freeRobotNum >= 100)
{
logger.error("create robot failed............");
logger.warn("机器人总数接近上限:" + (useRobotNum + freeRobotNum) + ", 考虑增加机器人容量");
}
try {
createRobot();
freeRobotNum = jedis.scard("free_robot");
if (freeRobotNum < maxPlayers)
{
logger.error("创建机器人后仍然不足,可用:" + freeRobotNum + ", 需要:" + maxPlayers);
return 0;
}
logger.info("成功创建机器人,当前可用:" + freeRobotNum);
}
catch (Exception e)
{
logger.error("创建机器人时发生异常: " + e.getMessage(), e);
return 0;
}
}
catch (Exception e)
{
logger.error(e);
}
}
try {
while(freeRobotNum > 0)
{
String strRobotId = Redis.use("group1_db1").spop("free_robot");
freeRobotNum = Redis.use("group1_db1").scard("free_robot");
String strRobotId = jedis.spop("free_robot");
freeRobotNum = jedis.scard("free_robot");
if (StringUtil.isEmpty(strRobotId))
{
createRobot();
@ -626,7 +656,7 @@ public class UpdatePlayRoomJob implements Job{
}
}
Redis.use("group1_db1").sadd("used_robot", strRobotId);
jedis.sadd("used_robot", strRobotId);
int robotId = Integer.parseInt(strRobotId);
String fakeHp = Redis.use("group1_db10").hget("fake_"+strRobotId, "fake_hp");
if (StringUtil.isEmpty(fakeHp))
@ -641,6 +671,21 @@ public class UpdatePlayRoomJob implements Job{
{
logger.error(e);
}
finally {
// 释放锁
try {
if (lockValue != null) {
String lockKey = "robot_allocation_lock_" + maxPlayers;
String currentValue = jedis.get(lockKey);
if (lockValue.equals(currentValue)) {
jedis.del(lockKey);
}
}
} catch (Exception e) {
logger.error("释放机器人分配锁时出错: " + e.getMessage());
}
jedis.close();
}
return 0;
}

View File

@ -507,6 +507,14 @@ public class UpdateRobotRoomJob implements Job{
String roomId = room_list.get(3);
int time = Integer.parseInt(roomtime);
if (time+20 < sttime) {
//回收机器人
for (int i = 0; i < players.size(); i++) {
int player_id = players.getInt(i);
Redis.use("group1_db1").srem("used_robot", Integer.toString(player_id));
Redis.use("group1_db1").sadd("free_robot", Integer.toString(player_id));
logger.info("回收机器人: " + player_id + " 到可用池");
}
js2.hset(allrobotkeys+gameid,players.get(0)+"",0+"");
//解散房间
GroupPublisherService.delRoomEvt(groupId, roomId);
@ -711,35 +719,65 @@ public class UpdateRobotRoomJob implements Job{
public int getRobot(int maxPlayers)
{
long freeRobotNum = Redis.use("group1_db1").scard("free_robot");
if (freeRobotNum < maxPlayers)
{
long useRobotNum = Redis.use("group1_db1").scard("used_robot");
if (useRobotNum >= 100)
{
logger.warn("robot not enough, cur used robot " + useRobotNum);
Jedis jedis = Redis.use("group1_db1").getJedis();
String lockValue = null;
try {
//按房间类型区分
String lockKey = "robot_allocation_lock_" + maxPlayers;
lockValue = System.currentTimeMillis() + "_" + Thread.currentThread().getId();
boolean locked = false;
for (int i = 0; i < 50; i++) {
String result = jedis.set(lockKey, lockValue, "NX", "EX", 15);
if ("OK".equals(result)) {
locked = true;
break;
}
Thread.sleep(50);
}
try {
createRobot();
freeRobotNum = Redis.use("group1_db1").scard("free_robot");
if (freeRobotNum < maxPlayers)
if (!locked) {
logger.warn("获取机器人分配锁超时maxPlayers:" + maxPlayers + ", 返回0");
return 0;
}
//检查是否有足够的机器人
long freeRobotNum = jedis.scard("free_robot");
long usedRobotNum = jedis.scard("used_robot");
logger.info("机器人资源状态 - 可用:" + freeRobotNum + ", 已使用:" + usedRobotNum + ", 需要:" + maxPlayers);
if (freeRobotNum < maxPlayers)
{
long useRobotNum = jedis.scard("used_robot");
logger.warn("机器人数量不足,当前可用:" + freeRobotNum + ", 已使用:" + useRobotNum + ", 需要:" + maxPlayers);
//如果机器人总数接近上限
if (useRobotNum + freeRobotNum >= 100)
{
logger.error("create robot failed............");
return 0;
logger.warn("机器人总数接近上限:" + (useRobotNum + freeRobotNum) + ", 考虑增加机器人容量");
}
}
catch (Exception e)
{
logger.error(e);
}
}
try {
createRobot();
freeRobotNum = jedis.scard("free_robot");
if (freeRobotNum < maxPlayers)
{
logger.error("创建机器人后仍然不足,可用:" + freeRobotNum + ", 需要:" + maxPlayers);
return 0;
}
logger.info("成功创建机器人,当前可用:" + freeRobotNum);
}
catch (Exception e)
{
logger.error("创建机器人时发生异常: " + e.getMessage(), e);
return 0;
}
}
try {
while(freeRobotNum > 0)
{
String strRobotId = Redis.use("group1_db1").spop("free_robot");
freeRobotNum = Redis.use("group1_db1").scard("free_robot");
String strRobotId = jedis.spop("free_robot");
freeRobotNum = jedis.scard("free_robot");
if (StringUtil.isEmpty(strRobotId))
{
createRobot();
@ -775,7 +813,7 @@ public class UpdateRobotRoomJob implements Job{
}
}
Redis.use("group1_db1").sadd("used_robot", strRobotId);
jedis.sadd("used_robot", strRobotId);
int robotId = Integer.parseInt(strRobotId);
String fakeHp = Redis.use("group1_db10").hget("fake_"+strRobotId, "fake_hp");
if (StringUtil.isEmpty(fakeHp))
@ -790,6 +828,21 @@ public class UpdateRobotRoomJob implements Job{
{
logger.error(e);
}
finally {
//释放锁
try {
if (lockValue != null) {
String lockKey = "robot_allocation_lock_" + maxPlayers;
String currentValue = jedis.get(lockKey);
if (lockValue.equals(currentValue)) {
jedis.del(lockKey);
}
}
} catch (Exception e) {
logger.error("释放机器人分配锁时出错: " + e.getMessage());
}
jedis.close();
}
return 0;
}

View File

@ -1328,7 +1328,7 @@ public class GroupRoomService {
for (int i = 0; i < players.size(); i++) {
int player_id = players.getInt(i);
Redis.use("group1_db1").srem("used_robot", Integer.toString(player_id));
//Redis.use("group1_db1").sadd("free_robot", Integer.toString(player_id));
Redis.use("group1_db1").sadd("free_robot", Integer.toString(player_id));
}
}