Beispiel #1
0
func (subscriber *Subscriber) Work(queueName string, concurrency int, fn Worker) {
	subscriber.isRunning = true
	subscriber.workChannel = make(chan string)
	for i := 0; i < concurrency; i++ {
		subscriber.wg.Add(1)
		go subscriber.doWork(fn)
	}
	go func() {
		for subscriber.isRunning {
			connection := subscriber.connectionPool.Get()
			data, err := connection.Do("BRPOP", "ceausescu:"+queueName, 0)
			if err != nil {
				log.Println(err)
				continue
			}
			connection.Close()
			result, err := redis.StringMap(data, nil)
			if err != nil {
				log.Println(err)
				continue
			}
			subscriber.workChannel <- result["ceausescu:"+queueName]
		}
	}()
}
Beispiel #2
0
func instanceIsMaster(pool *redis.Pool, port string) {
	c := pool.Get()
	defer c.Close()

	for {
		master, err := redis.StringMap(c.Do("CONFIG", "GET", "slaveof"))
		if err != nil {
			// Retry connection to Redis until it is back.
			//log.Println(err)
			defer c.Close()
			time.Sleep(time.Second * time.Duration(connectionLostInterval))
			c = pool.Get()
			continue
		}
		for _, value := range master {
			if value != "" {
				// Instance is now a slave, notify.
				if fetchPossible[port] {
					c.Do("PUBLISH", "redis-scouter", "stop")
					fetchPossible[port] = false
					log.Printf("[instance-check-%s] became a slave", port)
				}
			} else {
				// Re-enable metrics.
				if !fetchPossible[port] {
					fetchPossible[port] = true
					log.Printf("[instance-check-%s] became a master", port)
				}
			}
		}
		time.Sleep(time.Second * time.Duration(masterCheckInterval))
	}
}
Beispiel #3
0
func GetRedisSlaves(node *HANodeSt) {
	redisConn := gRedisPool.Get()
	defer redisConn.Close()
	//values, err := redis.Strings(redisConn.Do("SENTINEL", "slaves", "mymaster" ))
	//values, err := redisConn.Do("SENTINEL", "slaves", "mymaster" )
	//values, err := redisConn.Do("get", "hello1" )
	values, err := redis.Values(redisConn.Do("SENTINEL", "slaves", node.Name))

	if err != nil {
		log.Errorf("get master error:%s", err.Error())
	}

	slaveNo := len(values)
	slaveList := list.New()
	for i := 0; i < slaveNo; i++ {
		smap, _ := redis.StringMap(values[i], nil)
		host := smap["ip"]
		port := smap["port"]
		flag := smap["flags"]
		if flag != "slave" {
			continue
		}
		slaveList.PushBack(host + ":" + port)
	}
	node.Slaves = slaveList
	//gHANodeMap[item] = node
}
//GetJobs returns all the command jobs
func (client *clientImpl) GetJobs(ID string, timeout int) ([]*Job, error) {
	db := client.redis.Get()
	defer db.Close()

	queue := fmt.Sprintf(cmdQueueCmdQueued, ID)
	data, err := db.Do("BRPOPLPUSH", queue, queue, timeout)
	if err != nil {
		return nil, err
	}

	if data == nil {
		return nil, TIMEOUT
	}
	resultQueue := fmt.Sprintf(hashCmdResults, ID)
	jobsdata, err := redis.StringMap(db.Do("HGETALL", resultQueue))

	if err != nil {
		return nil, err
	}

	results := make([]*Job, 0, 10)
	for _, jobdata := range jobsdata {
		result := &Job{}
		if err := json.Unmarshal([]byte(jobdata), result); err != nil {
			return nil, err
		}

		result.redis = client.redis
		results = append(results, result)
	}

	return results, nil
}
// Expired implements the `func Expired` defined on the Strategy interface. It
// scans iteratively over the Heart's `location` field to look for items that
// have expired. An item is marked as expired iff the last update time happened
// before the instant of the maxAge subtracted from the current time.
func (s HashExpireyStrategy) Expired(location string,
	pool *redis.Pool) (expired []string, err error) {

	now := time.Now()

	cnx := pool.Get()
	defer cnx.Close()

	reply, err := redis.StringMap(cnx.Do("HGETALL", location))
	if err != nil {
		return
	}

	for id, tick := range reply {
		lastUpdate, err := time.Parse(DefaultTimeFormat, tick)

		if err != nil {
			continue
		} else if lastUpdate.Add(s.MaxAge).Before(now) {
			expired = append(expired, id)
		}
	}

	return
}
Beispiel #6
0
func forward(incoming, outgoing chan slack.Message) {
	c := redisPool.Get()
	defer c.Close()
	for m := range incoming {
		if m.Type != "message" {
			continue
		}
		ok, err := redis.Bool(c.Do("SISMEMBER", KEY_CHANNELS, m.Channel))
		if err != nil {
			log.Println(err)
		} else if ok {
			fwdMap, err := redis.StringMap(c.Do("HGETALL", m.Channel))
			if err != nil {
				log.Println(err)
			} else {
				for query, toChanId := range fwdMap {
					ok, err := regexp.MatchString(query, m.Text)
					if err != nil {
						log.Println(err)
					} else if ok {
						m.Channel = toChanId
						outgoing <- m
					}
				}
			}
		}
	}
}
Beispiel #7
0
func (rr *Redrec) getItemScores(item string, max int) (map[string]string, error) {
	scores, err := redis.StringMap(rr.rconn.Do("ZREVRANGE", fmt.Sprintf("item:%s:scores", item), 0, max))
	if err != nil {
		return nil, err
	}

	return scores, nil
}
Beispiel #8
0
func queueStats(port string) {
	// Connect to redis.
	pool := newPool(port)
	c := pool.Get()
	if !instanceAlive(pool) {
		log.Printf("error: cannot connect to redis on port %s, aborting\n", port)
		return
	}

	// Subscribe to the keyspace notifications.
	c.Send("PSUBSCRIBE", "__keyspace@*", "redis-scouter")
	c.Flush()
	// Ignore first two ACKs when subscribing.
	c.Receive()
	c.Receive()

	go instanceIsMaster(pool, port)
	go keyspaceEnable(pool, port)

	for {
		if fetchPossible[port] {
			reply, err := redis.StringMap(c.Receive())
			if err != nil {
				// Retry connection to Redis until it is back.
				defer c.Close()
				log.Printf("connection to redis lost. retry in %ds\n", connectionLostInterval)
				time.Sleep(time.Second * time.Duration(connectionLostInterval))
				c = pool.Get()
				//go keyspaceEnable(pool, port)
				c.Send("PSUBSCRIBE", "__keyspace@*", "redis-scouter")
				c.Flush()
				c.Receive()
				c.Receive()
				continue
			}
			// Match for a LIST keyspace event.
			for k, v := range reply {
				if v == "stop" {
					// Break loop if we get a message on redis-scouter pubsub.
					continue
				}
				operation := listOperationsRegex.FindString(v)
				queue := keyspaceRegex.FindStringSubmatch(k)
				if len(queue) == 2 && operation != "" {
					Stats.Add(fmt.Sprintf("%s.%s.%s", port, queue[1], operation), 1)
				}
			}
		} else {
			// Do not fetch stats for now.
			time.Sleep(time.Second * time.Duration(fetchMetricsInterval))
		}
	}
}
Beispiel #9
0
// GetStringMap returns map[string]string, err by its key
//returns nil  and a filled error if something wrong happens
func (r *Service) GetStringMap(key string) (map[string]string, error) {
	redisVal, err := r.Get(key)
	if redisVal == nil {
		return nil, ErrKeyNotFound.Format(key)
	}

	_map, err := redis.StringMap(redisVal, err)
	if err != nil {
		return nil, err
	}
	return _map, nil
}
Beispiel #10
0
func c_redis_counters(server string, db int) (opentsdb.MultiDataPoint, error) {
	var md opentsdb.MultiDataPoint
	conn, err := redis.Dial("tcp", server, redis.DialDatabase(db))
	if err != nil {
		return md, err
	}
	defer conn.Close()
	if _, err := conn.Do("CLIENT", "SETNAME", "scollector"); err != nil {
		return md, err
	}
	cursor := 0
	for {
		vals, err := redis.Values(conn.Do("HSCAN", collect.RedisCountersKey, cursor))
		if err != nil {
			return md, err
		}
		if len(vals) != 2 {
			return md, fmt.Errorf("Unexpected number of values")
		}
		cursor, err = redis.Int(vals[0], nil)
		if err != nil {
			return md, err
		}
		pairs, err := redis.StringMap(vals[1], nil)
		if err != nil {
			return md, err
		}
		for mts, val := range pairs {
			parts := strings.Split(mts, ":")
			if len(parts) != 2 {
				slog.Errorf("Invalid metric tag set counter: %s", mts)
				continue
			}
			metric := parts[0]
			tags, err := opentsdb.ParseTags(parts[1])
			if err != nil {
				slog.Errorf("Invalid tags: %s", parts[1])
				continue
			}
			v, err := strconv.Atoi(val)
			if err != nil {
				slog.Errorf("Invalid counter value: %s", val)
				continue
			}
			Add(&md, metric, v, tags, metadata.Counter, metadata.Count, "")
		}
		if cursor == 0 {
			break
		}
	}
	return md, nil
}
Beispiel #11
0
func (r *Checkpointer) Begin() error {
	conn := r.pool.Get()
	defer conn.Close()
	res, err := conn.Do("HGETALL", r.redisPrefix+".sequence")
	r.heads, err = redis.StringMap(res, err)
	if err != nil {
		return err
	}

	r.wg.Add(1)
	go r.RunCheckpointer()
	return nil
}
Beispiel #12
0
func (this *UserOption) GetAll() []*User {
	c := this.RO.Pool.Get()
	defer c.Close()
	length, _ := redis.Int(c.Do("LLEN", this.userListKey))
	users := make([]*User, length)
	userNames, _ := redis.Strings(c.Do("LRANGE", this.userListKey, 0, length-1))
	for k, userName := range userNames {
		userMap, _ := redis.StringMap(c.Do("HGETALL", this.nameKey(userName)))
		time, _ := strconv.ParseInt(userMap["time"], 10, 64)
		users[k] = NewUser(userMap["name"], userMap["nick"], userMap["password"], userMap["salt"], time)
	}
	return users
}
Beispiel #13
0
func c_redis_counters(server string, db int) (opentsdb.MultiDataPoint, error) {
	var md opentsdb.MultiDataPoint
	conn, err := redis.Dial("tcp", server, redis.DialDatabase(db))
	if err != nil {
		return md, slog.Wrap(err)
	}
	defer conn.Close()

	//do a dance to detect proper hscan command for ledis or redis
	hscanCmd := "XHSCAN"
	info, err := redis.String(conn.Do("info", "server"))
	if err != nil {
		return md, slog.Wrap(err)
	}
	if strings.Contains(info, "redis_version") {
		hscanCmd = "HSCAN"
	}

	cursor := "0"
	for {
		vals, err := redis.Values(conn.Do(hscanCmd, collect.RedisCountersKey, cursor))
		if err != nil {
			return md, slog.Wrap(err)
		}
		if len(vals) != 2 {
			return md, fmt.Errorf("Unexpected number of values")
		}
		cursor, err = redis.String(vals[0], nil)
		if err != nil {
			return md, slog.Wrap(err)
		}
		pairs, err := redis.StringMap(vals[1], nil)
		if err != nil {
			return md, slog.Wrap(err)
		}
		for key, val := range pairs {
			ak := models.AlertKey(key)

			v, err := strconv.Atoi(val)
			if err != nil {
				slog.Errorf("Invalid counter value: %s", val)
				continue
			}
			Add(&md, ak.Name(), v, ak.Group(), metadata.Counter, metadata.Count, "")
		}
		if cursor == "" || cursor == "0" {
			break
		}
	}
	return md, nil
}
Beispiel #14
0
// HGETALL() does the "HGETALL" command.
//
//   Params:
//       conn: redis.Conn.
//       key: key stores the hash.
//   Return:
//       m: map contains the specified fields and their respective values.
//       err: nil if no error occurs or specified error otherwise.
func HGETALL(conn redis.Conn, key string) (m map[string]string, err error) {
	if err = CheckKey(key); err != nil {
		return nil, err
	}

	if m, err = redis.StringMap(conn.Do("HGETALL", key)); err != nil {
		if DEBUG {
			fmt.Printf("HGETALL(%v) err: %v\n", key, err)
		}
		return nil, err
	}

	return m, nil
}
Beispiel #15
0
func (this *UserOption) Get(name string) (*User, error) {
	c := this.RO.Pool.Get()
	defer c.Close()
	key := this.nameKey(name)
	if existed, _ := redis.Int(c.Do("EXISTS", key)); existed == 0 {
		return nil, fmt.Errorf("不存在")
	}
	userMap, err := redis.StringMap(c.Do("HGETALL", key))
	if err != nil {
		return nil, err
	}
	time, _ := strconv.ParseInt(userMap["time"], 10, 64)
	return NewUser(userMap["name"], userMap["nick"], userMap["password"], userMap["salt"], time), nil
}
Beispiel #16
0
func (m MRedis) Hgetall(s string) {
	c, err := redis.Dial("tcp", "127.0.0.1:6379", redis.DialDatabase(2))
	if err != nil {
		panic(err)
	}
	defer c.Close()
	ok, err := redis.StringMap(c.Do("HGETALL", "*.pplive.com"))
	if err != nil {
		panic(err)
	}
	for k, v := range ok {
		fmt.Printf("%s = %s\n", k, v)
	}

}
Beispiel #17
0
func loadNodes() error {
	nodes = make(map[string]*node)
	servers, err := redis.StringMap(do("HGETALL", "servers"))
	if err != nil {
		return err
	}
	for name, ip := range servers {
		nn, err := newNode(name, ip)
		if err != nil {
			return err
		}
		nodes[nn.name] = nn
	}
	return nil
}
Beispiel #18
0
func (client *client) GetConfig(key string) (string, error) {
	configCommand := client.lookupAlias("CONFIG")

	output, err := redisclient.StringMap(client.connection.Do(configCommand, "GET", key))
	if err != nil {
		return "", err
	}

	value, found := output[key]
	if !found {
		return "", fmt.Errorf("Key '%s' not found", key)
	}

	return value, nil
}
Beispiel #19
0
func checkUser(regUser UserInfo) (map[string]string, error) {
	c, err := redis.Dial("tcp", "localhost:6379")
	errStr := make(map[string]string)
	if err != nil {
		fmt.Println("Connect to redis error", err)
		return errStr, err
	}
	defer c.Close()

	if res, err := redis.StringMap(c.Do("hgetall", userListKey+regUser.UserName)); err != nil {
		return errStr, err
	} else {
		return res, nil
	}
}
Beispiel #20
0
func TestHashesUsingDo(t *testing.T) {
	//tu.SkipLog(t)

	c := GetRedis().Conn
	//c := GetRedisInstance().Pool.Get()
	c.Do("HMSET", "key:subkey1", "field1", 1, "field2", 2)

	fields, err := redis.Ints(c.Do("HMGET", "key:subkey1", "field1", "field2"))
	if err != nil || fields[0] != 1 || fields[1] != 2 {
		t.Errorf("field1 should be 1, field2 should be 2 but result is %#v: err is %s", fields, err)
	}

	//HGETALL
	fields2, _ := redis.StringMap(c.Do("HGETALL", "key:subkey1"))
	lg.Debugf("HGETALL: %v, %s, %s", fields2, fields2["field1"], fields2["field2"])
}
Beispiel #21
0
func updateCache() {

	c, err := redis.Dial("tcp", "172.16.25.133:6379", redis.DialReadTimeout(time.Second*time.Duration(1)))

	if err != nil {
		log.Error(err)
		return
	}
	defer c.Close()

	values, err := redis.StringMap(c.Do("HGETALL", "se:adspaceMap"))
	if err != nil {
		log.Error(err)
		return
	}

	for k, v := range values {
		log.Info(k, v)

		var adspaceEntity AdspaceEntity
		json.Unmarshal([]byte(v), &adspaceEntity)

		if adspaceEntity.DemandAdspaces != nil && len(adspaceEntity.DemandAdspaces) >= 1 {
			for _, adspace := range adspaceEntity.DemandAdspaces {
				if adspace == nil || adspace.Timeout <= 0 || adspace.Weight <= 0 || adspace.DemandAdspaceKey == "" {
					log.Warn(k, v)
					continue
				}
				if adspace.OsType != "" {
					as := adspaceCache[k+"_"+adspace.OsType]
					as = append(as, *adspace)
					adspaceCache[k+"_"+adspace.OsType] = as
				} else {
					as_android := adspaceCache[k+"_"+system.OS_ANDROID]
					as_android = append(as_android, *adspace)
					as_ios := adspaceCache[k+"_"+system.OS_IOS]
					as_ios = append(as_ios, *adspace)

					adspaceCache[k+"_"+system.OS_ANDROID] = as_android
					adspaceCache[k+"_"+system.OS_IOS] = as_ios
				}
			}
		}

	}

}
Beispiel #22
0
func (redisSource *RedisSource) Get() (map[string]interface{}, error) {
	connection, err := redis.DialURL(redisSource.URI)

	if err != nil {
		return nil, err
	}

	reply, err := redis.StringMap(connection.Do("HGETALL", redisSource.Key))

	result := make(map[string]interface{})

	for key, value := range reply {
		result[key] = value
	}

	return result, nil
}
Beispiel #23
0
func getMailConfigFromRedis() (map[string]string, error) {
	defer func() {
		msg := recover()
		if msg != nil {
			fmt.Println("Panic error :", msg)
			return
		}
	}()
	conn := driver.RedisPool.Get()
	defer conn.Close()

	m, err := redis.StringMap(conn.Do("hgetall", REDIS_SEND_MAIL_CONFIG_TABLE))
	if err != nil {
		return nil, err
	}
	return m, nil
}
func (a *Alerter) GetAll(key_search string) (map[string][]map[string]interface{}, error) {
	var alerts = make(map[string][]map[string]interface{})
	conn := a.pool.Get()
	defer conn.Close()
	keys, _ := redis.Strings(conn.Do("KEYS", key_search))
	for _, v := range keys {
		capsule_id := strings.Split(v, ":")[1]
		current, _ := redis.StringMap(conn.Do("HGETALL", v))
		for _, alert := range current {
			var dat map[string]interface{}
			if err := json.Unmarshal([]byte(alert), &dat); err != nil {
				return alerts, err
			}
			alerts[capsule_id] = append(alerts[capsule_id], dat)
		}
	}
	return alerts, nil
}
Beispiel #25
0
func (this *TSKLOption) Get(user *User) (*TSKL, error) {
	c := this.RO.Pool.Get()
	defer c.Close()
	key := this.nameKey(user.Name)
	if existed, _ := redis.Int(c.Do("EXISTS", key)); existed == 0 {
		return nil, fmt.Errorf("不存在")
	}
	tsklMap, err := redis.StringMap(c.Do("HGETALL", key))
	if err != nil {
		return nil, err
	}
	tskl := NewTSKL(user)
	tskl.Hash = tsklMap["hash"]
	tskl.Send = tsklMap["send"]
	tskl.Back = tsklMap["back"]
	tskl.Berr = tsklMap["berr"]
	return tskl, nil
}
Beispiel #26
0
// GetAll returns all keys and their values from a specific key (map[string]string)
// returns a filled error if something bad happened
func (r *Service) GetAll(key string) (map[string]string, error) {
	c := r.pool.Get()
	defer c.Close()
	if err := c.Err(); err != nil {
		return nil, err
	}

	reply, err := c.Do("HGETALL", r.Config.Prefix+key)

	if err != nil {
		return nil, err
	}
	if reply == nil {
		return nil, ErrKeyNotFound.Format(key)
	}

	return redis.StringMap(reply, err)

}
func (p *RedisCounterStorage) GetOptions(counterName, key string) (opts []CounterOption, exist bool) {
	conn := p.pool.Get()
	defer conn.Close()

	redisKey := p.redisConfig.Prefix + _CONFIG_ + counterName + ":" + key

	vals, err := conn.Do("HGETALL", redisKey)

	if kvVals, e := redis.StringMap(vals, err); e != nil {
		exist = false
		return
	} else {
		for k, v := range kvVals {
			opts = append(opts, CounterOption{OptionName(k), v})
		}
		exist = true
	}

	return
}
func (ut *UserToken) getUserIdFromTokenCache() bool {
	conn := config.RedisPool.Get()
	defer conn.Close()
	v, err := redis.Values(conn.Do("HGETALL", "Token:"+ut.Token))

	if len(v) == 0 {
		return false
	}
	if err != nil {
		log.Err(ae.Redis("", err))
		return false
	}
	vMap, _ := redis.StringMap(v, nil)
	ut.UserID, _ = strconv.ParseUint(vMap["user_id"], 10, 64)
	ut.RefreshToken, _ = vMap["refresh_token"]
	ut.Created, _ = time.Parse("2006-01-02 15:04:05.999999999 -0700 -0700", vMap["created"])
	ut.Expires, _ = time.Parse("2006-01-02 15:04:05.999999999 -0700 -0700", vMap["expires"])

	return true
}
func (sched *Scheduler) Start() {
	db := sched.pool.Get()
	defer db.Close()

	var cursor int
	for {
		slice, err := redis.Values(db.Do("HSCAN", hashScheduleKey, cursor))
		if err != nil {
			log.Println("Failed to load schedule from redis", err)
			break
		}

		var fields interface{}
		if _, err := redis.Scan(slice, &cursor, &fields); err == nil {
			set, _ := redis.StringMap(fields, nil)

			for key, cmd := range set {
				job := &SchedulerJob{}

				err := json.Unmarshal([]byte(cmd), job)

				if err != nil {
					log.Println("Failed to load scheduled job", key, err)
					continue
				}

				job.ID = key
				sched.cron.AddJob(job.Cron, job)
			}
		} else {
			log.Println(err)
			break
		}

		if cursor == 0 {
			break
		}
	}

	sched.cron.Start()
}
Beispiel #30
0
func main() {
	c, err := redis.Dial("tcp", "10.100.51.125:6379")
	if err != nil {
		fmt.Println(err)
		return
	}
	defer c.Close()

	n, err := redis.StringMap(c.Do("HGETALL", "yes_dsp"))
	if err != nil {
		fmt.Println(err)
		return
	}

	for _, value := range n {
		js, err := json.NewJson([]byte(value))
		if err != nil {
			fmt.Println(err)
			continue
		}
		bidurl, err := js.Get("bidURL").String()
		if err != nil {
			fmt.Println(err)
			continue
		}
		purl, err := url.Parse(bidurl)
		if err != nil {
			fmt.Println(err)
			continue
		}
		prefix := purl.Host[0]
		if prefix == '1' || prefix == '2' || prefix == '3' || prefix == '4' || prefix == '5' || prefix == '6' || prefix == '7' || prefix == '8' || prefix == '9' || prefix == '0' {
			continue
		}
		hosts := strings.Split(purl.Host, ":")
		/*if len(hosts) > 1 {
			fmt.Println(hosts[1])
		}*/
		fmt.Println(hosts[0])
	}
}