src/sequelize/lib/dialects/abstract/connection-manager.js
/**
   * Initialize connection pool. By default pool autostart is set to false, so no connection will be
   * be created unless `pool.acquire` is called.
   */
Initialize connection pool. By default pool autostart is set to false, so no connection will be
be created unless `pool.acquire` is called.
Initialize connection pool . By default pool autostart is set to false so no connection will be be created unless ` pool . acquire ` is called .
initPools() {
    const config = this.config;
    if (!config.replication) {
      this.pool = new Pool({
        name: 'sequelize',
        create: () => this._connect(config),  
        destroy: async connection => {
          const result = await this._disconnect(connection); 
          debug('connection destroy');
          return result;
        },
        validate: config.pool.validate,
        max: config.pool.max,
        min: config.pool.min,
        acquireTimeoutMillis: config.pool.acquire,
        idleTimeoutMillis: config.pool.idle,
        reapIntervalMillis: config.pool.evict,
        maxUses: config.pool.maxUses
      });
      debug(`pool created with max/min: ${config.pool.max}/${config.pool.min}, no replication`);
      return;
    }
    if (!Array.isArray(config.replication.read)) {
      config.replication.read = [config.replication.read];
    }
    config.replication.write = _.defaults(config.replication.write, _.omit(config, 'replication'));
    config.replication.read = config.replication.read.map(readConfig =>
      _.defaults(readConfig, _.omit(this.config, 'replication'))
    );
    let reads = 0;
    this.pool = {
      release: client => {
        if (client.queryType === 'read') {
          this.pool.read.release(client);
        } else {
          this.pool.write.release(client);
        }
      },
      acquire: (queryType, useMaster) => {
        useMaster = useMaster === undefined ? false : useMaster;
        if (queryType === 'SELECT' && !useMaster) {
          return this.pool.read.acquire();
        }
        return this.pool.write.acquire();
      },
      destroy: connection => {
        this.pool[connection.queryType].destroy(connection);
        debug('connection destroy');
      },
      destroyAllNow: async () => {
        await Promise.all([
          this.pool.read.destroyAllNow(),
          this.pool.write.destroyAllNow()
        ]);
        debug('all connections destroyed');
      },
      drain: async () => Promise.all([
        this.pool.write.drain(),
        this.pool.read.drain()
      ]),
      read: new Pool({
        name: 'sequelize:read',
        create: async () => {
          const nextRead = reads++ % config.replication.read.length;
          const connection = await this._connect(config.replication.read[nextRead]);
          connection.queryType = 'read';
          return connection;
        },
        destroy: connection => this._disconnect(connection),
        validate: config.pool.validate,
        max: config.pool.max,
        min: config.pool.min,
        acquireTimeoutMillis: config.pool.acquire,
        idleTimeoutMillis: config.pool.idle,
        reapIntervalMillis: config.pool.evict,
        maxUses: config.pool.maxUses
      }),
      write: new Pool({
        name: 'sequelize:write',
        create: async () => {
          const connection = await this._connect(config.replication.write);
          connection.queryType = 'write';
          return connection;
        },
        destroy: connection => this._disconnect(connection),
        validate: config.pool.validate,
        max: config.pool.max,
        min: config.pool.min,
        acquireTimeoutMillis: config.pool.acquire,
        idleTimeoutMillis: config.pool.idle,
        reapIntervalMillis: config.pool.evict,
        maxUses: config.pool.maxUses
      })
    };
    debug(`pool created with max/min: ${config.pool.max}/${config.pool.min}, with replication`);
  }
initPools ( ) { const config = this . config ; if ( ! config . replication ) { this . pool = new Pool ( { name : 'sequelize' , create : ( ) => this . _connect ( config ) , destroy : async connection => { const result = await this . _disconnect ( connection ) ; debug ( 'connection destroy' ) ; return result ; } , validate : config . pool . validate , max : config . pool . max , min : config . pool . min , acquireTimeoutMillis : config . pool . acquire , idleTimeoutMillis : config . pool . idle , reapIntervalMillis : config . pool . evict , maxUses : config . pool . maxUses } ) ; debug ( ` ${ config . pool . max } ${ config . pool . min } ` ) ; return ; } if ( ! Array . isArray ( config . replication . read ) ) { config . replication . read = [ config . replication . read ] ; } config . replication . write = _ . defaults ( config . replication . write , _ . omit ( config , 'replication' ) ) ; config . replication . read = config . replication . read . map ( readConfig => _ . defaults ( readConfig , _ . omit ( this . config , 'replication' ) ) ) ; let reads = 0 ; this . pool = { release : client => { if ( client . queryType === 'read' ) { this . pool . read . release ( client ) ; } else { this . pool . write . release ( client ) ; } } , acquire : ( queryType , useMaster ) => { useMaster = useMaster === undefined ? false : useMaster ; if ( queryType === 'SELECT' && ! useMaster ) { return this . pool . read . acquire ( ) ; } return this . pool . write . acquire ( ) ; } , destroy : connection => { this . pool [ connection . queryType ] . destroy ( connection ) ; debug ( 'connection destroy' ) ; } , destroyAllNow : async ( ) => { await Promise . all ( [ this . pool . read . destroyAllNow ( ) , this . pool . write . destroyAllNow ( ) ] ) ; debug ( 'all connections destroyed' ) ; } , drain : async ( ) => Promise . all ( [ this . pool . write . drain ( ) , this . pool . read . drain ( ) ] ) , read : new Pool ( { name : 'sequelize:read' , create : async ( ) => { const nextRead = reads ++ % config . replication . read . length ; const connection = await this . _connect ( config . replication . read [ nextRead ] ) ; connection . queryType = 'read' ; return connection ; } , destroy : connection => this . _disconnect ( connection ) , validate : config . pool . validate , max : config . pool . max , min : config . pool . min , acquireTimeoutMillis : config . pool . acquire , idleTimeoutMillis : config . pool . idle , reapIntervalMillis : config . pool . evict , maxUses : config . pool . maxUses } ) , write : new Pool ( { name : 'sequelize:write' , create : async ( ) => { const connection = await this . _connect ( config . replication . write ) ; connection . queryType = 'write' ; return connection ; } , destroy : connection => this . _disconnect ( connection ) , validate : config . pool . validate , max : config . pool . max , min : config . pool . min , acquireTimeoutMillis : config . pool . acquire , idleTimeoutMillis : config . pool . idle , reapIntervalMillis : config . pool . evict , maxUses : config . pool . maxUses } ) } ; debug ( ` ${ config . pool . max } ${ config . pool . min } ` ) ; }
Initialize connection pool.
Initialize connection pool .
// Pool类只是一个管理连接的工具，真正创建连接和销毁连接还是定义在ConnectionManager类上 // 定义创建连接的方法 // 定义销毁连接的方法 /**
     * 下面的代码是用来处理读写分离的情况
     */ // Map main connection config // Apply defaults to each read config // custom pooling for replication (original author @janmeier) // round robin config