1,robot和channel的api请求要与客户端请求分开限频
This commit is contained in:
+13
-20
@@ -16,18 +16,18 @@ import cn.wildfirechat.common.ErrorCode;
|
||||
import java.util.concurrent.*;
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
|
||||
public class RPCCenter {
|
||||
private static final Logger LOG = LoggerFactory.getLogger(RPCCenter.class);
|
||||
public class ServerAPIHelper {
|
||||
private static final Logger LOG = LoggerFactory.getLogger(ServerAPIHelper.class);
|
||||
public static final String CHECK_USER_ONLINE_REQUEST = "check_user_online";
|
||||
public static final String KICKOFF_USER_REQUEST = "kickoff_user";
|
||||
|
||||
private Server server;
|
||||
public ConcurrentHashMap<Integer, RequestInfo> requestMap = new ConcurrentHashMap<>();
|
||||
private AtomicInteger aiRequestId = new AtomicInteger(1);
|
||||
public ScheduledExecutorService scheduledExecutorService = Executors.newScheduledThreadPool(2);
|
||||
private static Server server;
|
||||
public static ConcurrentHashMap<Integer, RequestInfo> requestMap = new ConcurrentHashMap<>();
|
||||
private static AtomicInteger aiRequestId = new AtomicInteger(1);
|
||||
public static ScheduledExecutorService scheduledExecutorService = Executors.newScheduledThreadPool(2);
|
||||
|
||||
public void init(Server server) {
|
||||
this.server = server;
|
||||
public static void init(Server s) {
|
||||
server = s;
|
||||
}
|
||||
|
||||
public interface Callback {
|
||||
@@ -37,18 +37,11 @@ public class RPCCenter {
|
||||
Executor getResponseExecutor();
|
||||
}
|
||||
|
||||
private static RPCCenter instance;
|
||||
|
||||
public static RPCCenter getInstance() {
|
||||
if (instance == null) {
|
||||
instance = new RPCCenter();
|
||||
}
|
||||
return instance;
|
||||
public static void sendRequest(String fromUser, String clientId, String request, byte[] message, String target, TargetEntry.Type type, Callback callback, boolean isAdmin) {
|
||||
sendRequest(fromUser, clientId, request, message, target, type, callback, isAdmin, false);
|
||||
}
|
||||
|
||||
protected RPCCenter() {}
|
||||
|
||||
public void sendRequest(String fromUser, String clientId, String request, byte[] message, String target, TargetEntry.Type type, Callback callback, boolean isAdmin) {
|
||||
public static void sendRequest(String fromUser, String clientId, String request, byte[] message, String target, TargetEntry.Type type, Callback callback, boolean isAdmin, boolean isRobotOrChannel) {
|
||||
int requestId = 0;
|
||||
|
||||
if (callback != null) {
|
||||
@@ -61,10 +54,10 @@ public class RPCCenter {
|
||||
requestMap.put(requestId, new RequestInfo(fromUser, clientId, callback, message, requestId, request));
|
||||
}
|
||||
|
||||
server.internalRpcMsg(fromUser, clientId, message, requestId, "", request, isAdmin);
|
||||
server.onApiMessage(fromUser, clientId, message, requestId, "", request, isAdmin, isRobotOrChannel);
|
||||
}
|
||||
|
||||
public void sendResponse(int errorCode, byte[] message, String toUuid, int requestId) {
|
||||
public static void sendResponse(int errorCode, byte[] message, String toUuid, int requestId) {
|
||||
LOG.debug("send async reponse to {} with requestId {}", toUuid, requestId);
|
||||
if (requestId > 0) {
|
||||
RequestInfo info = requestMap.remove(requestId);
|
||||
@@ -8,7 +8,7 @@
|
||||
|
||||
package io.moquette.persistence.remote;
|
||||
|
||||
import io.moquette.persistence.RPCCenter;
|
||||
import io.moquette.persistence.ServerAPIHelper;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
@@ -17,7 +17,7 @@ import java.util.concurrent.TimeUnit;
|
||||
|
||||
public class RequestInfo {
|
||||
private static final Logger LOG = LoggerFactory.getLogger(RequestInfo.class);
|
||||
public final RPCCenter.Callback callback;
|
||||
public final ServerAPIHelper.Callback callback;
|
||||
final byte[] message;
|
||||
final int requestId;
|
||||
final String fromUser;
|
||||
@@ -25,15 +25,15 @@ public class RequestInfo {
|
||||
final String clientId;
|
||||
public final ScheduledFuture future;
|
||||
|
||||
public RequestInfo(String fromUser, String clientId, RPCCenter.Callback callback, byte[] message, int requestId, String request) {
|
||||
public RequestInfo(String fromUser, String clientId, ServerAPIHelper.Callback callback, byte[] message, int requestId, String request) {
|
||||
this.callback = callback;
|
||||
this.message = message;
|
||||
this.requestId = requestId;
|
||||
this.fromUser = fromUser;
|
||||
this.clientId = clientId;
|
||||
this.request = request;
|
||||
this.future = RPCCenter.getInstance().scheduledExecutorService.schedule(() -> {
|
||||
RequestInfo info = RPCCenter.getInstance().requestMap.remove(requestId);
|
||||
this.future = ServerAPIHelper.scheduledExecutorService.schedule(() -> {
|
||||
RequestInfo info = ServerAPIHelper.requestMap.remove(requestId);
|
||||
if (info != null) {
|
||||
LOG.error("Request timeout. fromUser {}, cliendId {}, requestId {}, request {}", fromUser, clientId, requestId, request);
|
||||
info.callback.getResponseExecutor().execute(() -> {
|
||||
|
||||
Reference in New Issue
Block a user