Ejemplo n.º 1
0
// Attempt to use SETNX to assert that we have the control thread.
// If this happens, we have to set the expiration time.
func grabControlThread(c redis.Client) bool {
	val, err := c.Setnx(CONTROL_THREAD_LOCK, []byte(fmt.Sprint(*hostId)))
	if err == nil {
		if val {
			// New lock acquired, attempt to set expiry properly
			log.Info("grabControlThread: Acquired lock")
			c.Expire(CONTROL_THREAD_LOCK, CONTROL_THREAD_EXPIRY)
			return true
		} else {
			// Already there
			return false
		}
	} else {
		log.Err("grabControlThread: " + err.Error())
		return true
	}
	log.Warning("grabControlThread: Should never get here, defaulting to false")
	return false
}