func (self *MockListener) OnMessageCheck(tx *protocol.TxResponse) error { log.Debug("MockListener|OnMessageCheck|%s\n", tx.MessageId) v, _ := tx.GetProperty("tradeno") log.Debug("MockListener|OnMessageCheck|PROP|%s\n", v) tx.Commit() return nil }
func (self *MessageStore) recoverSnapshot() { //current segmentid if len(self.segments) > 0 { //replay log for i, s := range self.segments { err := s.Open(self.replay) if nil != err { log.Error("MessageStore|recoverSnapshot|Fail|%s", err, s.slog.path) panic(err) } //last segments if i == len(self.segments)-1 { if nil != err { panic("MessageStore|Load Last Segment|FAIL|" + err.Error()) } //set snapshost status if len(s.chunks) > 0 { self.chunkId = s.chunks[len(s.chunks)-1].id } } log.Debug("MessageStore|recoverSnapshot|%s", s.name) } } }
func (self *Session) Close() error { if !self.isClose { self.isClose = true //flush self.bw.Flush() self.conn.Close() close(self.WriteChannel) close(self.ReadChannel) log.Debug("Session|Close|%s...", self.remoteAddr) } return nil }
//delete chunk func (self *Segment) Delete(cid int64) bool { idx := sort.Search(len(self.chunks), func(i int) bool { return self.chunks[i].id >= cid }) if idx < len(self.chunks) { //mark delete s := self.chunks[idx] if s.flag != DELETE && s.flag != EXPIRED { s.flag = DELETE return true } } else { log.Debug("Segment|Delete|NO Chunk|chunkid:%d|%d", cid, idx, len(self.chunks)) } return true }
//启动当前的client func (self *RemotingClient) Start() { //重新初始化 laddr := self.conn.LocalAddr().(*net.TCPAddr) raddr := self.conn.RemoteAddr().(*net.TCPAddr) self.localAddr = fmt.Sprintf("%s:%d", laddr.IP, laddr.Port) self.remoteAddr = fmt.Sprintf("%s:%d", raddr.IP, raddr.Port) //开启写操作 go self.remoteSession.WritePacket() //开启转发 go self.dispatcherPacket() //启动读取 go self.remoteSession.ReadPacket() log.Debug("RemotingClient|Start|SUCC|local:%s|remote:%s\n", self.LocalAddr(), self.RemoteAddr()) }
func (self *ApnsConnection) sendMessage(msg *entry.Message) error { //将当前的msg强制设置为当前conn的id作为标识 msg.ProcessId = self.connectionId err, packet := msg.Encode() if nil != err { return err } length, sendErr := self.conn.Write(packet) if nil != err || length != len(packet) { log.Warn("CONNECTION|SEND MESSAGE|FAIL|%s", err) } else { log.Debug("CONNECTION|SEND MESSAGE|SUCC") } return sendErr }
func (self *ApnsClient) sendMessage(msg *entry.Message) error { var sendError error //重发逻辑 for i := 0; i < 3; i++ { err, conn := self.factory.Get() if nil != err || nil == conn { log.Error("APNSCLIENT|SEND MESSAGE|FAIL|GET CONN|FAIL|%s|%s", err, *msg) sendError = err continue } //将当前enchanced发送的数据写入到storage中 if nil != self.storage && msg.MsgType == entry.MESSAGE_TYPE_ENHANCED { //正常发送的记录即可 self.storage.Insert(entry.UmarshalIdentifier(msg), msg) // if rand.Intn(100) == 0 { // log.Printf("APNSCLIENT|sendMessage|RECORD MESSAGE|%s\n", msg) // } } else { //否则丢弃不开启重发........ } //直接发送的没有返回值 sendError = conn.sendMessage(msg) self.sendCounter.Incr(1) if nil != sendError { self.failCounter.Incr(1) //连接有问题直接销毁 releaseErr := self.factory.ReleaseBroken(conn) log.Debug("APNSCLIENT|SEND MESSAGE|FAIL|RELEASE BROKEN CONN|FAIL|%s", releaseErr) } else { //发送成功归还连接 self.factory.Release(conn) break } } return sendError }
func (self *FeedbackConn) readFeedBack(limit int) { buff := make([]byte, entry.FEEDBACK_RESP, entry.FEEDBACK_RESP) for self.alive && limit > 0 { length, err := self.conn.Read(buff) //如果已经读完数据那么久直接退出 if length == -1 || nil != err { self.feedbackChan <- nil break } //读取的数据 feedback := entry.NewFeedBack(buff) self.feedbackChan <- feedback limit-- buff = buff[:entry.FEEDBACK_RESP] } //本次读取完毕 log.Debug("FEEDBACK CONNECTION|READ FEEDBACK|FINISHED!") }
//内部发送代码 func (self *ApnsHttpServer) innerSend(pushType string, token string, payload *entry.PayLoad, resp *response) { var sendFunc func() error if NOTIFY_SIMPLE_FORMAT == pushType { //如果为简单 sendFunc = func() error { return self.apnsClient.SendSimpleNotification(token, *payload) } } else if NOTIFY_ENHANCED_FORMAT == pushType { //如果为扩展的 id := self.identifierId() sendFunc = func() error { return self.apnsClient.SendEnhancedNotification(id, self.expiredTime, token, *payload) } } else { resp.Status = RESP_STATUS_INVALID_NOTIFY_FORMAT resp.Error = errors.New("Invalid notification format " + pushType) } //能直接放在chan中异步发送 var err error //如果有异常则重试发送 if RESP_STATUS_SUCC == resp.Status { err = sendFunc() if nil == err { log.Debug("APNS_HTTP_SERVER|SendNotification|SUCC|FORMAT:%s|%s", pushType, *payload) } } if nil != err { log.ErrorLog("go-apns", "APNS_HTTP_SERVER|SendNotification|FORMAT:%s|FAIL|IGNORED|%s|%s", pushType, *payload, err) resp.Status = RESP_STATUS_SEND_OVER_TRY_ERROR resp.Error = err } }
//检查idle的数据 func (self *ConnPool) checkIdle() { self.mutex.Lock() defer self.mutex.Unlock() for e := self.idlePool.Back(); nil != e; e = e.Prev() { idleconn := e.Value.(*IdleConn) //如果当前时间在过期时间之后或者活动的链接大于corepoolsize则关闭 isExpired := idleconn.expiredTime.Before(time.Now()) if isExpired || (self.idlePool.Len()+self.workPool.Len()) > self.corepoolSize { idleconn.conn.Close() idleconn = nil self.idlePool.Remove(e) log.Debug("POOL_FACTORY|evict|Expired|%d/%d/%d", self.workPool.Len(), self.idlePool.Len(), (self.workPool.Len() + self.idlePool.Len())) } } //检查当前的连接数是否满足corepoolSize,不满足则创建 enhanceSize := self.corepoolSize - (self.idlePool.Len() + self.workPool.Len()) if enhanceSize > 0 { //创建这个数量的连接 self.enhancedPool(enhanceSize) } }
func parseDB(kc KiteQConfig) store.IKiteStore { db := kc.db var kitedb store.IKiteStore if strings.HasPrefix(db, "mock://") { kitedb = &store.MockKiteStore{} } else if strings.HasPrefix(db, "memory://") { url := strings.TrimPrefix(db, "memory://") split := strings.Split(url, "&") params := make(map[string]string, len(split)) for _, v := range split { p := strings.SplitN(v, "=", 2) if len(p) >= 2 { params[p[0]] = p[1] } } initval := 10 * 10000 initcap, ok := params["initcap"] if ok { v, e := strconv.ParseInt(initcap, 10, 32) if nil != e { log.Crashf("NewKiteQServer|INVALID|INIT CAP|%s\n", db) } initval = int(v) } max := 50 * 10000 maxcap, ok := params["maxcap"] if ok { v, e := strconv.ParseInt(maxcap, 10, 32) if nil != e { log.Crashf("NewKiteQServer|INVALID|MAX CAP|%s\n", db) } max = int(v) } kitedb = sm.NewKiteMemoryStore(initval, max) } else if strings.HasPrefix(db, "mysql://") { url := strings.TrimPrefix(db, "mysql://") mp := strings.Split(url, "?") params := make(map[string]string, 5) if len(mp) > 1 { split := strings.Split(mp[1], "&") for _, v := range split { p := strings.SplitN(v, "=", 2) params[p[0]] = p[1] } } bus := 100 u, ok := params["batchUpdateSize"] if ok { v, e := strconv.ParseInt(u, 10, 32) if nil != e { log.Crashf("NewKiteQServer|INVALID|batchUpdateSize|%s\n", db) } bus = int(v) } bds := 100 d, ok := params["batchDelSize"] if ok { v, e := strconv.ParseInt(d, 10, 32) if nil != e { log.Crashf("NewKiteQServer|INVALID|batchDelSize|%s\n", db) } bds = int(v) } flushPeriod := 1 * time.Second fp, ok := params["flushPeriod"] if ok { v, e := strconv.ParseInt(fp, 10, 32) if nil != e { log.Crashf("NewKiteQServer|INVALID|batchDelSize|%s\n", db) } flushPeriod = time.Duration(v * int64(flushPeriod)) } maxConn := 500 mc, ok := params["maxConn"] if ok { v, e := strconv.ParseInt(mc, 10, 32) if nil != e { log.Crashf("NewKiteQServer|INVALID|batchDelSize|%s\n", db) } maxConn = int(v) } //解析Mysql的host master := mp[0] slave := "" mysqlHosts := strings.Split(mp[0], ",") if len(mysqlHosts) > 1 { master = mysqlHosts[0] slave = mysqlHosts[1] } //解析用户名密码: username, _ := params["username"] password, pok := params["password"] if !pok { password = "" } //shard的数量 shardnum := 8 sd, sdok := params["shardnum"] if sdok { v, e := strconv.ParseInt(sd, 10, 32) if nil != e { log.Crashf("NewKiteQServer|INVALID|ShardNum|%s\n", db) } shardnum = int(v) } options := smq.MysqlOptions{ Addr: master, SlaveAddr: slave, ShardNum: shardnum, DB: params["db"], Username: username, Password: password, BatchUpSize: bus, BatchDelSize: bds, FlushPeriod: flushPeriod, MaxIdleConn: maxConn / 2, MaxOpenConn: maxConn} kitedb = smq.NewKiteMysql(options) } else if strings.HasPrefix(db, "file://") { url := strings.TrimPrefix(db, "file://") mp := strings.Split(url, "?") params := make(map[string]string, 5) if len(mp) > 1 { split := strings.Split(mp[1], "&") for _, v := range split { p := strings.SplitN(v, "=", 2) params[p[0]] = p[1] } } if len(mp[0]) <= 0 { log.Crashf("NewKiteQServer|INVALID|FilePath|%s\n", db) } //最大消息容量 maxcap := 100 d, ok := params["cap"] if ok { v, e := strconv.ParseInt(d, 10, 32) if nil != e { log.Crashf("NewKiteQServer|INVALID|cap|%s\n", db) } maxcap = int(v) } //检查文件过期时间 checkPeriod := 1 * time.Second fp, ok := params["checkPeriod"] if ok { v, e := strconv.ParseInt(fp, 10, 32) if nil != e { log.Crashf("NewKiteQServer|INVALID|checkPeriod|%s\n", db) } checkPeriod = time.Duration(v * int64(checkPeriod)) } kitedb = smf.NewKiteFileStore(mp[0], maxcap, checkPeriod) log.Debug("NewKiteQServer|FILESTORE|%s|%d|%d", mp[0], maxcap, int(checkPeriod.Seconds())) } else { log.Crashf("NewKiteQServer|UNSUPPORT DB PROTOCOL|%s\n", db) } kc.flowstat.Kitestore = kitedb return kitedb }
func main() { logxml := flag.String("logxml", "./log/log_producer.xml", "-logxml=./log/log_producer.xml") k := flag.Int("k", 1, "-k=1 //kiteclient num ") c := flag.Int("c", 10, "-c=100") tx := flag.Bool("tx", false, "-tx=true send Tx Message") zkhost := flag.String("zkhost", "localhost:2181", "-zkhost=localhost:2181") flag.Parse() runtime.GOMAXPROCS(8) log.LoadConfiguration(*logxml) go func() { log.Info(http.ListenAndServe(":28000", nil)) }() count := int32(0) lc := int32(0) fc := int32(0) flc := int32(0) go func() { for { tmp := count ftmp := fc time.Sleep(1 * time.Second) fmt.Printf("tps:%d/%d\n", (tmp - lc), (ftmp - flc)) lc = tmp flc = ftmp } }() wg := &sync.WaitGroup{} stop := false clients := make([]*client.KiteQClient, 0, *k) for j := 0; j < *k; j++ { kiteClient := client.NewKiteQClient(*zkhost, "pb-mts-test", "123456", &defualtListener{}) kiteClient.SetTopics([]string{"trade"}) kiteClient.Start() clients = append(clients, kiteClient) time.Sleep(3 * time.Second) log.Debug("Open Client %d", j) for i := 0; i < *c; i++ { go func(kite *client.KiteQClient) { wg.Add(1) for !stop { if *tx { msg := buildBytesMessage(false) err := kite.SendTxBytesMessage(msg, doTranscation) if nil != err { fmt.Printf("SEND TxMESSAGE |FAIL|%s\n", err) atomic.AddInt32(&fc, 1) } else { atomic.AddInt32(&count, 1) } } else { txmsg := buildBytesMessage(true) err := kite.SendBytesMessage(txmsg) if nil != err { // fmt.Printf("SEND MESSAGE |FAIL|%s\n", err) atomic.AddInt32(&fc, 1) } else { atomic.AddInt32(&count, 1) } } // stop = true } wg.Done() }(kiteClient) } } var s = make(chan os.Signal, 1) signal.Notify(s, syscall.SIGKILL, syscall.SIGUSR1) //是否收到kill的命令 for { cmd := <-s if cmd == syscall.SIGKILL { break } else if cmd == syscall.SIGUSR1 { //如果为siguser1则进行dump内存 unixtime := time.Now().Unix() path := "./heapdump-producer" + fmt.Sprintf("%d", unixtime) f, err := os.Create(path) if nil != err { continue } else { debug.WriteHeapDump(f.Fd()) } } } wg.Wait() for _, k := range clients { k.Destory() } }
func (self *MockListener) OnMessage(msg *protocol.QMessage) bool { log.Debug("MockListener|OnMessage", msg.GetHeader(), msg.GetBody()) return true }