Exemple #1
0
func TestReadTimeout(t *testing.T) {
	l, err := net.Listen("tcp", "127.0.0.1:0")
	if err != nil {
		t.Fatalf("net.Listen returned %v", err)
	}
	defer l.Close()

	go func() {
		for {
			c, err := l.Accept()
			if err != nil {
				return
			}
			go func() {
				time.Sleep(time.Second)
				c.Write([]byte("+OK\r\n"))
				c.Close()
			}()
		}
	}()

	// Do

	c1, err := redis.Dial(l.Addr().Network(), l.Addr().String(), redis.DialReadTimeout(time.Millisecond))
	if err != nil {
		t.Fatalf("redis.Dial returned %v", err)
	}
	defer c1.Close()

	_, err = c1.Do("PING")
	if err == nil {
		t.Fatalf("c1.Do() returned nil, expect error")
	}
	if c1.Err() == nil {
		t.Fatalf("c1.Err() = nil, expect error")
	}

	// Send/Flush/Receive

	c2, err := redis.Dial(l.Addr().Network(), l.Addr().String(), redis.DialReadTimeout(time.Millisecond))
	if err != nil {
		t.Fatalf("redis.Dial returned %v", err)
	}
	defer c2.Close()

	c2.Send("PING")
	c2.Flush()
	_, err = c2.Receive()
	if err == nil {
		t.Fatalf("c2.Receive() returned nil, expect error")
	}
	if c2.Err() == nil {
		t.Fatalf("c2.Err() = nil, expect error")
	}
}
Exemple #2
0
// CreatePool creates a redis connection pool
func CreatePool(
	host, password, network string,
	maxConn int,
	idleTimeout, connTimeout time.Duration,
) *rd.Pool {
	return &rd.Pool{
		MaxIdle:     maxConn,
		IdleTimeout: idleTimeout,
		Dial: func() (rd.Conn, error) {
			c, err := rd.Dial(network, host,
				rd.DialConnectTimeout(connTimeout),
				rd.DialReadTimeout(connTimeout),
				rd.DialWriteTimeout(connTimeout))
			if err != nil {
				return nil, err
			}
			if password != "" {
				if _, err := c.Do("AUTH", password); err != nil {
					c.Close()
					return nil, err
				}
			}
			return c, err
		},
	}
}
Exemple #3
0
// Iterate through `machines`, trying to connect to each in turn.
// Returns the first successful connection or the last error encountered.
// Assumes that `machines` is non-empty.
func tryConnect(machines []string, password string) (redis.Conn, error) {
	var err error
	for _, address := range machines {
		var conn redis.Conn
		network := "tcp"
		if _, err = os.Stat(address); err == nil {
			network = "unix"
		}
		log.Debug(fmt.Sprintf("Trying to connect to redis node %s", address))

		dialops := []redis.DialOption{
			redis.DialConnectTimeout(time.Second),
			redis.DialReadTimeout(time.Second),
			redis.DialWriteTimeout(time.Second),
		}

		if password != "" {
			dialops = append(dialops, redis.DialPassword(password))
		}

		conn, err = redis.Dial(network, address, dialops...)

		if err != nil {
			continue
		}
		return conn, nil
	}
	return nil, err
}
Exemple #4
0
func (m MRedis) pub(ch string, msg string) {

	c, err := redis.Dial("tcp", "192.168.176.3:6379", redis.DialReadTimeout(100*time.Second),
		redis.DialWriteTimeout(100*time.Second))
	if err != nil {
		log.Fatal(err)
	}
	defer c.Close()
	log.Printf("ch:%s msg:%s\n", ch, msg)
	ok, err := redis.Int64(c.Do("PUBLISH", ch, msg))
	if err != nil {
		log.Fatal(err)
	}
	fmt.Print(ok)
	/*
		var wg sync.WaitGroup
		wg.Add(2)
		go func()
		 {
			defer wg.Done()
			for {
				switch n := psc.Receive().(type) {
				case redis.Message:
					fmt.Printf("Message: %s %s\n", n.Channel, n.Data)
				case redis.PMessage:
					fmt.Printf("PMessage: %s %s %s\n", n.Pattern, n.Channel, n.Data)
				case redis.Subscription:
					fmt.Printf("Subscription: %s %s %d\n", n.Kind, n.Channel, n.Count)
					if n.Count == 0 {
						return
					}
				case error:
					fmt.Printf("error: %v\n", n)
					return
				}
			}
		}()
		go func() {
			defer wg.Done()

			psc.Subscribe("example")
			psc.PSubscribe("p*")

			// The following function calls publish a message using another
			// connection to the Redis server.
			publish("example", "hello")
			publish("example", "world")
			publish("pexample", "foo")
			publish("pexample", "bar")

			// Unsubscribe from all connections. This will cause the receiving
			// goroutine to exit.
			psc.Unsubscribe()
			psc.PUnsubscribe()
		}()
		wg.Wait()
		return
	*/
}
Exemple #5
0
// ping executes a PING command against addr until timeout occurs.
func (p *Process) ping(addr string, timeout time.Duration) error {
	// Default to local process if addr not specified.
	if addr == "" {
		addr = fmt.Sprintf("localhost:%s", p.Port)
	}

	logger := p.Logger.New("fn", "ping", "addr", addr, "timeout", timeout)
	logger.Info("sending")

	timer := time.NewTimer(timeout)
	defer timer.Stop()

	ticker := time.NewTicker(checkInterval)
	defer ticker.Stop()

	for {
		// Attempt to ping the server.
		if ok := func() bool {
			logger.Info("sending PING")

			conn, err := redis.Dial("tcp", addr,
				redis.DialPassword(p.Password),
				redis.DialConnectTimeout(timeout),
				redis.DialReadTimeout(timeout),
				redis.DialWriteTimeout(timeout),
			)
			if err != nil {
				logger.Error("conn error", "err", err)
				return false
			}
			defer conn.Close()

			if _, err := conn.Do("PING"); err != nil {
				logger.Error("error getting upstream status", "err", err)
				return false
			}

			logger.Info("PONG received")
			return true
		}(); ok {
			return nil
		}

		select {
		case <-timer.C:
			logger.Info("timeout")
			return ErrTimeout
		case <-ticker.C:
		}
	}
}
Exemple #6
0
func (l *RedisInput) Run(output chan common.MapStr) error {
	logp.Debug("redisinput", "Running Redis Input")
	var keysScript = redis.NewScript(1, `return redis.call('KEYS', KEYS[1])`)

	go func() {
		redisURL := fmt.Sprintf("redis://%s:%d/%d", l.Host, l.Port, l.DB)
		dialConnectTimeout := redis.DialConnectTimeout(3 * time.Second)
		dialReadTimeout := redis.DialReadTimeout(10 * time.Second)
		var backOffCount = 0
		var backOffDuration time.Duration = 5 * time.Second
		for {
			logp.Debug("redisinput", "Connecting to: %s", redisURL)
			server, err := redis.DialURL(redisURL, dialConnectTimeout, dialReadTimeout)
			if err != nil {
				logp.Err("couldn't start listening: " + err.Error())
				return
			}
			logp.Debug("redisinput", "Connected to Redis Server")

			reply, err := keysScript.Do(server, "*")
			if err != nil {
				logp.Err("An error occured while executing KEYS command: %s\n", err)
				return
			}

			keys, err := redis.Strings(reply, err)
			if err != nil {
				logp.Err("An error occured while converting reply to String: %s\n", err)
				return
			}

			for _, key := range keys {
				logp.Debug("redisinput", "key is %s", key)
				lineCount, err := l.handleConn(server, output, key)
				if err == nil {
					logp.Debug("redisinput", "Read %v events", lineCount)
					backOffCount = 0
					backOffDuration = time.Duration(backOffCount) * time.Second
					time.Sleep(backOffDuration)
				} else {
					backOffCount++
					backOffDuration = time.Duration(backOffCount) * time.Second
					time.Sleep(backOffDuration)
				}
			}
			defer server.Close()
		}
	}()
	return nil
}
Exemple #7
0
func init() {

	//	c, err := redis.Dial("tcp", cfg.RedisAddr(),
	//		redis.DialReadTimeout(1 * time.Second), redis.DialWriteTimeout(1 * time.Second))
	c, err := redis.Dial("tcp", fmt.Sprintf(":%d", 6379),
		redis.DialReadTimeout(1*time.Second), redis.DialWriteTimeout(1*time.Second))
	redisConn = c
	if err != nil {
		fmt.Print(err)
	}
	s, _ := redisConn.Do("SELECT", 11)
	fmt.Println("select 11, s=", s)

}
Exemple #8
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
				}
			}
		}

	}

}
Exemple #9
0
func FlushCfgToDB() {
	c, err := redis.Dial("tcp", RedisAddr(), redis.DialReadTimeout(1*time.Second), redis.DialWriteTimeout(1*time.Second))
	if err != nil {
		fmt.Println(err)
		return
	}
	defer c.Close()

	c.Do("SELECT", RedisDBs[Cfg])
	c.Do("FLUSHALL")
	c.Do("SET", "srv:port:agent", AgentPort)
	c.Do("SET", "srv:port:lobby", LobbyPort)
	c.Do("SET", "srv:port:game", GamePort)
	c.Do("SET", "srv:port:data", DaoPort)

	fmt.Println("FlushCfgToDB")
}
Exemple #10
0
// RedisInfo executes an INFO command against a Redis server and returns the results.
func (p *Process) RedisInfo(addr string, timeout time.Duration) (*RedisInfo, error) {
	// Default to local process if addr not specified.
	if addr == "" {
		addr = fmt.Sprintf("localhost:%s", p.Port)
	}

	logger := p.Logger.New("fn", "replInfo", "addr", addr)
	logger.Info("sending INFO")

	// Connect to the redis server.
	conn, err := redis.Dial("tcp", addr,
		redis.DialPassword(p.Password),
		redis.DialConnectTimeout(timeout),
		redis.DialReadTimeout(timeout),
		redis.DialWriteTimeout(timeout),
	)
	if err != nil {
		logger.Info("dial error", "err", err)
		return nil, err
	}
	defer conn.Close()

	// Execute INFO command.
	reply, err := conn.Do("INFO")
	if err != nil {
		logger.Error("info error", "err", err)
		return nil, err
	}

	buf, ok := reply.([]byte)
	if !ok {
		logger.Error("info reply type error", "type", fmt.Sprintf("%T", buf))
		return nil, fmt.Errorf("unexpected INFO reply format: %T", buf)
	}

	// Parse the bulk string reply info a typed object.
	info, err := ParseRedisInfo(string(buf))
	if err != nil {
		logger.Error("parse info error", "err", err)
		return nil, fmt.Errorf("parse info: %s", err)
	}

	logger.Info("INFO received")
	return info, nil
}
Exemple #11
0
// Callback function factory to vitess pool
func redisFactory(key string, config map[string]string) (redis.Conn, error) {
	host := config["host"]
	port := config["port"]
	redisString := fmt.Sprintf("%s:%s", host, port)
	cli, err := redis.Dial("tcp", redisString, redis.DialReadTimeout(time.Second), redis.DialWriteTimeout(time.Second))
	// select default db if not specified
	db, ok := config["db"]
	if ok {
		cli.Do("SELECT", db)
	} else {
		cli.Do("SELECT", 0)
	}
	if err != nil {
		// Write exit
		fmt.Println("Error in Redis Dial")
	}
	return cli, nil
}
Exemple #12
0
func NewUser(p *Models) *User {
	u := new(User)
	c, err := redis.Dial("tcp", cfg.RedisAddr(),
		redis.DialReadTimeout(1*time.Second), redis.DialWriteTimeout(1*time.Second))
	if err != nil {
		log.Error(err.Error())
		return nil
	}
	u.c = c

	//select db
	s, err := c.Do("SELECT", cfg.RedisDBs[cfg.Pf])
	if err != nil {
		log.Error(err.Error())
		return nil
	}
	//temp
	fw.PrintType(s, "s")

	//fill keys
	b, _ := redis.Bool(c.Do("EXISTS", k_account_count))

	//	switch b.(type) {
	//	case interface{}:
	//		log..Debug("interface")
	//	case []byte:
	//		log..Debug("byte")
	//	case string:
	//		log..Debug("string")
	//	case *int:
	//		log..Debug("int")
	//	default:
	//		log..Debug("other")
	//	}

	if b == false {
		c.Do("SET", k_account_count, 0)
		log.Info("fill key:", k_account_count)
	}

	//register model
	u.parent = p
	return u
}
Exemple #13
0
// Connect establishes a connection to Redis which provides the
// pub/sub implementation.
func (b *redisBroker) Connect() error {
	if b.pool != nil {
		return nil
	}

	var addr string

	if len(b.opts.Addrs) == 0 || b.opts.Addrs[0] == "" {
		addr = "redis://127.0.0.1:6379"
	} else {
		addr = b.opts.Addrs[0]

		if !strings.HasPrefix("redis://", addr) {
			addr = "redis://" + addr
		}
	}

	b.addr = addr

	b.pool = &redis.Pool{
		MaxIdle:     b.bopts.maxIdle,
		MaxActive:   b.bopts.maxActive,
		IdleTimeout: b.bopts.idleTimeout,
		Dial: func() (redis.Conn, error) {
			return redis.DialURL(
				b.addr,
				redis.DialConnectTimeout(b.bopts.connectTimeout),
				redis.DialReadTimeout(b.bopts.readTimeout),
				redis.DialWriteTimeout(b.bopts.writeTimeout),
			)
		},
		TestOnBorrow: func(c redis.Conn, t time.Time) error {
			_, err := c.Do("PING")
			return err
		},
	}

	return nil
}
Exemple #14
0
func main() {
	redisConn, _ = redis.Dial("tcp", cfg.RedisAddr(),
		redis.DialReadTimeout(1*time.Second), redis.DialWriteTimeout(1*time.Second))
	s, _ := redisConn.Do("SELECT", 11)
	fmt.Println("select 11, s=", s)

	defer redisConn.Close()

	N := 10000
	//	acc := make(chan int, 0)
	//	do := 0
	//	mu := sync.Mutex{}
	//
	t1 := time.Now()

	for i := 0; i < N; i++ {
		//		go func() {
		//			mu.Lock()
		//			do = do + 1
		////			_, e := redisConn.Do("SET", do, "jjj")
		_, e := redisConn.Do("PING")
		if e != nil {
			fmt.Print(e, "\n")
		}
		//			mu.Unlock()
		//			acc <- 1
		//		}()
	}
	//	for i := 0; i < N; i++ {
	//		<-acc
	//	}

	t2 := time.Now()
	d := t2.Sub(t1)

	//	time.Sleep(3 * time.Second)
	//	log.Infof("BenchmarkGo, %+v times in %+v", do, d)
	log.Infof("BenchmarkGo, %+v times in %+v", N, d)
}
Exemple #15
0
func NewGame(p *Models) *Game {
	g := new(Game)
	c, err := redis.Dial("tcp", cfg.RedisAddr(),
		redis.DialReadTimeout(1*time.Second), redis.DialWriteTimeout(1*time.Second))
	if err != nil {
		log.Error(err.Error())
		return nil
	}
	g.c = c

	//select db
	_, err = c.Do("SELECT", cfg.RedisDBs[cfg.Game])
	if err != nil {
		log.Error(err.Error())
		return nil
	}

	//register model
	g.parent = p

	return g
}
//create a redis connection pool
func newPoolRedis(server, password string, maxIdle int, maxActive int, readTimeout int) *redis.Pool {

	return &redis.Pool{
		MaxIdle:     maxIdle,
		IdleTimeout: 240 * time.Second,
		MaxActive:   maxActive,
		Wait:        false,
		Dial: func() (retConn redis.Conn, retErr error) {

			defer func() { //assure for not panicking
				if r := recover(); r != nil {
					log.Error("Error open redis conn!!  %v", r)
					log.Error("Retuning error")

					retConn = nil
					retErr = errors.New("Error trying to open redis conn!!")

					return
				}
			}()

			c, err := redis.Dial("tcp", server, redis.DialReadTimeout(time.Duration(readTimeout)*time.Millisecond))
			if err != nil {
				log.Error("Erro ao tentar se conectar ao redis! ", err)
				return nil, err
			}

			return c, err
		},

		TestOnBorrow: func(c redis.Conn, t time.Time) error {
			_, err := c.Do("PING")
			return err
		},
	}
}
Exemple #17
0
// updateMaster gets the address of the master node from sentinel
func updateMaster() error {
	config.Log.Debug("Contacting sentinel for address of master...")

	// connect to sentinel in order to query for the master address
	r, err := redis.DialURL("redis://"+config.SentinelAddress, redis.DialConnectTimeout(config.TimeoutNotReady), redis.DialReadTimeout(config.TimeoutSentinelPoll), redis.DialPassword(config.SentinelPassword))
	if err != nil {
		return fmt.Errorf("Failed to reach sentinel - %v", err)
	}

	// retrieve the master redis address
	addr, err := redis.Strings(r.Do("SENTINEL", "get-master-addr-by-name", config.MonitorName))
	if err != nil {
		return fmt.Errorf("Failed to get-master-addr-by-name - %v", err)
	}

	// cleanup after ourselves
	r.Close()

	// construct a useable address from sentinel's response
	masterAddr = fmt.Sprintf("%v:%v", addr[0], addr[1])
	config.Log.Debug("Master address: '%v'", masterAddr)

	// wait for redis to transition to master
	if err = verifyMaster(masterAddr, config.SentinelPassword); err != nil {
		return fmt.Errorf("Could not verify master - %v", err)
	}

	return nil
}