func (self *RedisQ) Init() error { log.Debug("init redis queue") addr := flag.Lookup("redis").Value.(flag.Getter).Get().(string) self.client = redis.NewTCPClient(&redis.Options{ Addr: addr, Password: "", // no password set DB: 0, // use default DB }) _, err := self.client.Ping().Result() return err }
// setupRedis creates a redis client and pubsub func setupRedis() { client = redis.NewTCPClient(&redis.Options{ Addr: config.RedisHost, Password: config.RedisPass, DB: config.RedisDB, }) if ping := client.Ping(); ping.Err() != nil { log.Fatal(ping.Err()) } pubsub = client.PubSub() socketMap = NewSocketMap(pubsub) }
func initRedis(addr string, db int64, pw string) { rds = redis.NewTCPClient(addr, pw, db) ret := rds.ScriptLoad(` local key, value, maxlength = KEYS[1], ARGV[1], tonumber(ARGV[2]) if not maxlength then return {err = "INVALID ARGUMENTS"} end redis.call("RPUSH", key, value) local listlength = redis.call("LLEN", key) if listlength >= maxlength then local start = listlength - maxlength redis.call("LTRIM", key, start, maxlength) end `) rdsCircularBuffer = ret.Val() ret = rds.ScriptLoad(` local key = KEYS[1] return redis.call("ZRANGEBYSCORE", key, 1, 3) `) rdsGetIPCache = ret.Val() ret = rds.ScriptLoad(` local key, value, maxlength = KEYS[1], ARGV[1], 3 local count = redis.call("ZCOUNT", key, 1, maxlength) local existingscore = redis.call("ZSCORE", key, value) if existingscore then -- renumber all the elements and make this one the last local elements = redis.call("ZRANGEBYSCORE", key, 1, maxlength) local i = 1 for _, v in ipairs(elements) do if v == value then redis.call("ZADD", key, count, v) else redis.call("ZADD", key, i, v) i = i + 1 end end return end if count == maxlength then -- delete the first element, modify the other elements score down -- and add the new one to the end redis.call("ZREMRANGEBYSCORE", key, 1, 1) local elements = redis.call("ZRANGEBYSCORE", key, 2, maxlength) local i = 1 for _, v in ipairs(elements) do redis.call("ZADD", key, i, v) i = i + 1 end return redis.call("ZADD", key, count, value) else -- otherwise just insert it with the next score return redis.call("ZADD", key, count + 1, value) end `) rdsSetIPCache = ret.Val() go (func() { t := time.NewTicker(time.Minute) cp := watchdog.register("redis check thread", time.Minute) defer watchdog.unregister("redis check thread") for { select { case <-t.C: cp <- true ping := rds.Ping() if ping.Err() != nil || ping.Val() != "PONG" { D("Could not ping redis: ", ping.Err()) rds.Close() initRedis(addr, db, pw) return } } } })() }