Files
ZYZ/shared/pubUtils/dispatcher.ts
2023-04-26 09:54:55 +08:00

33 lines
974 B
TypeScript

import * as crc from 'crc';
interface ServerInfo {
id: string;
serverType: string;
host: string;
port: number;
clientHost?: string;
clientPort?: number;
frontend ?: boolean;
args ?: string | string[];
cpu ?: number;
}
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 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 roleRoute = connectors.find(obj => obj.id == appid);
return roleRoute;
}
let index = Math.abs(crc.crc32(uid)) % connectors.length;
return connectors[index];
}