Пример #1
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
}
Пример #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
		},
	}
}
Пример #3
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
	*/
}
Пример #4
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:
		}
	}
}
Пример #5
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)

}
Пример #6
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")
}
Пример #7
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
}
Пример #8
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
}
Пример #9
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
}
Пример #10
0
// creates a redis connection pool to use
func (r Redis) newPool(server, password string) *redis.Pool {
	return &redis.Pool{
		MaxIdle:     3,
		IdleTimeout: 5 * time.Second,
		Dial: func() (redis.Conn, error) {
			c, err := redis.DialURL(server, redis.DialConnectTimeout(30*time.Second),
				redis.DialWriteTimeout(10*time.Second), redis.DialPassword(password))

			if err != nil {
				return nil, fmt.Errorf("Failed to reach redis - %v", err)
			}
			return c, err
		},
		TestOnBorrow: func(c redis.Conn, t time.Time) error {
			_, err := c.Do("PING")
			return err
		},
	}
}
Пример #11
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
}
Пример #12
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)
}
Пример #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
}