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} }
func init() { rc := turbo.NewRemotingConfig( "turbo-server:localhost:28888", 1000, 16*1024, 16*1024, 10000, 10000, 10*time.Second, 160000) remoteServer = NewRemotionServer("localhost:28888", rc, packetDispatcher) remoteServer.ListenAndServer() conn, _ := dial("localhost:28888") // //重连管理器 reconnManager := client.NewReconnectManager(false, -1, -1, handshake) clientManager = client.NewClientManager(reconnManager) rcc := turbo.NewRemotingConfig( "turbo-client:localhost:28888", 1000, 16*1024, 16*1024, 10000, 10000, 10*time.Second, 160000) remoteClient := client.NewRemotingClient(conn, clientPacketDispatcher, rcc) remoteClient.Start() auth := &client.GroupAuth{} auth.GroupId = "a" auth.SecretKey = "123" clientManager.Auth(auth, remoteClient) go func() { for { log.Println(clientf.Monitor()) time.Sleep(1 * time.Second) } }() }
func main() { go func() { http.ListenAndServe(":13801", nil) }() // 重连管理器 reconnManager := client.NewReconnectManager(false, -1, -1, handshake) clientManager := client.NewClientManager(reconnManager) rcc := turbo.NewRemotingConfig( "turbo-client:localhost:28888", 1000, 16*1024, 16*1024, 20000, 20000, 10*time.Second, 160000) go func() { for { log.Println(rcc.FlowStat.Stat()) time.Sleep(1 * time.Second) } }() //创建物理连接 conn, _ := func(hostport string) (*net.TCPConn, error) { //连接 remoteAddr, err_r := net.ResolveTCPAddr("tcp4", hostport) if nil != err_r { log.Printf("KiteClientManager|RECONNECT|RESOLVE ADDR |FAIL|remote:%s\n", err_r) return nil, err_r } conn, err := net.DialTCP("tcp4", nil, remoteAddr) if nil != err { log.Printf("KiteClientManager|RECONNECT|%s|FAIL|%s\n", hostport, err) return nil, err } return conn, nil }("localhost:28888") remoteClient := client.NewRemotingClient(conn, clientPacketDispatcher, rcc) remoteClient.Start() auth := &client.GroupAuth{} auth.GroupId = "a" auth.SecretKey = "123" clientManager.Auth(auth, remoteClient) //echo command p := packet.NewPacket(1, []byte("echo")) //find a client tmp := clientManager.FindRemoteClients([]string{"a"}, func(groupid string, c *client.RemotingClient) bool { return false }) for i := 0; i < 1; i++ { go func() { for { //write command and wait for response _, err := tmp["a"][0].WriteAndGet(*p, 100*time.Millisecond) if nil != err { log.Printf("WAIT RESPONSE FAIL|%s\n", err) break } else { // log.Printf("WAIT RESPONSE SUCC|%s\n", string(resp.([]byte))) } } }() } select {} }