func (s *Server) loopEvents() { ticker := time.NewTicker(time.Second) defer ticker.Stop() var tick int = 0 for s.info.State == models.PROXY_STATE_ONLINE { select { case <-s.kill: log.Infof("mark offline, proxy is killed: %s", s.info.Id) s.markOffline() case e := <-s.evtbus: evtPath := getEventPath(e) log.Infof("got event %s, %v, lastActionSeq %d", s.info.Id, e, s.lastActionSeq) if strings.Index(evtPath, models.GetActionResponsePath(s.conf.productName)) == 0 { seq, err := strconv.Atoi(path.Base(evtPath)) if err != nil { log.ErrorErrorf(err, "parse action seq failed") } else { if seq < s.lastActionSeq { log.Infof("ignore seq = %d", seq) continue } } } s.processAction(e) case <-ticker.C: if maxTick := s.conf.pingPeriod; maxTick != 0 { if tick++; tick >= maxTick { s.router.KeepAlive() tick = 0 } } } } }
func (s *Server) handleTopoEvent() { ticker := time.NewTicker(time.Second * 5) defer ticker.Stop() for { select { case e := <-s.evtbus: switch e.(type) { case *killEvent: s.handleMarkOffline() default: evtPath := getEventPath(e) log.Infof("got event %s, %v, lastActionSeq %d", s.info.Id, e, s.lastActionSeq) if strings.Index(evtPath, models.GetActionResponsePath(s.conf.productName)) == 0 { seq, err := strconv.Atoi(path.Base(evtPath)) if err != nil { log.WarnErrorf(err, "parse action seq failed") } else { if seq < s.lastActionSeq { log.Infof("ignore seq = %d", seq) continue } } } log.Infof("got event %s, %v, lastActionSeq %d", s.info.Id, e, s.lastActionSeq) s.processAction(e) } case <-ticker.C: for _, bc := range s.pool { bc.KeepAlive() } } } }
// TODO Lius: main event handler waiting for prepared struct (already do read) func (s *Server) handleTopoEvent() { // do Listen in Server.Run by outer main invoke for { select { case r := <-s.reqCh: // Lius: send to backend if s.slots[r.slotIdx].slotInfo.State.Status == models.SLOT_STATUS_PRE_MIGRATE { s.bufferedReq.PushBack(r) continue } // Lius: buffed request because of migrating keys or other strategy for e := s.bufferedReq.Front(); e != nil; { next := e.Next() s.dispatch(e.Value.(*PipelineRequest)) s.bufferedReq.Remove(e) e = next } // Lius: send current request s.dispatch(r) case e := <-s.evtbus: switch e.(type) { case *killEvent: s.handleMarkOffline() e.(*killEvent).done <- nil default: evtPath := GetEventPath(e) log.Infof("got event %s, %v, lastActionSeq %d", s.pi.Id, e, s.lastActionSeq) if strings.Index(evtPath, models.GetActionResponsePath(s.conf.productName)) == 0 { seq, err := strconv.Atoi(path.Base(evtPath)) if err != nil { log.Warning(err) } else { if seq < s.lastActionSeq { log.Info("ignore", seq) continue } } } log.Infof("got event %s, %v, lastActionSeq %d", s.pi.Id, e, s.lastActionSeq) s.processAction(e) } } } }
func (top *Topology) GetActionResponsePath(seq int) string { return path.Join(models.GetActionResponsePath(top.ProductName), top.zkConn.Seq2Str(int64(seq))) }