// cut down HP func (this *Watcher) hurt(ip string) { beelog.Trace("hurt", ip) h, ok := this.Hosts[ip] if !ok { return } if h.HP -= 1; h.HP < 0 { h.HP = 0 } this.updateState(ip) }
// recover HP func (this *Watcher) fix(ip string) { this.Lock() defer this.Unlock() beelog.Trace("fix", ip) h, ok := this.Hosts[ip] if !ok { this.Hosts[ip] = &Host{IP: ip, Time: time.Now(), HP: LevelFullHP, Alive: true} beelog.Trace("Len hosts:", len(this.Hosts)) return } h.HP += 1 if h.HP > LevelFullHP { h.HP = LevelFullHP } if h.HP < LevelInitHP { h.HP = LevelInitHP } h.Time = time.Now() this.updateState(ip) }
// When host is dead or come to alive, chan calls. func (this *Watcher) Watch() (chan Host, error) { ch, err := this.listen() if err != nil { return nil, err } go func() { for { ip := <-ch beelog.Trace("read channel ip:", ip) this.fix(ip) } }() notify := make(chan Host, 50) go this.drain(notify) // clean program return notify, nil }