🎈 perf(dispatch): 路由使用redis

This commit is contained in:
luying
2023-04-26 09:54:55 +08:00
parent 6907275ff2
commit e1d2b4b349
13 changed files with 67 additions and 154 deletions

View File

@@ -14,45 +14,19 @@ interface ServerInfo {
}
let roleRoute = new Map<string, Map<string, string>>()
export function dispatch(uid: string, connectors: ServerInfo[], serverType: string = '') {
//测试代码
// if (serverType == 'activity') {
// if (uid == 'SPDtGqx9Gw') {
// if (connectors.length > 1) {
// connectors = connectors.filter(obj => { return obj.id != 'activity-server-1' })
// }
// } else {
// connectors = connectors.filter(obj => { return obj.id == 'activity-server-1' })
// }
// }
export async function dispatch(redis: any, uid: string, connectors: ServerInfo[], serverType: string = '') {
connectors.sort((a, b) => a.id > b.id? 1: -1);
if (serverType) {
let roleRouteData = roleRoute.get(uid);
let serverId = '';
if (roleRouteData) {
serverId = roleRouteData.get(serverType);
if (serverId) {
let index = connectors.findIndex(obj => { return obj.id === serverId });
if (index != -1) {
// console.log('aaaaaaaaa 1', roleRouteData)
return connectors[index];
}
}
let appid: string = await redis.hgetAsync('roleRoute', `${uid}_${serverType}`);
if(!appid) {
let index = Math.abs(crc.crc32(uid)) % connectors.length;
let connector = connectors[index];
appid = connector.id;
redis.hsetAsync('roleRoute', `${uid}_${serverType}`, appid);
}
let index = Math.abs(crc.crc32(uid)) % connectors.length;
if (!roleRouteData) {
roleRouteData = new Map<string, string>()
}
roleRouteData.set(serverType, connectors[index].id);
roleRoute.set(uid, roleRouteData)
// console.log('aaaaaaaaa 2', roleRouteData)
return connectors[index];
let roleRoute = connectors.find(obj => obj.id == appid);
return roleRoute;
}
let index = Math.abs(crc.crc32(uid)) % connectors.length;
return connectors[index];