Ejemplo n.º 1
0
func OnGetCurrentMaxTime(_tenant, _company int, _window, _parameter1, _parameter2 string, resultChannel chan int) {
	defer func() {
		if r := recover(); r != nil {
			fmt.Println("Recovered in OnGetCurrentMaxTime", r)
		}
	}()
	client, err := redis.DialTimeout("tcp", redisIp, time.Duration(10)*time.Second)
	errHndlr(err)
	defer client.Close()
	//authServer
	authE := client.Cmd("auth", redisPassword)
	errHndlr(authE.Err)
	// select database
	r := client.Cmd("select", redisDb)
	errHndlr(r.Err)

	maxtimeSearch := fmt.Sprintf("SESSION:%d:%d:%s:*:%s:%s", _tenant, _company, _window, _parameter1, _parameter2)
	keyList, _ := client.Cmd("keys", maxtimeSearch).List()
	if len(keyList) > 0 {
		tempMaxTime := 0
		tm := time.Now()
		for _, key := range keyList {
			tmx, _ := client.Cmd("hget", key, "time").Str()
			tm2, _ := time.Parse(layout, tmx)
			timeDiff := int(tm.Local().Sub(tm2.Local()).Seconds())
			if tempMaxTime < timeDiff {
				tempMaxTime = timeDiff
			}
		}
		resultChannel <- tempMaxTime

	} else {
		resultChannel <- 0
	}
}
Ejemplo n.º 2
0
func GetQueueName(queueId string) string {
	defer func() {
		if r := recover(); r != nil {
			fmt.Println("Recovered in GetQueueName", r)
		}
	}()
	client, err := redis.DialTimeout("tcp", redisIp, time.Duration(10)*time.Second)
	errHndlr(err)
	defer client.Close()
	//authServer
	authE := client.Cmd("auth", redisPassword)
	errHndlr(authE.Err)
	// select database
	r := client.Cmd("select", ardsRedisDb)
	errHndlr(r.Err)

	qId := strings.Replace(queueId, "-", ":", -1)
	queueName, _ := client.Cmd("hget", "QueueNameHash", qId).Str()
	fmt.Println("queueName: ", queueName)
	if queueName == "" {
		return queueId
	} else {
		return queueName
	}
}
Ejemplo n.º 3
0
func OnGetMaxTime(_tenant, _company int, _window, _parameter1, _parameter2 string, resultChannel chan int) {
	defer func() {
		if r := recover(); r != nil {
			fmt.Println("Recovered in OnGetMaxTime", r)
		}
	}()
	client, err := redis.DialTimeout("tcp", redisIp, time.Duration(10)*time.Second)
	errHndlr(err)
	defer client.Close()
	//authServer
	authE := client.Cmd("auth", redisPassword)
	errHndlr(authE.Err)
	// select database
	r := client.Cmd("select", redisDb)
	errHndlr(r.Err)

	maxtimeSearch := fmt.Sprintf("MAXTIME:%d:%d:%s:%s:%s", _tenant, _company, _window, _parameter1, _parameter2)
	keyList, _ := client.Cmd("keys", maxtimeSearch).List()
	if len(keyList) > 0 {
		tempMaxTime := 0
		for _, key := range keyList {
			value, _ := client.Cmd("get", key).Int()
			if tempMaxTime < value {
				tempMaxTime = value
			}
		}
		resultChannel <- tempMaxTime

	} else {
		resultChannel <- 0
	}
}
Ejemplo n.º 4
0
func (self *OutputConfig) initRedisClient() (err error) {
	var (
		client *redis.Client
	)

	self.closeRedisClient()

	for _, addr := range self.Host {
		if client, err = redis.DialTimeout("tcp", addr, time.Duration(self.Timeout)*time.Second); err == nil {
			self.clients = append(self.clients, client)
		} else {
			log.Warnf("Redis connection failed: %q\n%s", addr, err)
		}
	}

	if len(self.clients) > 0 {
		self.client = self.clients[0]
		err = nil
	} else {
		self.client = nil
		err = errors.New("no valid redis server connection")
	}

	return
}
Ejemplo n.º 5
0
func NewCache(cacheUrl string, id string, ttl time.Duration) (*Cache, error) {
	u, err := url.Parse(cacheUrl)
	if err != nil {
		return nil, err
	}
	redisConn, err := redis.DialTimeout("tcp", u.Host,
		time.Duration(30)*time.Second)
	if err != nil {
		return nil, err
	}
	if len(u.Path) > 2 {
		db, err := strconv.Atoi(u.Path[1:])
		if err == nil {
			return nil, fmt.Errorf("Wrong Redis db: %s", err)
		}
		r := redisConn.Cmd("select", db)
		if r.Err != nil {
			return nil, r.Err
		}
	}
	if u.User != nil {
		if pwd, ok := u.User.Password(); ok {
			r := redisConn.Cmd("auth", pwd)
			if r.Err != nil {
				return nil, r.Err
			}
		}
	}
	cache := &Cache{redisConn, id, ttl}
	cache.publishEvent("new_host", id)
	return cache, nil
}
// Redis String Methods
func RedisAdd(key, value string) string {
	defer func() {
		if r := recover(); r != nil {
			fmt.Println("Recovered in RedisSet", r)
		}
	}()
	client, err := redis.DialTimeout("tcp", redisIp, time.Duration(10)*time.Second)
	errHndlr(err)
	defer client.Close()

	// select database
	r := client.Cmd("select", redisDb)
	errHndlr(r.Err)

	isExists, _ := client.Cmd("EXISTS", key).Bool()

	if isExists {
		return "Key Already exists"
	} else {
		result, sErr := client.Cmd("set", key, value).Str()
		errHndlr(sErr)
		fmt.Println(result)
		return result
	}
}
Ejemplo n.º 7
0
func (s *StateStore) connect() error {
	client, err := redis.DialTimeout(
		"tcp",
		s.config.Address,
		s.config.Timeout*time.Second,
	)
	if err != nil {
		return err
	}

	// check if passowrd given and authenticate accordingly
	if len(s.config.Password) > 0 {
		r := client.Cmd("AUTH", s.config.Password)
		if r.Err != nil {
			return r.Err
		}
	}

	// select the database
	r := client.Cmd("SELECT", s.config.Database)
	if r.Err != nil {
		return r.Err
	}

	// set client
	s.client = client
	return nil
}
Ejemplo n.º 8
0
func OnGetCurrentCount(_tenant, _company int, _window, _parameter1, _parameter2 string, resultChannel chan int) {
	defer func() {
		if r := recover(); r != nil {
			fmt.Println("Recovered in OnGetCurrentCount", r)
		}
	}()
	client, err := redis.DialTimeout("tcp", redisIp, time.Duration(10)*time.Second)
	errHndlr(err)
	defer client.Close()
	//authServer
	authE := client.Cmd("auth", redisPassword)
	errHndlr(authE.Err)
	// select database
	r := client.Cmd("select", redisDb)
	errHndlr(r.Err)

	concurrentSearch := fmt.Sprintf("CONCURRENT:%d:%d:%s:%s:%s", _tenant, _company, _window, _parameter1, _parameter2)
	keyList, _ := client.Cmd("keys", concurrentSearch).List()
	if len(keyList) > 0 {
		temptotal := 0
		for _, key := range keyList {
			value, _ := client.Cmd("get", key).Int()
			temptotal = temptotal + value
		}
		if temptotal < 0 {
			resultChannel <- 0
		} else {
			resultChannel <- temptotal
		}

	} else {
		resultChannel <- 0
	}
}
Ejemplo n.º 9
0
func connect() *redis.Client {
	c, err := redis.DialTimeout("tcp", os.Getenv("REDIS_PORT_6379_TCP_ADDR")+":"+os.Getenv("REDIS_PORT_6379_TCP_PORT"), time.Duration(10)*time.Second)

	errHndlr(err)

	return c
}
Ejemplo n.º 10
0
func getClient() (*redis.Client, error) {
	c, err := redis.DialTimeout("tcp", "127.0.0.1:6379", time.Duration(10)*time.Second)
	if err != nil {
		return c, err
	}

	c.Cmd("select", 0)
	return c, nil
}
Ejemplo n.º 11
0
// ********************   Worker Pool information
func worker(id int, jobs <-chan Redisbox, results chan<- string) {
	for j := range jobs {
		c, err := redis.DialTimeout("tcp", fmt.Sprintf("%s:%d", j.Hostname, j.Port), time.Duration(3)*time.Second)
		check(err)
		c.Cmd("PING")
		results <- "OK"
		c.Close()
	}
}
Ejemplo n.º 12
0
func getRedisConnection() *redis.Client {

	// Finally, load the redis instance
	rc, redisErr := redis.DialTimeout("tcp", state.Config["redisIp"]+":"+state.Config["redisPort"], time.Duration(2)*time.Second)
	redisErrHandler(redisErr, "["+tools.DateStampAsString()+"] 1 - tcp connect")
	// Select the desired DB
	r := rc.Cmd("select", state.Config["redisDb"])
	redisErrHandler(r.Err, "["+tools.DateStampAsString()+"] Redis op error: select "+state.Config["redisDb"])
	return rc
}
Ejemplo n.º 13
0
func makeClient(address string, password string, db int64) *redis.Client {
	client, err := redis.DialTimeout("tcp", address, time.Duration(10)*time.Second)
	errHndlr(err)
	if password != "" {
		r := client.Cmd("AUTH", password)
		errHndlr(r.Err)
	}
	r := client.Cmd("SELECT", db)
	errHndlr(r.Err)
	return client
}
Ejemplo n.º 14
0
func worker(id int, jobs <-chan string, results chan<- string, hostame string, port int, database int, dst_database int) {
	c, err := redis.DialTimeout("tcp", fmt.Sprintf("%s:%d", hostname, port), time.Duration(3)*time.Second)
	errHndlr(err)
	for j := range jobs {
		errHndlr(err)
		k := c.Cmd("SELECT", database)
		k = c.Cmd("MOVE", j, dst_database)
		results <- k.String()
	}
	c.Close()
}
Ejemplo n.º 15
0
func DbConnect(addr *string) error {
	var e error
	log.Println("Connecting to DB...")
	myDbClient, e = redis.DialTimeout("tcp", *addr, time.Duration(10)*time.Second)
	if e == nil {
		log.Println("db connection successful.")
	} else {
		log.Println("db connect error: ", e)
	}
	return e
}
Ejemplo n.º 16
0
func connect(host string, port int, timeout int) *redis.Client {
	connectionString := fmt.Sprintf("%s:%d", host, port)
	conn, err := redis.DialTimeout("tcp", connectionString, time.Duration(timeout)*time.Second)

	if err != nil {
		fmt.Println("Cannot connect to Redis, exiting.")
		os.Exit(int(syscall.ECONNREFUSED))
	}

	return conn
}
Ejemplo n.º 17
0
func fetchAllKeys(hostame string, port int, database int) []string {
	c, err := redis.DialTimeout("tcp", fmt.Sprintf("%s:%d", hostname, port), time.Duration(3)*time.Second)
	errHndlr(err)
	keys := c.Cmd("SELECT", database)
	keys = c.Cmd("KEYS", "*")
	//fmt.Println("type:", reflect.TypeOf(keys))
	j := keys.Elems
	redis_keys := make([]string, len(j), len(j))
	for i := 0; i < len(j); i++ {
		redis_keys[i] = fmt.Sprintf("%s", j[i])
	}
	return redis_keys
}
Ejemplo n.º 18
0
Archivo: radix.go Proyecto: rif/gocmd
func main() {
	c, err := redis.DialTimeout("tcp", "192.168.0.17:6379", 5*time.Second)
	if err != nil {
		log.Fatal("Cannot connect to redis: ", err)
	}
	c.Cmd("select", 11)
	defer c.Close()

	err = someFunc(c)
	log.Print("after: ", err, err == nil)
	r := c.Cmd("get", "mykey")
	log.Print(r.Str())
}
Ejemplo n.º 19
0
func (db *DB) Connect() (*redis.Client, error) {
	// NOTE: On a low level this uses net.Dial, see:
	//		 http://golang.org/pkg/net/#Dial for details on network & addr
	conn, err := redis.DialTimeout(db.Network, db.Address, time.Duration(10)*time.Second)
	if err != nil {
		return conn, err
	}
	result := conn.Cmd("select", db.Database)
	if result.Err != nil {
		return conn, err
	}
	return conn, err
}
Ejemplo n.º 20
0
func connectRedis(m redisSetting) (*redis.Client, error) {
	network := "tcp"
	target := fmt.Sprintf("%s:%s", m.Host, m.Port)
	if m.Socket != "" {
		target = m.Socket
		network = "unix"
	}
	c, err := redis.DialTimeout(network, target, time.Duration(m.Timeout)*time.Second)
	if err != nil {
		return nil, fmt.Errorf("couldn't connect: %s", err)
	}
	return c, nil
}
func fetchAllKeys(hostame string, port int, database int, prefix string) []string {
	c, err := redis.DialTimeout("tcp", fmt.Sprintf("%s:%d", hostname, port), time.Duration(3000)*time.Second)
	errHndlr(err)
	keys := c.Cmd("KEYS", fmt.Sprintf("%s*", prefix))
	fmt.Println("got keys")
	j := keys.Elems
	fmt.Println("keys count ", len(j))
	redis_keys := make([]string, len(j), len(j))
	for i := 0; i < len(j); i++ {
		redis_keys[i] = fmt.Sprintf("%s", j[i])
	}
	return redis_keys
}
func main() {
	c, err := redis.DialTimeout("tcp", "127.0.0.1:6379", time.Duration(10)*time.Second)
	if err != nil {
		panic(fmt.Sprintln("error:", err))
	}
	defer c.Close()

	for i := 0; i < N; i++ {
		c.Append("set", "foo", "bar")
	}
	for i := 0; i < N; i++ {
		c.GetReply()
	}
	fmt.Println("Done")
}
func worker(id int, jobs <-chan string, results chan<- string, hostame string, port int, database int) {
	c, err := redis.DialTimeout("tcp", fmt.Sprintf("%s:%d", hostname, port), time.Duration(3)*time.Second)
	errHndlr(err)
	for j := range jobs {
		errHndlr(err)
		k := c.Cmd("SELECT", database)
		k = c.Cmd("DEL", j)
		fmt.Println("deleting ", j)
		if 3 < 2 {
			fmt.Println(k.Elems[0].Str())
		}
		results <- "OK"
	}
	c.Close()
}
Ejemplo n.º 24
0
func worker(id int, jobs <-chan int, results chan<- int, hostame string, port int) {
	c, err := redis.DialTimeout("tcp", fmt.Sprintf("%s:%d", hostname, port), time.Duration(3)*time.Second)
	for j := range jobs {
		errHndlr(err)
		// r := c.Cmd("select", 1) < - this will not work with nutcracker since it only allows you to set 1 DB
		s := randomString(20)
		now := time.Now()
		r := c.Cmd("set", s, s)
		errHndlr(r.Err)
		r = c.Cmd("get", s)
		later := time.Now()
		fmt.Println("request:", j, "time:", later.Sub(now))
		results <- 1
	}
	c.Close()
}
Ejemplo n.º 25
0
func worker(id int, jobs <-chan string, results chan<- string, hostame string, port int, database int) {
	re := regexp.MustCompile("serializedlength:([0-9]+)")
	c, err := redis.DialTimeout("tcp", fmt.Sprintf("%s:%d", hostname, port), time.Duration(3)*time.Second)
	errHndlr(err)
	for j := range jobs {
		errHndlr(err)
		k := c.Cmd("SELECT", database)
		k = c.Cmd("DEBUG", "OBJECT", j)
		match := re.FindStringSubmatch(fmt.Sprintf("%s", k))
		if len(match) != 0 {
			fmt.Printf("%s:%s:%d:%d:%s\n", match[1], hostname, port, database, j)
		}
		results <- "OK"
	}
	c.Close()
}
Ejemplo n.º 26
0
func RedisListLlen(lname string) int {
	defer func() {
		if r := recover(); r != nil {
			fmt.Println("Recovered in RedisListLlen", r)
		}
	}()
	client, err := redis.DialTimeout("tcp", redisIp, time.Duration(10)*time.Second)
	errHndlr(err)
	defer client.Close()

	// select database
	r := client.Cmd("select", redisDb)
	errHndlr(r.Err)

	result, _ := client.Cmd("llen", lname).Int()
	return result
}
Ejemplo n.º 27
0
func RedisZRemove(lname, value string) bool {
	defer func() {
		if r := recover(); r != nil {
			fmt.Println("Recovered in RedisZRemove", r)
		}
	}()
	client, err := redis.DialTimeout("tcp", redisIp, time.Duration(10)*time.Second)
	errHndlr(err)
	defer client.Close()

	// select database
	r := client.Cmd("select", redisDb)
	errHndlr(r.Err)

	result, _ := client.Cmd("zrem", lname, value).Bool()
	return result
}
Ejemplo n.º 28
0
func RedisSearchKeys(pattern string) []string {
	defer func() {
		if r := recover(); r != nil {
			fmt.Println("Recovered in RedisSearchKeys", r)
		}
	}()
	client, err := redis.DialTimeout("tcp", redisIp, time.Duration(10)*time.Second)
	errHndlr(err)
	defer client.Close()

	// select database
	r := client.Cmd("select", redisDb)
	errHndlr(r.Err)

	strObj, _ := client.Cmd("keys", pattern).List()
	return strObj
}
Ejemplo n.º 29
0
func (self *RedisPool) getConn(addr string) (*RedisEntry, error) {
	if c, ok := self.rds[addr]; ok {
		return c, nil

	} else {
		log.Printf("add conn addr:%s\n", addr)
		c, err := redis.DialTimeout("tcp", addr, time.Duration(10)*time.Second)
		if err != nil {
			return nil, err
		}

		en := &RedisEntry{c, make(chan bool, 1)}
		self.rds[addr] = en
		return en, nil
	}

}
Ejemplo n.º 30
0
func main() {
	m := martini.Classic()

	c, err := redis.DialTimeout("tcp", "127.0.0.1:6379", time.Duration(10)*time.Second)
	errHndlr(err)
	defer c.Close()

	m.Group("/listings", func(r martini.Router) {
		r.Get("", GetListingsIndex)
		r.Get("/:id", GetListing)
		r.Post("/new", NewListing)
		r.Put("/update/:id", UpdateListing)
		r.Delete("/delete/:id", DeleteListing)
	})

	m.Run()
}