func (this *Ext) Collect() { for { now := time.Now() timeDelta := now.Sub(this.timestamp) timeCondition := (this.timestamp.Before(now) && timeDelta > this.period) if timeCondition || this.init { this.init = false logging.Info(fmt.Sprintf("GET %s", this.url)) resp, err := http.Get(this.url) if err != nil { logging.Error(err) return } payload, err := ioutil.ReadAll(resp.Body) if err != nil { logging.Error(err) return } this.dataCh <- payload } select { case <-time.After(this.period): continue case <-this.quitCh: return } } }
func (this *Manager) CacheHandler(w http.ResponseWriter, r *http.Request) { this.logAccess(r) latestItem, err := this.cache.Get() if err != nil { logging.Error(err) w.WriteHeader(500) return } response, err := latestItem.JSON() if err != nil { logging.Error(err) w.WriteHeader(500) return } w.Header().Set("Content-Type", "application/json") w.WriteHeader(200) w.Write(response) }