Live Query basic monitoring (#4168)

* Adds uuid based client identification to prevent overflows

* no-super

* Adds cloud code monitoring

* fixes test

* nit
This commit is contained in:
Florent Vilmart
2017-09-15 17:20:51 -04:00
committed by GitHub
parent 7ecb36e71e
commit d0184f438d
5 changed files with 90 additions and 16 deletions

View File

@@ -9,10 +9,11 @@ import { matchesQuery, queryHash } from './QueryTools';
import { ParsePubSub } from './ParsePubSub';
import { SessionTokenCache } from './SessionTokenCache';
import _ from 'lodash';
import uuid from 'uuid';
import { runLiveQueryEventHandlers } from '../triggers';
class ParseLiveQueryServer {
clientId: number;
clients: Object;
clients: Map;
// className -> (queryHash -> subscription)
subscriptions: Object;
parseWebSocketServer: Object;
@@ -21,7 +22,6 @@ class ParseLiveQueryServer {
subscriber: Object;
constructor(server: any, config: any) {
this.clientId = 0;
this.clients = new Map();
this.subscriptions = new Map();
@@ -269,10 +269,16 @@ class ParseLiveQueryServer {
});
parseWebsocket.on('disconnect', () => {
logger.info('Client disconnect: %d', parseWebsocket.clientId);
logger.info(`Client disconnect: ${parseWebsocket.clientId}`);
const clientId = parseWebsocket.clientId;
if (!this.clients.has(clientId)) {
logger.error('Can not find client %d on disconnect', clientId);
runLiveQueryEventHandlers({
event: 'ws_disconnect_error',
clients: this.clients.size,
subscriptions: this.subscriptions.size,
error: `Unable to find client ${clientId}`
});
logger.error(`Can not find client ${clientId} on disconnect`);
return;
}
@@ -298,6 +304,17 @@ class ParseLiveQueryServer {
logger.verbose('Current clients %d', this.clients.size);
logger.verbose('Current subscriptions %d', this.subscriptions.size);
runLiveQueryEventHandlers({
event: 'ws_disconnect',
clients: this.clients.size,
subscriptions: this.subscriptions.size
});
});
runLiveQueryEventHandlers({
event: 'ws_connect',
clients: this.clients.size,
subscriptions: this.subscriptions.size
});
}
@@ -404,12 +421,17 @@ class ParseLiveQueryServer {
return;
}
const hasMasterKey = this._hasMasterKey(request, this.keyPairs);
const client = new Client(this.clientId, parseWebsocket, hasMasterKey);
parseWebsocket.clientId = this.clientId;
this.clientId += 1;
const clientId = uuid();
const client = new Client(clientId, parseWebsocket, hasMasterKey);
parseWebsocket.clientId = clientId;
this.clients.set(parseWebsocket.clientId, client);
logger.info('Create new client: %d', parseWebsocket.clientId);
logger.info(`Create new client: ${parseWebsocket.clientId}`);
client.pushConnect();
runLiveQueryEventHandlers({
event: 'connect',
clients: this.clients.size,
subscriptions: this.subscriptions.size
});
}
_hasMasterKey(request: any, validKeyPairs: any): boolean {
@@ -481,8 +503,13 @@ class ParseLiveQueryServer {
client.pushSubscribe(request.requestId);
logger.verbose('Create client %d new subscription: %d', parseWebsocket.clientId, request.requestId);
logger.verbose(`Create client ${parseWebsocket.clientId} new subscription: ${request.requestId}`);
logger.verbose('Current client number: %d', this.clients.size);
runLiveQueryEventHandlers({
event: 'subscribe',
clients: this.clients.size,
subscriptions: this.subscriptions.size
});
}
_handleUpdateSubscription(parseWebsocket: any, request: any): any {
@@ -529,6 +556,11 @@ class ParseLiveQueryServer {
if (classSubscriptions.size === 0) {
this.subscriptions.delete(className);
}
runLiveQueryEventHandlers({
event: 'unsubscribe',
clients: this.clients.size,
subscriptions: this.subscriptions.size
});
if (!notifyClient) {
return;
@@ -536,7 +568,7 @@ class ParseLiveQueryServer {
client.pushUnsubscribe(request.requestId);
logger.verbose('Delete client: %d | subscription: %d', parseWebsocket.clientId, request.requestId);
logger.verbose(`Delete client: ${parseWebsocket.clientId} | subscription: ${request.requestId}`);
}
}