Exemple #1
0
// Create a new update client.
// the time to wait between updates in updatewait and the time to wait after failure
// before retrying in retrywait.
// The updatewait duration is also the timeout duration for the actual
// network connections.
func (c *Client) Updater(
	contentitem Content,
	updatewait time.Duration, retrywait time.Duration,
	notifyQuit *topic.Topic,
	notifySuccess *topic.Topic,
	handler func() error) {

	quit := make(chan interface{})
	notifyQuit.Register(quit)
	defer notifyQuit.Unregister(quit)

	for {
		timeout := updatewait
		log.Printf("UpdateClient: Updating data from alfred server for type %d", contentitem.GetPacketType())
		err := c.RequestContent(contentitem, handler)
		if err != nil {
			log.Printf("UpdateClient: type %d, error fetching data: %v", contentitem.GetPacketType(), err)
			timeout = retrywait
		} else {
			notifySuccess.Broadcast <- struct{}{}
		}
		select {
		case <-quit:
			break
		case <-time.After(timeout):
			continue
		}
	}
}
Exemple #2
0
// Run a background task to purge invalid items of a given type in given intervals.
// Pass a *topic.Topic to be able to receive NotifyPurge messages for each purged
// item.
func (b *DB) Purger(itemtype Item, interval time.Duration, notifyQuit, notifyPurge *topic.Topic) {
	quit := make(chan interface{})
	notifyQuit.Register(quit)
	defer notifyQuit.Unregister(quit)
actionloop:
	for {
		select {
		case <-quit:
			break actionloop
		case <-time.After(interval):
			meta := NewMeta(itemtype)
			b.Update(func(tx *bolt.Tx) error {
				return b.ForEach(tx, meta, func(cursor *bolt.Cursor) (bool, error) {
					if !meta.IsValid() {
						if notifyPurge != nil {
							notifyPurge.Broadcast <- NotifyPurge{StoreID: itemtype.StoreID(), Key: meta.Key()}
						}
						cursor.Delete()
						return true, nil
					}
					return false, nil
				})
			})
		}
	}
	if notifyPurge != nil {
		close(notifyPurge.Broadcast)
	}
}