Exemplo n.º 1
0
Arquivo: server.go Projeto: Ell/bot
func NewServer(configPath string) (*Server, error) {
	config, err := LoadConfig(configPath)

	if err != nil {
		panic(err)
	}

	rClient := redis.NewClient(&redis.Options{
		Addr:     "localhost:6379",
		Password: "",
		DB:       0,
	})

	pubsub, err := rClient.Subscribe(config.Irc.Server)
	if err != nil {
		panic(err)
	}

	server := Server{
		Config: config,
		Data:   make(chan *irc.Message),
		Redis:  rClient,
		Pubsub: pubsub,
		tries:  0,
	}

	return &server, nil
}
Exemplo n.º 2
0
func NewRedisWriter(options ...func(adaptor.Processor)) (*RedisWriter, error) {
	w := &RedisWriter{
		lineQ: make(chan string, 1),
		sig:   make(chan struct{}),
		wg:    &sync.WaitGroup{},
		host:  "127.0.0.1:23456",
		pw:    "",
		db:    0,
	}

	for _, option := range options {
		option(w)
	}

	client := redis.NewClient(&redis.Options{
		Addr:     w.host,
		Password: w.pw,
		DB:       w.db,
	})

	err := client.Ping().Err()
	if err != nil {
		return nil, err
	}

	w.client = client

	return w, nil
}
Exemplo n.º 3
0
func NewWorker(cfg *config.WorkerConfig) (*Worker, error) {
	var err error
	w := new(Worker)
	w.cfg = cfg

	vec := strings.SplitN(cfg.RedisAddr, "/", 2)
	if len(vec) == 2 {
		w.redisAddr = vec[0]
		w.redisDB, err = strconv.Atoi(vec[1])
		if err != nil {
			return nil, err
		}
	} else {
		w.redisAddr = vec[0]
		w.redisDB = config.DefaultRedisDB
	}

	w.redisClient = redis.NewClient(
		&redis.Options{
			Addr:     w.redisAddr,
			Password: "", // no password set
			DB:       int64(w.redisDB),
		},
	)
	_, err = w.redisClient.Ping().Result()
	if err != nil {
		golog.Error("worker", "NewWorker", "ping redis fail", 0, "err", err.Error())
		return nil, err
	}

	return w, nil
}
Exemplo n.º 4
0
func init() {
	rd = redis.NewClient(&redis.Options{
		Addr: "localhost:6379",
		DB:   0,
	})
	flag.Parse()
}
Exemplo n.º 5
0
Arquivo: main.go Projeto: Ell/bot
func newLogger() (*Logger, error) {
	client := redis.NewClient(&redis.Options{
		Addr:     "localhost:6379",
		Password: "",
		DB:       0,
	})

	pubsub, err := client.Subscribe("messages")
	session, err := rdb.Connect(rdb.ConnectOpts{
		Address: "localhost:28015",
	})

	if err != nil {
		panic(err)
	}

	logger := &Logger{
		Redis:     client,
		Pubsub:    pubsub,
		DbSession: session,
		messages:  make(chan string),
	}

	go logger.handleMessages()

	return logger, err
}
Exemplo n.º 6
0
func NewRedisClient(config *Config) (*RedisClient, error) {
	var client *redis.Client

	if config.RedisSentinel {
		client = redis.NewFailoverClient(&redis.FailoverOptions{
			MasterName:    config.RedisMasterName,
			SentinelAddrs: []string{config.RedisAddr},
			Password:      config.RedisPassword,
			DB:            config.RedisDB,
		})
	} else {
		client = redis.NewClient(&redis.Options{
			Addr:     config.RedisAddr,
			Password: config.RedisPassword,
			DB:       config.RedisDB,
		})
	}
	ps, err := client.Subscribe(channelName)

	if err != nil {
		return nil, err
	}

	rc := &RedisClient{
		client: client,
		pubsub: ps,
	}
	go rc.Receive()
	return rc, nil
}
Exemplo n.º 7
0
Arquivo: kvarg.go Projeto: SudoQ/kvarg
func init() {
	addr := os.Getenv("DB_PORT_6379_TCP_ADDR")
	port := os.Getenv("DB_PORT_6379_TCP_PORT")
	if addr == "" {
		addr = "localhost"
	}
	if port == "" {
		port = "6379"
	}
	addrPort := fmt.Sprintf("%s:%s", addr, port)

	redisClient = redis.NewClient(&redis.Options{
		Addr:     addrPort,
		Password: "", // no password set
		DB:       0,  // use default DB
	})

	var err error
	tries := 0.0
	maxTries := 5.0
	for tries < maxTries {
		err = redisClient.Ping().Err()
		if err != nil {
			fmt.Println(err)
			tries += 1
			time.Sleep(time.Second * time.Duration(math.Pow(2.0, tries)))
			continue
		}
		return // Success
	}
	os.Exit(1) // Failure
}
Exemplo n.º 8
0
func worker() {
	var wg sync.WaitGroup
	wg.Add(3)

	redisClient = &RedisClientWrap{redis.NewClient(&redis.Options{
		Addr:     fmt.Sprintf("%s:%d", cfg.Redis.Host, cfg.Redis.Port),
		Password: "", // no password set
		DB:       0,  // use default DB
	})}

	if *test != "" {
		bot.SetTest(*test)
	}
	anzu := bot.NewAnzu(cfg.Bot["Anzu"].Token, &stop, redisClient)
	meu := bot.NewMeu(cfg.Bot["Meu"].Token, &stop, redisClient)

	go bot.StartBot(anzu, &wg)
	go bot.StartBot(meu, &wg)

	server := command.NewServer(cfg.Commands, cfg.Command)

	go server.Start(&wg)

	wg.Wait()

	done <- struct{}{}
}
Exemplo n.º 9
0
func init() {
	rd = redis.NewClient(&redis.Options{
		Addr: "localhost:6379",
		DB:   0,
	})

	mu = new(sync.Mutex)
}
Exemplo n.º 10
0
func initRedisClient() *redis.Client {
	redisUrl, _ := Config.String(ENV, "redis-url")
	return redis.NewClient(&redis.Options{
		Addr:     redisUrl,
		Password: "",
		DB:       0,
	})
}
Exemplo n.º 11
0
func NewMonitor(redisAddr string) *Monitor {
	return (&Monitor{
		redisClient: redis.NewClient(&redis.Options{
			Addr:     redisAddr,
			Password: "", // no password set
			DB:       0,  // use default DB
		}),
		statisticMap: make(map[string]int64),
		stopChan:     make(chan error),
		stopedNotify: make(chan error),
	}).start()
}
Exemplo n.º 12
0
func GetRedisClient() *redis.Client {
	if rdx != nil {
		return rdx
	}

	rdx = redis.NewClient(&redis.Options{
		Addr:     REDIS_ADDR,
		Password: REDIS_PASSWORD,
		DB:       0,
	})
	return rdx
}
Exemplo n.º 13
0
func newRedisClient(addr string, auth string, poolSize int) (cli *redisClient) {
	cli = &redisClient{
		poolSize: poolSize,
		addr:     addr,
		conns: redis.NewClient(&redis.Options{
			Addr:         addr,
			Password:     auth,
			ReadTimeout:  DEFAULT_RW_TIMEOUT,
			WriteTimeout: DEFAULT_RW_TIMEOUT,
		}),
	}
	return
}
Exemplo n.º 14
0
func GetRedis(host, port, password string) (*redis.Client, error) {
	connstr := fmt.Sprintf("%s:%s", host, port)
	redis_cli := redis.NewClient(&redis.Options{
		Addr:     connstr,
		Password: password,
		DB:       0,
	})
	_, err := redis_cli.Ping().Result()
	if err != nil {
		return nil, err
	}
	return redis_cli, nil
}
Exemplo n.º 15
0
func getRedisConn(addr string, password string, db int64) *redis.Client {
	// addr: "127.0.0.1:6379"
	// password: ""
	// db: 0
	client := redis.NewClient(&redis.Options{
		Addr:     addr,
		Password: password,
		DB:       db,
	})
	_, err := client.Ping().Result()
	_err(err)
	return client
}
Exemplo n.º 16
0
func GetRedis() (*redis.Client, error) {
	if redis_cli == nil {
		connstr := fmt.Sprintf("%s:%s", redis_conf.Host, redis_conf.Port)
		redis_cli = redis.NewClient(&redis.Options{
			Addr:     connstr,
			Password: redis_conf.Password,
			DB:       0,
		})
		_, err := redis_cli.Ping().Result()
		if err != nil {
			return nil, err
		}
	}
	return redis_cli, nil
}
Exemplo n.º 17
0
func NewBaseClient(host string, port int) (client *rpkg.Client, err error) {
	op := &rpkg.Options{
		Addr: fmt.Sprintf("%s:%d", host, port),
	}
	client = rpkg.NewClient(op)

	ping := client.Ping()
	if ping.Err() != nil {
		err = errors.New("faild to connect, reason:" + ping.Err().Error())
		return
	}

	// client.ConnPool.(*rpkg.MultiConnPool).MaxCap = 20
	return
}
Exemplo n.º 18
0
func init() {
	conf.ACCESS_KEY = os.Getenv("QINIU_AK")
	conf.SECRET_KEY = os.Getenv("QINIU_SK")
	addr := os.Getenv("REDIS_ADDR")
	if addr == "" {
		addr = "localhost:6379"
	}
	rdx = redis.NewClient(&redis.Options{
		Addr:     addr,
		Password: os.Getenv("REDIS_PASSWORD"),
		DB:       0,
	})
	if _, err := rdx.Ping().Result(); err != nil {
		log.Fatal(err)
	}
}
Exemplo n.º 19
0
func InitRedis() error {
	client := redis.NewClient(&redis.Options{
		Addr:     muconfig.Conf.Redis.Host,
		Password: muconfig.Conf.Redis.Pass, // no password set
		DB:       muconfig.Conf.Redis.Db,   // use default DB
	})

	pong, err := client.Ping().Result()
	if err != nil {
		return err
	}
	Log.Info(pong)
	Redis.SetClient(client)
	// set storage
	SetStorage(Redis)
	return nil
}
func BenchmarkRedisGet(b *testing.B) {
	key := randSeq(30)

	client := redis.NewClient(&redis.Options{Addr: "redis:6379"})

	err := client.Set(key, randSeq(30), 0).Err()
	if err != nil {
		panic(err)
	}

	for i := 0; i < b.N; i++ {
		_, err := client.Get(key).Result()
		if err != nil {
			panic(err)
		}
	}
}
Exemplo n.º 21
0
func init() {
	db_str = config.Mysql.GetConnStr() //"root:root@tcp(d.local:3306)/db_blog?charset=utf8"

	if rc == nil || rc.Ping().Err() != nil {
		rc = redis.NewClient(&redis.Options{
			Addr:     config.Redis.GetConnStr(),
			Password: config.Redis.Password,
			DB:       config.Redis.Database,
		})
		// rc = redis.NewTCPClient(config.Redis.GetConnStr(), config.Redis.Password, config.Redis.Database)
		if err := rc.Ping().Err(); err != nil {
			log.Warningf("Redis Ping Failed by %s, ConnStr is: %s, %s ,%d\n", err, config.Redis.GetConnStr(), config.Redis.Password, config.Redis.Database)
		} else {
			log.Notice("Redis Connect Success...")
		}
	}
}
Exemplo n.º 22
0
Arquivo: main.go Projeto: Ell/bot
func newHandler() (*Handler, error) {
	client := redis.NewClient(&redis.Options{
		Addr:     "localhost:6379",
		Password: "",
		DB:       0,
	})
	pubsub, err := client.Subscribe("messages")

	handler := &Handler{
		Redis:     client,
		Pubsub:    pubsub,
		Sender:    ServerSender{r: client},
		Callbacks: make(map[string][]Callback),
	}

	return handler, err
}
Exemplo n.º 23
0
Arquivo: redis.go Projeto: Pholey/Exgo
func init() {
	info := config.File.GetStringMap("redis")
	port := config.File.GetInt("redis.port")
	database := config.File.GetInt("redis.database")
	Client = redis.NewClient(&redis.Options{
		Addr:     fmt.Sprintf("%s:%d", info["host"].(string), port),
		Password: "",
		DB:       int64(database),
	})

	var _, err = Client.Ping().Result()

	if err != nil {
		panic(err)
	}

	println("Connected to Redis at port " + fmt.Sprintf("%d", port))
}
Exemplo n.º 24
0
func Redis(hostPort, password string, dbID int64) *RedisMemory {
	r := &RedisMemory{
		brain: Brain(),
	}

	r.db = redis.NewClient(&redis.Options{
		Addr:     hostPort,
		Password: password, // no password set
		DB:       dbID,     // use default DB
	})

	_, err := r.db.Ping().Result()
	if err != nil {
		r.err = err
	}

	return r
}
Exemplo n.º 25
0
func NewKV(opts ...kv.Option) kv.KV {
	var options kv.Options
	for _, o := range opts {
		o(&options)
	}

	if len(options.Servers) == 0 {
		options.Servers = []string{"127.0.0.1:6379"}
	}

	return &rkv{
		Client: redis.NewClient(&redis.Options{
			Addr:     options.Servers[0],
			Password: "", // no password set
			DB:       0,  // use default DB
		}),
	}
}
Exemplo n.º 26
0
func NewBroker(cfg *config.BrokerConfig) (*Broker, error) {
	var err error

	broker := new(Broker)
	broker.cfg = cfg
	broker.addr = cfg.Addr
	if len(broker.addr) == 0 {
		return nil, errors.ErrInvalidArgument
	}

	vec := strings.SplitN(cfg.RedisAddr, "/", 2)
	if len(vec) == 2 {
		broker.redisAddr = vec[0]
		broker.redisDB, err = strconv.Atoi(vec[1])
		if err != nil {
			return nil, err
		}
	} else {
		broker.redisAddr = vec[0]
		broker.redisDB = config.DefaultRedisDB
	}

	broker.web = echo.New()

	broker.timer = timer.New(time.Millisecond * 10)
	go broker.timer.Start()

	broker.redisClient = redis.NewClient(
		&redis.Options{
			Addr:     broker.redisAddr,
			Password: "", // no password set
			DB:       int64(broker.redisDB),
		},
	)
	_, err = broker.redisClient.Ping().Result()
	if err != nil {
		golog.Error("broker", "NewBroker", "ping redis fail", 0, "err", err.Error())
		return nil, err
	}

	return broker, nil
}
Exemplo n.º 27
0
func redisNewClient() RedisClient {
	var client RedisClient

	switch Config.Redis.Method {
	case "standalone":
		client = redis.NewClient(&redis.Options{
			Addr:     Config.Redis.Host[0],
			Password: "",
			DB:       0,
		})
	case "sentinel":
		client = redis.NewFailoverClient(&redis.FailoverOptions{
			MasterName:    "master",
			SentinelAddrs: Config.Redis.Host,
		})
	default:
		Log.Fatal("Unknown redis connection method specified")
	}

	return client
}
Exemplo n.º 28
0
/*
 * Subscribe to a stream, listen for all messages and push them to the channel.
 * Meant to be run in a goroutine.
 *
 * @go
 */
