Пример #1
0
// OpenConnection opens and returns a new connection
func OpenConnection(tag, network, address string, db int) *redisConnection {
	redisClient := redis.NewClient(&redis.Options{
		Network: network,
		Addr:    address,
		DB:      int64(db),
	})

	name := fmt.Sprintf("%s-%s", tag, uniuri.NewLen(6))

	connection := &redisConnection{
		Name:         name,
		heartbeatKey: strings.Replace(connectionHeartbeatTemplate, phConnection, name, 1),
		queuesKey:    strings.Replace(connectionQueuesTemplate, phConnection, name, 1),
		redisClient:  redisClient,
	}

	if !connection.updateHeartbeat() { // checks the connection
		log.Panicf("rmq connection failed to update heartbeat %s", connection)
	}

	// add to connection set after setting heartbeat to avoid race with cleaner
	redisErrIsNil(redisClient.SAdd(connectionsKey, name))

	go connection.heartbeat()
	// log.Printf("rmq connection connected to %s %s:%s %d", name, network, address, db)
	return connection
}
Пример #2
0
func NewRedisIOWriter(options logrus_mate.Options) (writer io.Writer, err error) {
	conf := RedisIOConfig{}

	if err = options.ToObject(&conf); err != nil {
		return
	}

	if conf.ListName == "" {
		err = errors.New("logurs mate: redisio's list name is empty")
		return
	}

	if conf.Network == "" {
		conf.Network = "tcp"
	}

	if conf.Address == "" {
		conf.Address = "127.0.0.1:6379"
	}

	var redisCli *redis.Client

	redisOpt := &redis.Options{
		Network:  conf.Network,
		Addr:     conf.Address,
		Password: conf.Password,
		DB:       conf.Db,
	}

	redisCli = redis.NewClient(redisOpt)

	writer, err = redisio.NewWriter(redisCli, conf.ListName)
	return
}
Пример #3
0
// OpenConnection opens and returns a new connection
func OpenConnection(tag, network, address string, db int) *redisConnection {
	redisClient := redis.NewClient(&redis.Options{
		Network: network,
		Addr:    address,
		DB:      int64(db),
	})
	return OpenConnectionWithRedisClient(tag, redisClient)
}