🎈 perf(dispatch): 路由使用redis
This commit is contained in:
@@ -1,66 +0,0 @@
|
||||
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>>());
|
||||
roleRoute = pinus.app.get('roleRoute');
|
||||
}
|
||||
|
||||
//测试代码
|
||||
// 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];
|
||||
}
|
||||
@@ -1,20 +1,21 @@
|
||||
|
||||
import { dispatch } from './dispatcher';
|
||||
import { dispatch } from '../pubUtils/dispatcher';
|
||||
import { Session, Application } from 'pinus';
|
||||
import { redisClient } from '../services/redisService';
|
||||
|
||||
export function chat(session: Session, msg: any, app: Application, cb: (err: Error, serverId?: string) => void) {
|
||||
export async function chat(session: Session, msg: any, app: Application, cb: (err: Error, serverId?: string) => void) {
|
||||
let chatServers = app.getServersByType('chat');
|
||||
|
||||
if (!chatServers || chatServers.length === 0) {
|
||||
cb(new Error('can not find chat servers.'));
|
||||
return;
|
||||
}
|
||||
let id = session.get('chatServer')||dispatch(session.get('roleId'), chatServers, 'chat').id;
|
||||
let id = session.get('chatServer')||(await dispatch(redisClient(), session.get('roleId'), chatServers, 'chat')).id;
|
||||
|
||||
cb(null, id);
|
||||
}
|
||||
|
||||
export function battle(session: Session, msg: any, app: Application, cb: (err: Error, serverId?: string) => void) {
|
||||
export async function battle(session: Session, msg: any, app: Application, cb: (err: Error, serverId?: string) => void) {
|
||||
let battleServers = app.getServersByType('battle');
|
||||
|
||||
if (!battleServers || battleServers.length === 0) {
|
||||
@@ -42,16 +43,16 @@ export function battle(session: Session, msg: any, app: Application, cb: (err: E
|
||||
}
|
||||
|
||||
if(needDispatch) {
|
||||
let id = dispatch(rid, battleServers, 'battle').id;
|
||||
let id = (await dispatch(redisClient(), rid, battleServers, 'battle')).id;
|
||||
cb(null, id);
|
||||
} else {
|
||||
let id = session.get('battleServer')||dispatch(rid, battleServers, 'battle').id;
|
||||
let id = session.get('battleServer')||(await dispatch(redisClient(), rid, battleServers, 'battle')).id;
|
||||
cb(null, id);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
export function guild(session: Session, msg: any, app: Application, cb: (err: Error, serverId?: string) => void) {
|
||||
export async function guild(session: Session, msg: any, app: Application, cb: (err: Error, serverId?: string) => void) {
|
||||
let guildServers = app.getServersByType('guild');
|
||||
|
||||
if (!guildServers || guildServers.length === 0) {
|
||||
@@ -126,25 +127,25 @@ export function guild(session: Session, msg: any, app: Application, cb: (err: Er
|
||||
}
|
||||
|
||||
if(needDispatch) {
|
||||
let id = dispatch(rid, guildServers, 'guild').id;
|
||||
let id = (await dispatch(redisClient(), rid, guildServers, 'guild')).id;
|
||||
cb(null, id);
|
||||
} else {
|
||||
let id = session.get('guildServer')||dispatch(rid, guildServers, 'guild').id;
|
||||
let id = session.get('guildServer')||(await dispatch(redisClient(), rid, guildServers, 'guild')).id;
|
||||
cb(null, id);
|
||||
}
|
||||
}
|
||||
|
||||
export function activity(session: Session, msg: any, app: Application, cb: (err: Error, serverId?: string) => void) {
|
||||
export async function activity(session: Session, msg: any, app: Application, cb: (err: Error, serverId?: string) => void) {
|
||||
let activityServers = app.getServersByType('activity');
|
||||
if (!activityServers || activityServers.length === 0) {
|
||||
cb(new Error('can not find activity servers.'));
|
||||
return;
|
||||
}
|
||||
let id = session.get('activityServer')||dispatch(session.get('roleId'), activityServers, 'activity');
|
||||
let id = session.get('activityServer')||(await dispatch(redisClient(), session.get('roleId'), activityServers, 'activity'));
|
||||
cb(null, id);
|
||||
}
|
||||
|
||||
export function gm(session: Session, msg: any, app: Application, cb: (err: Error, serverId?: string) => void) {
|
||||
export async function gm(session: Session, msg: any, app: Application, cb: (err: Error, serverId?: string) => void) {
|
||||
let gmServers = app.getServersByType('gm');
|
||||
|
||||
if (!gmServers || gmServers.length === 0) {
|
||||
@@ -152,11 +153,11 @@ export function gm(session: Session, msg: any, app: Application, cb: (err: Error
|
||||
return;
|
||||
}
|
||||
|
||||
let res = dispatch(session.get('roleId'), gmServers, 'gm');
|
||||
let res = await dispatch(redisClient(), session.get('roleId'), gmServers, 'gm');
|
||||
cb(null, res.id);
|
||||
}
|
||||
|
||||
export function role(session: Session, msg: any, app: Application, cb: (err: Error, serverId?: string) => void) {
|
||||
export async function role(session: Session, msg: any, app: Application, cb: (err: Error, serverId?: string) => void) {
|
||||
let roleServers = app.getServersByType('role');
|
||||
|
||||
if (!roleServers || roleServers.length === 0) {
|
||||
@@ -164,11 +165,11 @@ export function role(session: Session, msg: any, app: Application, cb: (err: Err
|
||||
return;
|
||||
}
|
||||
|
||||
let id = session.get('roleServer')||dispatch(session.get('roleId'), roleServers).id;
|
||||
let id = session.get('roleServer')||(await dispatch(redisClient(), session.get('roleId'), roleServers)).id;
|
||||
cb(null, id);
|
||||
}
|
||||
|
||||
export function order(session: Session, msg: any, app: Application, cb: (err: Error, serverId?: string) => void) {
|
||||
export async function order(session: Session, msg: any, app: Application, cb: (err: Error, serverId?: string) => void) {
|
||||
let orderServers = app.getServersByType('order');
|
||||
|
||||
if (!orderServers || orderServers.length === 0) {
|
||||
@@ -176,6 +177,6 @@ export function order(session: Session, msg: any, app: Application, cb: (err: Er
|
||||
return;
|
||||
}
|
||||
|
||||
let id = session.get('orderServer')||dispatch(session.get('roleId'), orderServers, 'order').id;
|
||||
let id = session.get('orderServer')||(await dispatch(redisClient(), session.get('roleId'), orderServers, 'order')).id;
|
||||
cb(null, id);
|
||||
}
|
||||
Reference in New Issue
Block a user