func redisSub(s stream, c chan string) {
	logger.Trace.Printf("PubSub subscribing to %s", s.name)

	rsrv := fmt.Sprintf("%s:%d", config.Redis.Host, config.Redis.Port)
	logger.Trace.Println("Connecting to redis server: %s", rsrv)

	client := redis.NewClient(&redis.Options{
		Addr:     rsrv,
		Password: config.Redis.Auth, // no password set
		DB:       0,                 // use default DB
	})

	// TODO, handle this failure.
	pubsub, err := client.Subscribe(s.name)
	if err != nil {
		logger.Error.Printf("Redis subscribe error: %s", err)
		os.Exit(1)
	}
	defer pubsub.Close()

	logger.Trace.Println("Connected to redis cluster")

	/*
		err = client.Publish(stream, "hello").Err()
		if err != nil {
			panic(err)
		}
	*/

	for {
		msg, err := pubsub.ReceiveMessage()
		if err != nil {
			logger.Trace.Printf("No message in channel? %s", err)
			os.Exit(1)
		}

		logger.Trace.Printf("Got redis message: %s => %s", msg.Channel, msg.Payload)
		c <- msg.String()
	}
}
Exemplo n.º 29
0
func connectToRedis(connStr string) (err error) {
	log.WithFields(log.Fields{
		"host": connStr,
	}).Info("Connecting to redis")

	// Open the connection
	rcli = redis.NewClient(&redis.Options{Addr: connStr, DB: 0})

	// Attempt to ping it
	_, err = rcli.Ping().Result()

	if err != nil {
		log.WithFields(log.Fields{
			"host":  connStr,
			"error": err,
		}).Error("Failed to connect to redis")
		fmt.Printf("Failed to connect to redis: %s\n", err)
		return err
	}

	return nil
}
Exemplo n.º 30
0
func NewRedisClient(config *Config) *RedisClient {
	var client *redis.Client
	if config.RedisSentinel {
		client = redis.NewFailoverClient(&redis.FailoverOptions{
			MasterName:    config.RedisMasterName,
			SentinelAddrs: []string{config.RedisAddr},
			Password:      config.RedisPassword,
			DB:            int64(config.RedisDB),
		})
	} else {
		client = redis.NewClient(&redis.Options{
			Addr:     config.RedisAddr,
			Password: config.RedisPassword,
			DB:       int64(config.RedisDB),
		})
	}

	logrus.Infof("Redis client created for addr='%s'", config.RedisAddr)
	return &RedisClient{
		client: client,
	}
}