func NewKiteClientManager(zkAddr, groupId, secretKey string, listen listener.IListener) *KiteClientManager { flowstat := stat.NewFlowStat("kiteclient-" + groupId) rc := turbo.NewRemotingConfig( "remoting-"+groupId, 50, 16*1024, 16*1024, 10000, 10000, 10*time.Second, 160000) //重连管理器 reconnManager := c.NewReconnectManager(true, 30*time.Second, 100, handshake) //构造pipeline的结构 pipeline := pipe.NewDefaultPipeline() clientm := c.NewClientManager(reconnManager) pipeline.RegisteHandler("kiteclient-packet", chandler.NewPacketHandler("kiteclient-packet")) pipeline.RegisteHandler("kiteclient-heartbeat", chandler.NewHeartbeatHandler("kiteclient-heartbeat", 10*time.Second, 5*time.Second, clientm)) pipeline.RegisteHandler("kiteclient-accept", chandler.NewAcceptHandler("kiteclient-accept", listen)) pipeline.RegisteHandler("kiteclient-remoting", pipe.NewRemotingHandler("kiteclient-remoting", clientm)) manager := &KiteClientManager{ ga: c.NewGroupAuth(groupId, secretKey), kiteClients: make(map[string][]*kiteClient, 10), topics: make([]string, 0, 10), pipeline: pipeline, clientManager: clientm, rc: rc, flowstat: flowstat, zkAddr: zkAddr} //开启流量统计 manager.remointflow() manager.flowstat.Start() return manager }
func NewKiteQServer(kc KiteQConfig) *KiteQServer { kiteqName, _ := os.Hostname() kitedb := parseDB(kc, kiteqName) kitedb.Start() //重连管理器 reconnManager := client.NewReconnectManager(false, -1, -1, handshake) //客户端连接管理器 clientManager := client.NewClientManager(reconnManager) // 临时在这里创建的BindExchanger exchanger := binding.NewBindExchanger(kc.zkhost, kc.server) //创建消息投递注册器 registry := stat.NewDeliveryRegistry(10 * 10000) //重投策略 rw := make([]handler.RedeliveryWindow, 0, 10) rw = append(rw, handler.NewRedeliveryWindow(0, 3, 30)) rw = append(rw, handler.NewRedeliveryWindow(4, 10, 2*60)) rw = append(rw, handler.NewRedeliveryWindow(10, 20, 4*60)) rw = append(rw, handler.NewRedeliveryWindow(20, 30, 8*60)) rw = append(rw, handler.NewRedeliveryWindow(30, 40, 16*60)) rw = append(rw, handler.NewRedeliveryWindow(40, 50, 32*60)) rw = append(rw, handler.NewRedeliveryWindow(50, -1, 60*60)) //初始化pipeline pipeline := pipe.NewDefaultPipeline() pipeline.RegisteHandler("packet", handler.NewPacketHandler("packet")) pipeline.RegisteHandler("access", handler.NewAccessHandler("access", clientManager)) pipeline.RegisteHandler("validate", handler.NewValidateHandler("validate", clientManager)) pipeline.RegisteHandler("accept", handler.NewAcceptHandler("accept")) pipeline.RegisteHandler("heartbeat", handler.NewHeartbeatHandler("heartbeat")) pipeline.RegisteHandler("check_message", handler.NewCheckMessageHandler("check_message", kc.topics)) pipeline.RegisteHandler("persistent", handler.NewPersistentHandler("persistent", kc.deliverTimeout, kitedb, kc.fly, kc.flowstat)) pipeline.RegisteHandler("txAck", handler.NewTxAckHandler("txAck", kitedb)) pipeline.RegisteHandler("deliverpre", handler.NewDeliverPreHandler("deliverpre", kitedb, exchanger, kc.flowstat, kc.maxDeliverWorkers)) pipeline.RegisteHandler("deliver", handler.NewDeliverHandler("deliver", registry)) pipeline.RegisteHandler("remoting", pipe.NewRemotingHandler("remoting", clientManager)) pipeline.RegisteHandler("remote-future", handler.NewRemotingFutureHandler("remote-future")) pipeline.RegisteHandler("deliverResult", handler.NewDeliverResultHandler("deliverResult", kc.deliverTimeout, kitedb, rw, registry)) //以下是处理投递结果返回事件,即到了remoting端会backwark到future-->result-->record recoverManager := NewRecoverManager(kiteqName, kc.recoverPeriod, pipeline, kitedb) return &KiteQServer{ reconnManager: reconnManager, clientManager: clientManager, exchanger: exchanger, pipeline: pipeline, recoverManager: recoverManager, kc: kc, kitedb: kitedb, stop: false} }