redis-proxy的三个库文件,稍微做了点注释

// **************************************************** lib/connection_pool.js
// 连接池的管理。
连接池由两个小连接池组成:freePool和inUsePool . freePool里面装的是空闲的连接(这些连接有可能是新创建的连接
,也有可能是以前创建的用完释放后的连接), inUsePool里面装的是正在使用的的连接。ConnectionPool提供一个
function take(id) 这样的函数。
当每次需要更具一个id来从连接池中取出连接时。先直接看inUsePool里面有没有该id对应的连接,有的话就返回这个
连接。没有的话,在freePool里面考虑,当freePool里面不为空池时,就取freePool中的第一个连接作为id对应的连接。
若freePool为空时,我们就每次创建一组数量为groupBy的连接放到连接池里面(这样做可以优化资源的利用,假设每次取一个连接,
用完了就释放,下次再用在创建,这实际浪费了释放掉的那些连接。程序中是用完掉一个连接后,释放到freePool中,暂时保存起来,
下次需要创建一个连接时,就直接从freePool中取出来,填上连接信息就可以直接用了),然后取第一个连接作为id对应的连接。

下面以此解释一下ConnectionPool.js中各个函数的大致意思

1.ConnectionPool = moudle.exports = function(o){
  ....
}

初始化下列成员:

* options   :
              最大连接数maxSize 
              关闭函数close() ,
              第一次创建的连接数startSize=max( maxSize/3 , maxSize) ,
              第一次以后创建的连接数growBy=max(maxSize/3 , maxSize) .
 
* freepool  :
              空闲的连接对象集合
* inUsePool :
              正在使用的连接对象集合
* delayCreation :
              是否推迟创建连接池。

2.ConnctionPool.prototype.addToFreePool
创建 howManyConnection个连接,把这些连接放到freepool池中。

3.addConnectionsToPool 作为addToFreePool的一个接口。

4.take函数第一段讲了。

5.close(id) 把连接从inUsePool中搬到freepool中。

6.release(id) -- 调用close(id) 释放掉id对应的连接。

7.closeAll()  -- 调用close(id)关闭所有连接.

8. totalConnections = freepool.length + inUsePool.size

9. totalFreeConnections = freepool.length

10.totalInUseConnections = inUsePool.size ;

 

//**********************************************************************************************************************

//  lib/server.js

1.Server=module.exports=function(serverInfo){}
* status : 服务器的状态
* errorCount : default = 0
* options :
      a .  serverInfo  由host和port组成
      b . softErrorCount : defalut = 5
      c .  client
* client : 通过该客户端来管理Server .该client通过setupControlClient函数
注册了ready,error,end三个事件。

2.Server.prototype.setupControlClient() 为server的控制客服端注册响应事件。

3._attachHandlers -- 注册事件

4.Server.prototype.up()  -- 将服务状态设为up , 并且发出up动作。

5.Server.setMaster()   -- 将该服务器设为master , 为该服务器创建连接池用来管理
连接该服务器的连接。并调用this.slavenone()函数表明该服务器不是任何服务器的从服务
器。

6.Server.prototype.sendCommand(command ,id) :从连接池中选择一个叫id的连接,
然后给该连接发命令。set , get ..等等redis的操作命令。

7.Server.prototype.close() -- 关闭连接池中名为id的连接。

8.Server.prototype.slave=function(that_server){ }
通过客户端将this.server设为that_server的从服务器。

9.Server.prototype.slavenone = function(){}

10.Server.prototype.incrErrorCount = function(){} errorCount ++;

11.Server.prototype.ping() -- this.client.ping()

12.Server.prototype.isUp() -- return this.status ==='up'

13.Server.prototype.down() -- this.status = 'down'  ; this.emit('down')

 

//*****************************************************************************************************************

// lib/redis-proxy.js

1.RedisProxy = module.exports = function(o){}
对象o传过来的是config/config.json配置文件对象。
a . debug_mode
b . _active
c . options : listen_port 6379 ; softErrorCount 5 ; pool_size 10 ;
d . servers []  :
 servers[0] -- master
 servers[1] -- slave
 所有服务器都注册 up 和 down 监听事件
 当第一次选择master时,直接把第一个定为master , 并将其他定位该服务器的从。
 当一台服务器关闭时,通过调用nextActive选择下一台处于up状态的服务器作为master.

2.RedisProxy.prototype.readyup = function(active)
将active服务器设为master ,并把其他设为active的从。

3.RedisProxy.prototype.nextActive = function(){}
依次遍历服务器,找到第一台处于up状态的服务器,并调用readyup().

4.Object.defineProperty(RedisProxy.prototype , 'active'){
    get :function(){ return this._active}
}

5.RedisProxy.prototype.sendCommand(command ,id ,callback){
    this.active.sendCommand(command ,id ,callback);
}

6.RedisProxy.prototype.quit=function(id){
    return this.active.close(id);
}
 
7.RedisProxy.prototype.watch = function(){}
通过每隔check_period毫秒ping每台服务,来达到监控服务器目的。

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值