Пример #1
0
// Run triggers periodic syncing of services and checks with Consul.  This is
// a long lived go-routine which is stopped during shutdown.
func (c *Syncer) Run() {
	sync := time.NewTimer(0)
	for {
		select {
		case <-sync.C:
			d := syncInterval - lib.RandomStagger(syncInterval/syncJitter)
			sync.Reset(d)

			if err := c.SyncServices(); err != nil {
				if c.consulAvailable {
					c.logger.Printf("[DEBUG] consul.syncer: error in syncing: %v", err)
				}
				c.consulAvailable = false
			} else {
				if !c.consulAvailable {
					c.logger.Printf("[DEBUG] consul.syncer: syncs succesful")
				}
				c.consulAvailable = true
			}
		case <-c.notifySyncCh:
			sync.Reset(syncInterval)
		case <-c.shutdownCh:
			c.Shutdown()
		case <-c.notifyShutdownCh:
			sync.Stop()
			c.logger.Printf("[INFO] consul.syncer: shutting down syncer ")
			return
		}
	}
}
Пример #2
0
// StartDiskWatcher periodically checks the disk space consumed by the shared
// allocation directory.
func (d *AllocDir) StartDiskWatcher() {
	start := time.Now()

	sync := time.NewTimer(d.MaxCheckDiskInterval)
	defer sync.Stop()

	d.running = true
	d.watchCh = make(chan struct{})

	for {
		select {
		case <-d.watchCh:
			return
		case <-sync.C:
			if err := d.syncDiskUsage(); err != nil {
				log.Printf("[WARN] client: failed to sync disk usage: %v", err)
			}
			// Calculate the disk ratio.
			diskRatio := float64(d.size) / float64(d.MaxSize*structs.BytesInMegabyte)

			// Exponentially decrease the interval when the disk ratio increases.
			nextInterval := time.Duration(int64(1.0/(0.1*math.Pow(diskRatio, 2))+5)) * time.Second

			// Use the maximum interval for the first five minutes or if the
			// disk ratio is sufficiently high. Also use the minimum check interval
			// if the disk ratio becomes low enough.
			if nextInterval < d.MaxCheckDiskInterval || time.Since(start) < d.CheckDiskMaxEnforcePeriod {
				nextInterval = d.MaxCheckDiskInterval
			} else if nextInterval > d.MinCheckDiskInterval {
				nextInterval = d.MinCheckDiskInterval
			}
			sync.Reset(nextInterval)
		}
	}
}