//headWithRange checks node n has records with range and adds records which should be downloaded to downloadmanager.
func headWithRange(n *node.Node, c *thread.Cache, dm *Manager) bool {
	begin := time.Now().Unix() - cfg.GetRange
	if rec, err := recentlist.Newest(c.Datfile); err == nil {
		begin = rec.Stamp - cfg.GetRange
	}
	if cfg.GetRange == 0 || begin < 0 {
		begin = 0
	}
	res, err := n.Talk(fmt.Sprintf("/head/%s/%d-", c.Datfile, begin), nil)
	if err != nil {
		return false
	}
	if len(res) == 0 {
		ress, errr := n.Talk(fmt.Sprintf("/have/%s", c.Datfile), nil)
		if errr != nil || len(ress) == 0 || ress[0] != "YES" {
			manager.RemoveFromTable(c.Datfile, n)
		} else {
			manager.AppendToTable(c.Datfile, n)
		}
		return false
	}
	manager.AppendToTable(c.Datfile, n)
	dm.Set(res, n)
	return true
}
Exemple #2
0
//RecentStamp  returns time of getting by /recent.
func (c *Cache) RecentStamp() int64 {
	n, err := recentlist.Newest(c.Datfile)
	s := c.Stamp()
	if err != nil || n.Stamp < s {
		return s
	}
	return n.Stamp
}
//bg waits for at least one record in the cache.
func bg(c *thread.Cache, wg *sync.WaitGroup) {
	w := 2 * time.Second
	newest, err := recentlist.Newest(c.Datfile)
	var done chan struct{}
	go func() {
		wg.Wait()
		done <- struct{}{}
	}()
	if err == nil && (newest.Stamp == c.Stamp()) {
		return
	}
	for {
		select {
		case <-done:
			return
		case <-time.After(w):
			w += time.Second
			if c.HasRecord() || w >= 5*time.Second {
				return
			}
		}
	}
}