func newPool(server, password string, database int, isRedis bool, maxActive int, wait bool) *redis.Pool { return &redis.Pool{ MaxIdle: 50, MaxActive: maxActive, Wait: wait, IdleTimeout: 240 * time.Second, Dial: func() (redis.Conn, error) { c, err := redis.Dial("tcp", server, redis.DialDatabase(database)) if err != nil { return nil, err } if password != "" { if _, err := c.Do("AUTH", password); err != nil { c.Close() return nil, err } } if isRedis { if _, err := c.Do("CLIENT", "SETNAME", "bosun"); err != nil { c.Close() return nil, err } } return c, err }, } }
func (m MRedis) Dial(s string) { c, err := redis.Dial("tcp", "127.0.0.1:6379", redis.DialDatabase(2)) if err != nil { panic(err) } defer c.Close() }
func testTopologyInRedis(t *testing.T, cfg map[string]interface{}) { tests := []struct { out *redisOut name string ips []string }{ {nil, "proxy1", []string{"10.1.0.4"}}, {nil, "proxy2", []string{"10.1.0.9", "fe80::4e8d:79ff:fef2:de6a"}}, {nil, "proxy3", []string{"10.1.0.10"}}, } db := 0 index := cfg["index"].(string) if v, ok := cfg["db_topology"]; ok { db = v.(int) } // prepare redis { conn, err := redis.Dial("tcp", getRedisAddr(), redis.DialDatabase(db)) if err != nil { t.Fatalf("redis.Dial failed %v", err) } // delete old key if present defer conn.Close() conn.Do("DEL", index) } // 1. connect for i := range tests { tests[i].out = newRedisTestingOutput(t, cfg) defer tests[i].out.Close() } // 2. publish ips twice (so all outputs have same topology map) for i := 0; i < 2; i++ { for _, test := range tests { t.Logf("publish %v ips: %v", test.name, test.ips) err := test.out.PublishIPs(test.name, test.ips) assert.NoError(t, err) } } // 3. check names available for _, test := range tests { t.Logf("check %v knows ips", test.name) for _, other := range tests { t.Logf(" check ips of %v", other.name) for _, ip := range other.ips { name := test.out.GetNameByIP(ip) t.Logf(" check ip: %v -> %v", ip, other.name == name) assert.Equal(t, other.name, name) } } } }
func (m MRedis) Set() { c, err := redis.Dial("tcp", "192.168.176.3:6379", redis.DialDatabase(3)) if err != nil { panic(err) } defer c.Close() ok, err := redis.String(c.Do("SET", "testkey", "test")) if err != nil { panic(err) } fmt.Print(ok) }
func (m MRedis) Get(s string) { c, err := redis.Dial("tcp", "192.168.176.3:6379", redis.DialDatabase(2)) if err != nil { panic(err) } defer c.Close() ok, err := redis.String(c.Do("GET", "service_role_code")) if err != nil { panic(err) } fmt.Print(ok) }
func c_redis_counters(server string, db int) (opentsdb.MultiDataPoint, error) { var md opentsdb.MultiDataPoint conn, err := redis.Dial("tcp", server, redis.DialDatabase(db)) if err != nil { return md, err } defer conn.Close() if _, err := conn.Do("CLIENT", "SETNAME", "scollector"); err != nil { return md, err } cursor := 0 for { vals, err := redis.Values(conn.Do("HSCAN", collect.RedisCountersKey, cursor)) if err != nil { return md, err } if len(vals) != 2 { return md, fmt.Errorf("Unexpected number of values") } cursor, err = redis.Int(vals[0], nil) if err != nil { return md, err } pairs, err := redis.StringMap(vals[1], nil) if err != nil { return md, err } for mts, val := range pairs { parts := strings.Split(mts, ":") if len(parts) != 2 { slog.Errorf("Invalid metric tag set counter: %s", mts) continue } metric := parts[0] tags, err := opentsdb.ParseTags(parts[1]) if err != nil { slog.Errorf("Invalid tags: %s", parts[1]) continue } v, err := strconv.Atoi(val) if err != nil { slog.Errorf("Invalid counter value: %s", val) continue } Add(&md, metric, v, tags, metadata.Counter, metadata.Count, "") } if cursor == 0 { break } } return md, nil }
func c_redis_counters(server string, db int) (opentsdb.MultiDataPoint, error) { var md opentsdb.MultiDataPoint conn, err := redis.Dial("tcp", server, redis.DialDatabase(db)) if err != nil { return md, slog.Wrap(err) } defer conn.Close() //do a dance to detect proper hscan command for ledis or redis hscanCmd := "XHSCAN" info, err := redis.String(conn.Do("info", "server")) if err != nil { return md, slog.Wrap(err) } if strings.Contains(info, "redis_version") { hscanCmd = "HSCAN" } cursor := "0" for { vals, err := redis.Values(conn.Do(hscanCmd, collect.RedisCountersKey, cursor)) if err != nil { return md, slog.Wrap(err) } if len(vals) != 2 { return md, fmt.Errorf("Unexpected number of values") } cursor, err = redis.String(vals[0], nil) if err != nil { return md, slog.Wrap(err) } pairs, err := redis.StringMap(vals[1], nil) if err != nil { return md, slog.Wrap(err) } for key, val := range pairs { ak := models.AlertKey(key) v, err := strconv.Atoi(val) if err != nil { slog.Errorf("Invalid counter value: %s", val) continue } Add(&md, ak.Name(), v, ak.Group(), metadata.Counter, metadata.Count, "") } if cursor == "" || cursor == "0" { break } } return md, nil }
func (m MRedis) Hmget(s string) { c, err := redis.Dial("tcp", "127.0.0.1:6379", redis.DialDatabase(2)) if err != nil { panic(err) } defer c.Close() ok, err := redis.Strings(c.Do("HMGET", "w.pplive.com", "cache_key_without_args", "sorted_src_ip_usability_list")) if err != nil { panic(err) } for k, v := range ok { fmt.Printf("%d = %s\n", k, v) } }
//连接 func (p *Client_t) Connect(ip string, port uint16, dataBases int) (err error) { p.ip = ip p.port = port p.dataBases = dataBases var addr = ip + ":" + strconv.Itoa(int(port)) dialOption := redis.DialDatabase(dataBases) p.Conn, err = redis.Dial("tcp", addr, dialOption) if nil != err { fmt.Println("######redis.Dial err:", err, ip, port, dataBases) return err } return err }
func newRedisPool(server string, database int) *redis.Pool { return &redis.Pool{ MaxIdle: 10, MaxActive: 10, Wait: true, IdleTimeout: 240 * time.Second, Dial: func() (redis.Conn, error) { c, err := redis.Dial("tcp", server, redis.DialDatabase(database)) if err != nil { return nil, err } return c, err }, } }
func (m MRedis) Hgetall(s string) { c, err := redis.Dial("tcp", "127.0.0.1:6379", redis.DialDatabase(2)) if err != nil { panic(err) } defer c.Close() ok, err := redis.StringMap(c.Do("HGETALL", "*.pplive.com")) if err != nil { panic(err) } for k, v := range ok { fmt.Printf("%s = %s\n", k, v) } }
func InitRedis() { redisDB := redigo.DialDatabase(config.DatabaseID) redisPool = &redigo.Pool{ MaxIdle: 10, IdleTimeout: 1 * time.Second, Dial: func() (redigo.Conn, error) { return redigo.Dial("tcp", config.RedisServerAndPort, redisDB) }, TestOnBorrow: func(c redigo.Conn, t time.Time) (err error) { _, err = c.Do("PING") if err != nil { panic("Error connecting to redis") } return }, } }
func newRedisPool(server string, database int) *redis.Pool { return &redis.Pool{ MaxIdle: 10, MaxActive: 10, Wait: true, IdleTimeout: 240 * time.Second, Dial: func() (redis.Conn, error) { c, err := redis.Dial("tcp", server, redis.DialDatabase(database)) if err != nil { return nil, err } if _, err := c.Do("CLIENT", "SETNAME", metricRoot+"_counters"); err != nil { c.Close() return nil, err } return c, err }, } }
func startRedis() redis.Conn { addr := os.Getenv("REDIS_ADDR") if addr == "" { addr = redisDefaultAddress } dbIndex, err := strconv.Atoi(os.Getenv("REDIS_DB")) if err != nil { dbIndex = redisDefaultDDb } client, err := redis.Dial("tcp", addr, redis.DialDatabase(dbIndex)) if err != nil { panic("Failed to start redis.\n" + err.Error()) } log.Printf("Redis started addr=%s dbIndex=%d.\n", addr, dbIndex) return client }
// NewPool creates a redis pool connected to the given host:port and db. func NewPool(host string, port, db uint) (*Wredis, error) { if host == "" { return nil, errors.New("host cannot be empty") } if port == 0 { return nil, errors.New("port cannot be 0") } addr := fmt.Sprintf("%s:%d", host, int(port)) pool := &redis.Pool{ MaxIdle: 3, IdleTimeout: 240 * time.Second, Dial: func() (redis.Conn, error) { return redis.Dial("tcp", addr, redis.DialDatabase(int(db))) }, TestOnBorrow: func(c redis.Conn, t time.Time) error { _, err := c.Do("PING") return err }, } return &Wredis{pool, true}, nil }
func (p *RedisCounterStorage) initalPool() { p.pool = &redis.Pool{ MaxIdle: p.redisConfig.MaxIdle, IdleTimeout: p.redisConfig.IdleTimeout, Dial: func() (redis.Conn, error) { c, err := redis.Dial("tcp", p.redisConfig.Address, redis.DialDatabase(p.redisConfig.Db), redis.DialPassword(p.redisConfig.Password)) if err != nil { return nil, err } return c, err }, TestOnBorrow: func(c redis.Conn, t time.Time) error { _, err := c.Do("PING") return err }, Wait: true, } }
func (t *topology) PublishIPs(name string, localAddrs []string) error { if t.cfg.host == "" { debugf("Not publishing IPs because, no host configured") } dialOpts := []redis.DialOption{ redis.DialPassword(t.cfg.password), redis.DialDatabase(t.cfg.db), } if t.transCfg != nil { d, err := transport.MakeDialer(t.transCfg) if err != nil { return err } dialOpts = append(dialOpts, redis.DialNetDial(d.Dial)) } conn, err := redis.Dial("tcp", t.cfg.host, dialOpts...) if err != nil { return err } defer conn.Close() _, err = conn.Do("HSET", name, "ipaddrs", strings.Join(localAddrs, ",")) if err != nil { logp.Err("[%s] Fail to set the IP addresses: %s", name, err) return err } _, err = conn.Do("EXPIRE", name, int(t.cfg.expire.Seconds())) if err != nil { logp.Err("[%s] Fail to set the expiration time: %s", name, err) return err } t.updateMap(conn) return nil }
func testredis() { fmt.Printf("test redis") c, err := redis.Dial("tcp", "115.29.188.30:7379", redis.DialPassword("zouke7788"), redis.DialDatabase(7)) if err != nil { panic(err) } defer c.Close() c.Send("SET", "go_basecode_lang", "xxxx1") c.Flush() c.Receive() c.Send("GET", "go_basecode_lang") c.Flush() value, err := c.Receive() if nil != err { panic(err) } fmt.Printf("%s", value) return }
func newPool(conf *Configure, host string, dbnum int) *RedisPool { pool := &redis.Pool{ MaxIdle: conf.Redis.MaxIdle, MaxActive: conf.Redis.MaxActive, IdleTimeout: time.Duration(conf.Redis.IdleTimeout) * time.Second, Dial: func() (redis.Conn, error) { opt_timeout := redis.DialConnectTimeout(time.Duration(conf.Redis.ConnTimeout) * time.Second) opt_selectdb := redis.DialDatabase(dbnum) c, err := redis.Dial("tcp", host, opt_timeout, opt_selectdb) if err != nil { return nil, err } return c, err }, TestOnBorrow: func(c redis.Conn, t time.Time) error { _, err := c.Do("PING") return err }, } p := &RedisPool{p: pool, db: dbnum, host: host} log.Printf("[redis_pool]on host:%s:%d, create redis pool success.\n", host, dbnum) return p }
func testPublishList(t *testing.T, cfg map[string]interface{}) { batches := 100 batchSize := 1000 total := batches & batchSize db := 0 index := cfg["index"].(string) if v, ok := cfg["db"]; ok { db = v.(int) } conn, err := redis.Dial("tcp", getRedisAddr(), redis.DialDatabase(db)) if err != nil { t.Fatalf("redis.Dial failed %v", err) } // delete old key if present defer conn.Close() conn.Do("DEL", index) out := newRedisTestingOutput(t, cfg) err = sendTestEvents(out, batches, batchSize) assert.NoError(t, err) results := make([][]byte, total) for i := range results { results[i], err = redis.Bytes(conn.Do("LPOP", index)) assert.NoError(t, err) } for i, raw := range results { evt := struct{ Message int }{} err = json.Unmarshal(raw, &evt) assert.NoError(t, err) assert.Equal(t, i+1, evt.Message) } }
func main() { // Load the configuration m := multiconfig.NewWithPath(os.Getenv("config")) cfg := &dns.Config{} m.MustLoad(cfg) // Parse the log level lvl, err := log.LvlFromString(cfg.LogLevel) if err != nil { panic(err) } // Create a new logger l := log.New() l.SetHandler( log.LvlFilterHandler(lvl, log.StdoutHandler), ) // Start the initialization il := l.New("module", "init") il.Info("Starting up the application") // Parse the Redis connection string url := redisurl.Parse(cfg.Redis) // Create an options list opts := []redis.DialOption{} if url.Database != 0 { opts = append(opts, redis.DialDatabase(url.Database)) } if url.Password != "" { opts = append(opts, redis.DialPassword(url.Password)) } // Verify the DNS setup results, err := net.LookupNS(cfg.Domain) if err != nil { il.Warn("Unable to look up the NS of domain", "domain", cfg.Domain, "error", err.Error()) } if len(results) > 0 { found := false for _, record := range results { if record.Host == cfg.Hostname { found = true } } if !found { existing := []string{} for _, record := range results { existing = append(existing, record.Host) } il.Warn("Invalid NS records for the domain", "domain", cfg.Domain, "records", existing) } } else { il.Warn("No NS records found for domain", "domain", cfg.Domain, "error", err.Error()) } // Dial the server rc, err := redis.Dial("tcp", url.Host+":"+strconv.Itoa(url.Port), opts...) if err != nil { il.Crit("Unable to connect to the Redis server", "error", err.Error()) return } // Initialize the DNS server ds := &dns.DNS{ Config: cfg, Redis: rc, } if err := ds.ListenAndServe(); err != nil { il.Crit("Error while listening to the specified port", "error", err.Error()) } }
func testPublishChannel(t *testing.T, cfg map[string]interface{}) { batches := 100 batchSize := 1000 total := batches & batchSize db := 0 index := cfg["index"].(string) if v, ok := cfg["db"]; ok { db = v.(int) } conn, err := redis.Dial("tcp", getRedisAddr(), redis.DialDatabase(db)) if err != nil { t.Fatalf("redis.Dial failed %v", err) } // delete old key if present defer conn.Close() conn.Do("DEL", index) // subscribe to packetbeat channel psc := redis.PubSubConn{conn} if err := psc.Subscribe(index); err != nil { t.Fatal(err) } defer psc.Unsubscribe(index) // connect and publish events var wg sync.WaitGroup var pubErr error out := newRedisTestingOutput(t, cfg) wg.Add(1) go func() { defer wg.Done() pubErr = sendTestEvents(out, batches, batchSize) }() // collect published events by subscription var messages [][]byte assert.NoError(t, conn.Err()) for conn.Err() == nil { t.Logf("try collect message") switch v := psc.Receive().(type) { case redis.Message: messages = append(messages, v.Data) case error: t.Error(v) default: t.Logf("received: %#v", v) } if len(messages) == total { break } } wg.Wait() // validate assert.NoError(t, pubErr) assert.Equal(t, total, len(messages)) for i, raw := range messages { evt := struct{ Message int }{} err = json.Unmarshal(raw, &evt) assert.NoError(t, err) assert.Equal(t, i+1, evt.Message) } }
// Returns / creates instance of Redis connection func (redisBroker *RedisBroker) open() (redis.Conn, error) { if redisBroker.socketPath != "" { return redis.Dial("unix", redisBroker.socketPath, redis.DialPassword(redisBroker.password), redis.DialDatabase(redisBroker.db)) } // package redis takes care of pwd or db return redis.Dial("tcp", redisBroker.host, redis.DialPassword(redisBroker.password), redis.DialDatabase(redisBroker.db)) }