🎈 perf(内存): roleRoute挂在app上

This commit is contained in:
luying
2023-04-21 20:48:09 +08:00
parent d8bb70753b
commit 5182337667
12 changed files with 75 additions and 11 deletions

View File

@@ -0,0 +1,65 @@
import * as crc from 'crc';
import { pinus } from 'pinus';
interface ServerInfo {
id: string;
serverType: string;
host: string;
port: number;
clientHost?: string;
clientPort?: number;
frontend ?: boolean;
args ?: string | string[];
cpu ?: number;
}
export function dispatch(uid: string, connectors: ServerInfo[], serverType: string = '') {
let roleRoute = pinus.app.get('roleRoute');
if(!roleRoute) {
pinus.app.set('roleRoute', new Map<string, Map<string, 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' })
// }
// }
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 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];
}
pinus.app.set('roleRoute', roleRoute);
let index = Math.abs(crc.crc32(uid)) % connectors.length;
return connectors[index];
}

View File

@@ -1,5 +1,5 @@
import { dispatch } from '../pubUtils/dispatcher';
import { dispatch } from './dispatcher';
import { Session, Application } from 'pinus';
export function chat(session: Session, msg: any, app: Application, cb: (err: Error, serverId?: string) => void) {