func UpdateRedis(readChan chan *Distribution, id int) error { var redisConn redis.Conn var now int lastStatusTime := int(time.Now().Unix()) updateCount := 0 for dist := range readChan { // Only do a update if we have all the data necissary or we expect // there to be a decay event now = int(time.Now().Unix()) if dist.Full() || float64(now-dist.LastSyncT)*dist.Rate > 0.75 { redisConn = redisServer.GetConnection() err := UpdateDistribution(redisConn, dist) if err != nil { log.Printf("[%d] Failed to update: %s: %v: %s", id, dist.Name, redisConn.Err(), err.Error()) } updateCount += 1 if now-lastStatusTime > *UpdateOutputTime { rate := float64(updateCount) / float64(now-lastStatusTime) log.Printf("[%d] Performing redis updates at %e updates/second", id, rate) lastStatusTime = now updateCount = 0 } redisConn.Close() } } return nil }
func UpdateRedis(readChan chan *Distribution, id int) error { var redisConn redis.Conn for dist := range readChan { log.Printf("[%d] Updating distribution: %s", id, dist.Name) redisConn = redisServer.GetConnection() err := UpdateDistribution(redisConn, dist) if err != nil { log.Printf("[%d] Failed to update: %s: %v: %s", id, dist.Name, redisConn.Err(), err.Error()) } redisConn.Close() } return nil }
func (r *RedisStorage) Do(commandName string, args ...interface{}) (interface{}, error) { var conn redis.Conn i := r.retry for ; i > 0; i-- { conn = r.pool.Get() err := conn.Err() if err == nil { break } else { //log.Warnf("failed to get conn from pool (%s)", err) } time.Sleep(2 * time.Second) } if i == 0 || conn == nil { return nil, fmt.Errorf("failed to find a useful redis conn") } else { ret, err := conn.Do(commandName, args...) conn.Close() return ret, err } }
func (p *connectionPool) put(conn redis.Conn) { p.mu.Lock() defer p.mu.Unlock() if conn == nil || conn.Err() != nil { // Failed to dial, closed, or some other problem if p.outstanding > 0 { p.outstanding-- } p.co.Signal() // someone can try to dial return } if len(p.available) >= p.max { go conn.Close() // don't block return } p.available = append(p.available, conn) if p.outstanding > 0 { p.outstanding-- } p.co.Signal() }