|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452 |
- 'use strict';
-
- const TopologyBase = require('./topology_base').TopologyBase;
- const MongoError = require('mongodb-core').MongoError;
- const CMongos = require('mongodb-core').Mongos;
- const Cursor = require('../cursor');
- const Server = require('./server');
- const Store = require('./topology_base').Store;
- const MAX_JS_INT = require('../utils').MAX_JS_INT;
- const translateOptions = require('../utils').translateOptions;
- const filterOptions = require('../utils').filterOptions;
- const mergeOptions = require('../utils').mergeOptions;
-
-
-
-
- var legalOptionNames = [
- 'ha',
- 'haInterval',
- 'acceptableLatencyMS',
- 'poolSize',
- 'ssl',
- 'checkServerIdentity',
- 'sslValidate',
- 'sslCA',
- 'sslCRL',
- 'sslCert',
- 'ciphers',
- 'ecdhCurve',
- 'sslKey',
- 'sslPass',
- 'socketOptions',
- 'bufferMaxEntries',
- 'store',
- 'auto_reconnect',
- 'autoReconnect',
- 'emitError',
- 'keepAlive',
- 'keepAliveInitialDelay',
- 'noDelay',
- 'connectTimeoutMS',
- 'socketTimeoutMS',
- 'loggerLevel',
- 'logger',
- 'reconnectTries',
- 'appname',
- 'domainsEnabled',
- 'servername',
- 'promoteLongs',
- 'promoteValues',
- 'promoteBuffers',
- 'promiseLibrary',
- 'monitorCommands'
- ];
-
-
- class Mongos extends TopologyBase {
- constructor(servers, options) {
- super();
-
- options = options || {};
- var self = this;
-
-
- options = filterOptions(options, legalOptionNames);
-
-
- for (var i = 0; i < servers.length; i++) {
- if (!(servers[i] instanceof Server)) {
- throw MongoError.create({
- message: 'all seed list instances must be of the Server type',
- driver: true
- });
- }
- }
-
-
- var storeOptions = {
- force: false,
- bufferMaxEntries:
- typeof options.bufferMaxEntries === 'number' ? options.bufferMaxEntries : MAX_JS_INT
- };
-
-
- var store = options.store || new Store(self, storeOptions);
-
-
- var seedlist = servers.map(function(x) {
- return { host: x.host, port: x.port };
- });
-
-
- var reconnect = typeof options.auto_reconnect === 'boolean' ? options.auto_reconnect : true;
- reconnect = typeof options.autoReconnect === 'boolean' ? options.autoReconnect : reconnect;
-
-
- var clonedOptions = mergeOptions(
- {},
- {
- disconnectHandler: store,
- cursorFactory: Cursor,
- reconnect: reconnect,
- emitError: typeof options.emitError === 'boolean' ? options.emitError : true,
- size: typeof options.poolSize === 'number' ? options.poolSize : 5,
- monitorCommands:
- typeof options.monitorCommands === 'boolean' ? options.monitorCommands : false
- }
- );
-
-
- clonedOptions = translateOptions(clonedOptions, options);
-
-
- var socketOptions =
- options.socketOptions && Object.keys(options.socketOptions).length > 0
- ? options.socketOptions
- : options;
-
-
- clonedOptions = translateOptions(clonedOptions, socketOptions);
-
-
- clonedOptions.clientInfo = this.clientInfo;
-
- if (options.appname) {
- clonedOptions.clientInfo.application = { name: options.appname };
- }
-
-
- this.s = {
-
- coreTopology: new CMongos(seedlist, clonedOptions),
-
- sCapabilities: null,
-
- debug: clonedOptions.debug,
-
- storeOptions: storeOptions,
-
- clonedOptions: clonedOptions,
-
- store: store,
-
- options: options,
-
- sessionPool: null,
-
- sessions: [],
-
- promiseLibrary: options.promiseLibrary || Promise
- };
- }
-
-
- connect(_options, callback) {
- var self = this;
- if ('function' === typeof _options) (callback = _options), (_options = {});
- if (_options == null) _options = {};
- if (!('function' === typeof callback)) callback = null;
- _options = Object.assign({}, this.s.clonedOptions, _options);
- self.s.options = _options;
-
-
- self.s.storeOptions.bufferMaxEntries =
- typeof _options.bufferMaxEntries === 'number' ? _options.bufferMaxEntries : -1;
-
-
- var connectErrorHandler = function() {
- return function(err) {
-
- var events = ['timeout', 'error', 'close'];
- events.forEach(function(e) {
- self.removeListener(e, connectErrorHandler);
- });
-
- self.s.coreTopology.removeListener('connect', connectErrorHandler);
-
- self.close(true);
-
-
- try {
- callback(err);
- } catch (err) {
- process.nextTick(function() {
- throw err;
- });
- }
- };
- };
-
-
- var errorHandler = function(event) {
- return function(err) {
- if (event !== 'error') {
- self.emit(event, err);
- }
- };
- };
-
-
- var reconnectHandler = function() {
- self.emit('reconnect');
- self.s.store.execute();
- };
-
-
- var relay = function(event) {
- return function(t, server) {
- self.emit(event, t, server);
- };
- };
-
-
- var connectHandler = function() {
-
- var events = ['timeout', 'error', 'close', 'fullsetup'];
- events.forEach(function(e) {
- self.s.coreTopology.removeAllListeners(e);
- });
-
-
- self.s.coreTopology.on('timeout', errorHandler('timeout'));
- self.s.coreTopology.on('error', errorHandler('error'));
- self.s.coreTopology.on('close', errorHandler('close'));
-
-
- self.s.coreTopology.on('fullsetup', function() {
- self.emit('fullsetup', self);
- });
-
-
- self.emit('open', null, self);
-
-
- try {
- callback(null, self);
- } catch (err) {
- process.nextTick(function() {
- throw err;
- });
- }
- };
-
-
- var events = [
- 'timeout',
- 'error',
- 'close',
- 'serverOpening',
- 'serverDescriptionChanged',
- 'serverHeartbeatStarted',
- 'serverHeartbeatSucceeded',
- 'serverHeartbeatFailed',
- 'serverClosed',
- 'topologyOpening',
- 'topologyClosed',
- 'topologyDescriptionChanged',
- 'commandStarted',
- 'commandSucceeded',
- 'commandFailed'
- ];
- events.forEach(function(e) {
- self.s.coreTopology.removeAllListeners(e);
- });
-
-
- self.s.coreTopology.on('serverDescriptionChanged', relay('serverDescriptionChanged'));
- self.s.coreTopology.on('serverHeartbeatStarted', relay('serverHeartbeatStarted'));
- self.s.coreTopology.on('serverHeartbeatSucceeded', relay('serverHeartbeatSucceeded'));
- self.s.coreTopology.on('serverHeartbeatFailed', relay('serverHeartbeatFailed'));
- self.s.coreTopology.on('serverOpening', relay('serverOpening'));
- self.s.coreTopology.on('serverClosed', relay('serverClosed'));
- self.s.coreTopology.on('topologyOpening', relay('topologyOpening'));
- self.s.coreTopology.on('topologyClosed', relay('topologyClosed'));
- self.s.coreTopology.on('topologyDescriptionChanged', relay('topologyDescriptionChanged'));
- self.s.coreTopology.on('commandStarted', relay('commandStarted'));
- self.s.coreTopology.on('commandSucceeded', relay('commandSucceeded'));
- self.s.coreTopology.on('commandFailed', relay('commandFailed'));
-
-
- self.s.coreTopology.once('timeout', connectErrorHandler('timeout'));
- self.s.coreTopology.once('error', connectErrorHandler('error'));
- self.s.coreTopology.once('close', connectErrorHandler('close'));
- self.s.coreTopology.once('connect', connectHandler);
-
- self.s.coreTopology.on('joined', relay('joined'));
- self.s.coreTopology.on('left', relay('left'));
-
-
- self.s.coreTopology.on('reconnect', reconnectHandler);
-
-
- self.s.coreTopology.connect(_options);
- }
- }
-
- Object.defineProperty(Mongos.prototype, 'haInterval', {
- enumerable: true,
- get: function() {
- return this.s.coreTopology.s.haInterval;
- }
- });
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- module.exports = Mongos;
|