注册分布式锁添加

master
lvxinran 2021-08-09 14:14:07 +08:00
parent 70f21cea83
commit f28de9e226
2 changed files with 28 additions and 17 deletions

View File

@ -149,25 +149,34 @@ public class GetUserController extends HttpServlet {
}
int uid = 0;
//加分布式锁
List<DBObject> userInfos = BaseGlobal.getInstance().mongoDBPool.find(_COLLECTION_NAME, dbObject);
if (userInfos.size() == 0) {
DBObject next = server_info.iterator().next();
Object register_state = next.get("register_state");
if(register_state!=null&& register_state.equals("0")){
returnErrorToFront(res,response,"该服务器已停止注册,请选择其他服务器游戏");
return;
try{
boolean getLock=false;
while(!getLock){
getLock = BaseGlobal.getInstance().redisApp.tryGetDistributedLock(RedisKey.REGISTER_LOCK, "", "get", 800);
if (getLock&&userInfos.size() == 0) {
DBObject next = server_info.iterator().next();
Object register_state = next.get("register_state");
if(register_state!=null&& register_state.equals("0")){
returnErrorToFront(res,response,"该服务器已停止注册,请选择其他服务器游戏");
return;
}
uid = BaseGlobal.getInstance().mongoDBPool.inc("uid") + 10000000;
dbObject.put("uid", uid);
dbObject.put("_id", uid);
userInfos.add(dbObject);
if (gid != null && !gid.isEmpty()) {
dbObject.put("gid", gid);
}
if (pid != null && !pid.isEmpty()) {
dbObject.put("pid", pid);
}
BaseGlobal.getInstance().mongoDBPool.save(_COLLECTION_NAME, dbObject);
}
}
uid = BaseGlobal.getInstance().mongoDBPool.inc("uid") + 10000000;
dbObject.put("uid", uid);
dbObject.put("_id", uid);
userInfos.add(dbObject);
if (gid != null && !gid.isEmpty()) {
dbObject.put("gid", gid);
}
if (pid != null && !pid.isEmpty()) {
dbObject.put("pid", pid);
}
BaseGlobal.getInstance().mongoDBPool.save(_COLLECTION_NAME, dbObject);
}finally {
BaseGlobal.getInstance().redisApp.releaseDistributedLock(RedisKey.REGISTER_LOCK,"","get");
}
Random random = new Random();
int utoken = Math.abs(random.nextInt());

View File

@ -19,4 +19,6 @@ public interface RedisKey {
* openId
*/
String Ban_Open_Id = "Ban_Open_Id";
String REGISTER_LOCK = "REGISTER_LOCK";
}