diff --git a/cmd/stock/main.go b/cmd/stock/main.go index 29bbfde..a1c164c 100644 --- a/cmd/stock/main.go +++ b/cmd/stock/main.go @@ -97,7 +97,6 @@ func main() { } } - user.ForEachUser(func(u *user.User) bool { if u.IsStop() { return true @@ -172,6 +171,10 @@ func main() { logx.Errorf("get stock err: %v", err) } + if !st.Notify() { + continue + } + msg_ := st.Msg() if msg_ != "" { err = msg.Send(msg_) @@ -199,6 +202,8 @@ func main() { return true }) + + st.FinishSendAll() } } }) diff --git a/stock/stock.go b/stock/stock.go index eae47d8..d286681 100644 --- a/stock/stock.go +++ b/stock/stock.go @@ -30,6 +30,7 @@ type stock struct { lastRise float64 nowRise float64 nowRiseStr string + needSend bool } func (s *stock) Code() string { @@ -59,6 +60,7 @@ func (s *stock) notify() bool { s.nowRiseStr = fmt.Sprintf("%.2f%%", s.nowRise) if (s.lastRise == 0 && rs != 0) || rs-s.lastRise >= cfg.ThresholdValue || s.lastRise-rs >= cfg.ThresholdValue { s.lastRise = rs + s.needSend = true return true } return false @@ -250,12 +252,32 @@ func (sk *stocks) Update() error { return nil } +func (sk *stocks) Notify() bool { + sk.mx.RLock() + defer sk.mx.RUnlock() + var notify bool + for _, s := range sk.stkMap { + if s.notify() { + notify = true + } + } + return notify +} + +func (sk *stocks) FinishSendAll() { + sk.mx.Lock() + defer sk.mx.Unlock() + for _, s := range sk.stkMap { + s.needSend = false + } +} + func (sk *stocks) Msg() string { sk.mx.RLock() defer sk.mx.RUnlock() var resp string for _, s := range sk.stkMap { - if s.notify() { + if s.needSend { msg := s.Msg() resp = resp + msg + "\n" }