// Returns / creates instance of Redis connection func (redisBroker *RedisBroker) open() (redis.Conn, error) { if redisBroker.password != "" { return redis.Dial("tcp", redisBroker.host, redis.DialPassword(redisBroker.password)) } return redis.Dial("tcp", redisBroker.host) }
// Returns / creates instance of Redis connection func (redisBackend *RedisBackend) open() (redis.Conn, error) { if redisBackend.password != "" { return redis.Dial("tcp", redisBackend.host, redis.DialPassword(redisBackend.password)) } return redis.Dial("tcp", redisBackend.host) }
// Open a new *R func Open(network, address, password string) (*R, error) { // Test connection _, err := redis.Dial(network, address) // redigo pool pool := &redis.Pool{ MaxIdle: 3, IdleTimeout: 240 * time.Second, Dial: func() (redis.Conn, error) { c, err := redis.Dial(network, address) if err != nil { return nil, err } if password != "" { if _, err := c.Do("AUTH", password); err != nil { c.Close() return nil, err } } return c, err }, TestOnBorrow: func(c redis.Conn, t time.Time) error { _, err := c.Do("PING") return err }, } // Return *R return &R{ pool: pool, }, err }
// Returns a new pool of Redis connections func (redisBroker *RedisBroker) newPool() *redis.Pool { return &redis.Pool{ MaxIdle: 3, IdleTimeout: 240 * time.Second, Dial: func() (redis.Conn, error) { var ( c redis.Conn err error ) if redisBroker.password != "" { c, err = redis.Dial("tcp", redisBroker.host, redis.DialPassword(redisBroker.password)) } else { c, err = redis.Dial("tcp", redisBroker.host) } if err != nil { return nil, err } return c, err }, TestOnBorrow: func(c redis.Conn, t time.Time) error { _, err := c.Do("PING") return err }, } }
// NewReceiverFunc returns the function that // listens of redis for start/stop commands func NewReceiverFunc(redisAddr string, redisDB int) pingd.Receiver { return func(startHostCh, stopHostCh chan<- pingd.Host) { conPubSub, err := redis.Dial("tcp", redisAddr) if err != nil { log.Panicln(err) } connKV, err := redis.Dial("tcp", redisAddr) if err != nil { log.Panicln(err) } servername, _ := os.Hostname() conPubSub.Do("CLIENT", "SETNAME", "receive-"+servername) conPubSub.Do("SELECT", redisDB) connKV.Do("CLIENT", "SETNAME", "receive-"+servername) connKV.Do("SELECT", redisDB) psc := redis.PubSubConn{conPubSub} psc.Subscribe(startRK, stopRK) for { switch n := psc.Receive().(type) { case redis.Message: if n.Channel == startRK { host := string(n.Data) down := false if strings.HasSuffix(host, downSuffix) { down = true host = strings.Replace(host, downSuffix, "", 1) } // Add to the list of pinged hosts _, err := connKV.Do("SADD", hostListRK, host) if err != nil { log.Panicln(err) } startHostCh <- pingd.Host{Host: host, Down: down} } else if n.Channel == stopRK { host := string(n.Data) // Remove from the list of pinged hosts _, err := connKV.Do("SREM", hostListRK, host) if err != nil { log.Panicln(err) } stopHostCh <- pingd.Host{Host: host} } case redis.PMessage: case redis.Subscription: log.Println("BOOT Listening to " + n.Channel) case error: log.Printf("error: %v\n", n) return } } } }
func NewRedisClient(host string) (*RedisClient, error) { conn, err := redis.Dial("tcp", host) if err != nil { log.Printf("Error dialing redis pubsub: %s", err) return nil, err } pubsub, _ := redis.Dial("tcp", host) client := RedisClient{conn, redis.PubSubConn{pubsub}, sync.Mutex{}} if DEBUG { log.Println("Subscribed to Redis on: ", host) } go func() { for { time.Sleep(200 * time.Millisecond) client.Lock() client.conn.Flush() client.Unlock() } }() go client.PubsubHub() h.rclient = &client h.rconn = conn return &client, nil }
func NewRedisPool() *redis.Pool { return &redis.Pool{ MaxIdle: config.GetInt("redis_max_idle"), MaxActive: config.GetInt("redis_concurrent"), // max number of connections Dial: func() (redis.Conn, error) { var ( c redis.Conn err error ) redis_host := fmt.Sprintf("%s:%s", config.GetMulti("redis_host", "redis_port")...) redis_passwd := config.Get("redis_passwd") if redis_passwd != "" { pwd := redis.DialPassword(redis_passwd) c, err = redis.Dial("tcp", redis_host, pwd) } else { c, err = redis.Dial("tcp", redis_host) } return c, err }, TestOnBorrow: func(c redis.Conn, t time.Time) error { _, err := c.Do("PING") return err }, } }
func TestAutoStart(t *testing.T) { s, err := NewServer(false, nil) if err != nil { t.Error("NewServer is err:", err.Error()) } defer s.Stop() t.Log("unixsocket:", s.Config["unixsocket"]) _, err = redis.Dial("unix", s.Config["unixsocket"]) if err == nil { t.Error("should not connect to redis server. because redis server is not runing yet") return } t.Log("start redis server immediately") if err := s.Start(); err != nil { t.Error("failed to start", err) } conn, err := redis.Dial("unix", s.Config["unixsocket"]) if err != nil { t.Error("failed to connect to redis via unixscoket:", err.Error()) return } _, err = conn.Do("PING") if err != nil { t.Error("failed to execute command:", err) return } }
func TestSelect(t *testing.T) { s, err := Run() ok(t, err) defer s.Close() c, err := redis.Dial("tcp", s.Addr()) ok(t, err) _, err = redis.String(c.Do("SET", "foo", "bar")) ok(t, err) _, err = redis.String(c.Do("SELECT", "5")) ok(t, err) _, err = redis.String(c.Do("SET", "foo", "baz")) ok(t, err) // Direct access. got, err := s.Get("foo") ok(t, err) equals(t, "bar", got) s.Select(5) got, err = s.Get("foo") ok(t, err) equals(t, "baz", got) // Another connection should have its own idea of the db: c2, err := redis.Dial("tcp", s.Addr()) ok(t, err) v, err := redis.String(c2.Do("GET", "foo")) ok(t, err) equals(t, "bar", v) }
func New(addr string) (c *Client, err error) { c = &Client{ addr: addr, in: make(chan jobInfo), } c.conn, err = redis.Dial("tcp", c.addr) if err != nil { return } c.clientPool = &redis.Pool{ MaxIdle: 3, MaxActive: 25, // max number of connections Dial: func() (redis.Conn, error) { c, err := redis.Dial("tcp", c.addr) if err != nil { panic(err.Error()) } return c, err }, } go c.jobLoop() return }
func (s *testProxyRouterSuite) TestAuthCmd(c *C) { if len(proxyAuth) > 0 { cc, err := redis.Dial("tcp", proxyAddr) c.Assert(err, IsNil) _, err = cc.Do("SET", "foo", "bar") c.Assert(err, NotNil) c.Assert(err, ErrorMatches, "ERR NOAUTH Authentication required") } else { cc, err := redis.Dial("tcp", proxyAddr) c.Assert(err, IsNil) _, err = cc.Do("SET", "foo", "bar") c.Assert(err, IsNil) } cc, err := redis.Dial("tcp", proxyAddr) c.Assert(err, IsNil) ok, err := redis.String(cc.Do("AUTH", proxyAuth)) c.Assert(err, IsNil) c.Assert(ok, Equals, "OK") _, err = cc.Do("SET", "foo", "bar") c.Assert(err, IsNil) ok, err = redis.String(cc.Do("AUTH", "Wrong-auth-key")) c.Assert(err, NotNil) c.Assert(err, ErrorMatches, "ERR invalid auth") c.Assert(ok, Equals, "") s.s1.store.Reset() s.s2.store.Reset() }
// 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)) }
// Test that the keepalive function actually keeps the lock alive func Test_TimeoutKeepAlive(t *testing.T) { key := "Test_Keepalive" wg := new(sync.WaitGroup) conn1, err := redigo.Dial("tcp", RedisHost) if err != nil { t.Errorf("redigo.Dial failure due to '%s'", err) return } conn2, err := redigo.Dial("tcp", RedisHost) if err != nil { t.Errorf("redigo.Dial failure due to '%s'", err) return } lock1 := New(conn1, key, 1000, 1000, 0, 5) status, err := lock1.Lock() if err != nil || !status { t.Error("unable to lock") } wg.Add(20) go func() { for i := 0; i < 20; i++ { err := lock1.KeepAlive() if err != nil { t.Errorf("timed out during lock contention due to '%v'", err) } wg.Done() time.Sleep(time.Second / 2) } }() time.Sleep(time.Second * 2) lock2 := New(conn2, key, 1000, 1000, 0, 5) status, err = lock2.Lock() if status { t.Error("should not have been able to lock") } wg.Wait() time.Sleep(time.Second * 2) status, err = lock2.Lock() if err != nil || !status { t.Error("should have been able to lock") } }
func TestRedisRestart(t *testing.T) { InitEnv() c, err := redis.Dial("tcp", "localhost:19000") if err != nil { t.Fatal(err) } defer c.Close() _, err = c.Do("SET", "key1", "value1") if err != nil { t.Fatal(err) } _, err = c.Do("SET", "key2", "value2") if err != nil { t.Fatal(err) } //close redis redis1.Close() redis2.Close() _, err = c.Do("SET", "key3", "value1") if err == nil { t.Fatal("should be error") } _, err = c.Do("SET", "key4", "value2") if err == nil { t.Fatal("should be error") } //restart redis redis1.Restart() redis2.Restart() time.Sleep(3 * time.Second) //proxy should closed our connection _, err = c.Do("SET", "key5", "value3") if err == nil { t.Error("should be error") } // may error c, err = redis.Dial("tcp", "localhost:19000") c.Do("SET", "key6", "value6") c.Close() //now, proxy should recovered from connection error c, err = redis.Dial("tcp", "localhost:19000") if err != nil { t.Fatal(err) } defer c.Close() _, err = c.Do("SET", "key7", "value7") if err != nil { t.Fatal(err) } }
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") } }
func init() { if s := os.Getenv("WERCKER_REDIS_HOST"); s != "" { conn, _ = redis.Dial("tcp", s+":6379") } else { conn, _ = redis.Dial("tcp", address) } }
// Test that our locks are actually preventing race conditions by doing a // non-atomic Redis operation surrounded by a lock func Test_Increment(t *testing.T) { key := "Test_Increment" wg := new(sync.WaitGroup) wg.Add(100) for i := 0; i < 100; i++ { go func() { defer wg.Done() conn, err := redigo.Dial("tcp", RedisHost) if err != nil { t.Errorf("redigo.Dial failure due to '%s'", err) return } lock := New(conn, key, 2000, DefaultAutoExpire, DefaultHold, 5) status, err := lock.Lock() if status { // arbitrary distributed non-atomic operation v, _ := redigo.Int(conn.Do("GET", key)) v++ time.Sleep(100000) conn.Do("SET", key, v) } else { if err != nil { t.Errorf("lock operation failure due to '%s", err) } else { t.Error("timed out during lock contention") } } lock.UnlockIfLocked() }() } wg.Wait() conn, err := redigo.Dial("tcp", RedisHost) if err != nil { t.Errorf("redigo.Dial failure due to '%s'", err) } v, _ := redigo.Int(conn.Do("GET", key)) if v != 100 { t.Error("increment miscalculation") } conn.Do("DEL", key) }
func insertImage(repo, nodeID string, c *gocheck.C) func() { conn, err := redis.Dial("tcp", "localhost:6379") c.Assert(err, gocheck.IsNil) defer conn.Close() _, err = conn.Do("set", "tests:image:"+repo, nodeID) c.Assert(err, gocheck.IsNil) return func() { conn, err := redis.Dial("tcp", "localhost:6379") c.Assert(err, gocheck.IsNil) defer conn.Close() conn.Do("del", "tests:image:"+repo) } }
func (eb *EventBus) Join(configFileName string) (err error) { data, err := ioutil.ReadFile(configFileName) if err != nil { log.Fatalln("Can't open Gopi config file") } err = json.Unmarshal(data, eb) if err != nil { log.Fatalln("Unable to parse Gopi config file") } if eb.Address == "" { log.Fatalln("Need redis address") } eb.inconn, err = redis.Dial("tcp", eb.Address) if err != nil { log.Fatalln("Error connecting to redis", err) } eb.outconn, err = redis.Dial("tcp", eb.Address) if err != nil { log.Fatalln("Error connecting to redis", err) } var names []interface{} for name, holders := range eb.handlerMap { names = append(names, "gopi:queue:"+name) log.Println("listening at", name) for _, holder := range holders { go eb.listen(holder.c, name, holder.h) } } names = append(names, 0) for { n, err := redis.Values(eb.inconn.Do("BRPOP", names...)) if err != nil { log.Println("Opps", err) continue } queuename := string(n[0].([]byte)) name := strings.Split(queuename, ":")[2] for _, handler := range eb.handlerMap[name] { handler.c <- n[1].([]byte) } } return }
func (d *Disque) explore() (err error) { // clear nodes d.nodes = map[string]string{} for _, host := range d.servers { var scout redis.Conn if scout, err = redis.Dial("tcp", host); err == nil { defer scout.Close() if lines, err := redis.String(scout.Do("CLUSTER", "NODES")); err == nil { for _, line := range strings.Split(lines, "\n") { if strings.TrimSpace(line) != "" { fields := strings.Fields(line) id := fields[0] clusterHost := fields[1] flag := fields[2] prefix := id[0:8] if flag == "myself" { // close main client if it exists if d.client != nil { d.client.Close() } // configure main client if d.client, err = redis.Dial("tcp", host); err == nil { // keep track of selected node d.prefix = prefix } } d.nodes[prefix] = clusterHost } } return err } else { log.Printf("Error returned when querying for cluster nodes on host: %s, exception: %s", host, err) } } else { log.Printf("Error while exploring connection to host: %s, exception: %s", host, err) } } if len(d.nodes) == 0 { err = errors.New("Nodes unavailable") } return err }
// Returns a new Redis client. The underlying redigo package uses // Go's bufio package which will flush the connection when it contains // enough data to send, but we still need to set up some kind of timed // flusher, so it's done here with a goroutine. func NewRedisClient(host string) *RedisClient { host = fmt.Sprintf("%s:6379", host) conn, _ := redis.Dial("tcp", host) pubsub, _ := redis.Dial("tcp", host) client := RedisClient{conn, redis.PubSubConn{pubsub}, sync.Mutex{}} go func() { for { time.Sleep(200 * time.Millisecond) client.Lock() client.conn.Flush() client.Unlock() } }() return &client }
//savepath like redisserveraddr,poolsize,password //127.0.0.1:6379,100,astaxie func (rp *RedisProvider) SessionInit(maxlifetime int64, savePath string) error { rp.maxlifetime = maxlifetime configs := strings.Split(savePath, ",") if len(configs) > 0 { rp.savePath = configs[0] } if len(configs) > 1 { poolsize, err := strconv.Atoi(configs[1]) if err != nil || poolsize <= 0 { rp.poolsize = MAX_POOL_SIZE } else { rp.poolsize = poolsize } } else { rp.poolsize = MAX_POOL_SIZE } if len(configs) > 2 { rp.password = configs[2] } rp.poollist = redis.NewPool(func() (redis.Conn, error) { c, err := redis.Dial("tcp", rp.savePath) if err != nil { return nil, err } if rp.password != "" { if _, err := c.Do("AUTH", rp.password); err != nil { c.Close() return nil, err } } return c, err }, rp.poolsize) return nil }
func (db *DB) dial() (redis.Conn, error) { conn, err := redis.Dial("tcp", db.cfg.addr) if err != nil { return nil, err } //if LogLevel >= 2 { // conn = redis.NewLoggingConn(conn, Logger, "") //} if db.cfg.password != "" { if _, err := conn.Do("AUTH", db.cfg.password); err != nil { //Logln("h: invalid redis password") conn.Close() return nil, err } } if db.cfg.db != 0 { if _, err := conn.Do("SELECT", db.cfg.db); err != nil { //Logln("h: invalid redis password") conn.Close() return nil, err } } return conn, nil }
func RedisDo(commandName string, args ...interface{}) (interface{}, error) { var redisConn redis.Conn var err error for i := 0; i < redisRetryCount; i++ { if redisConn, err = redis.Dial("tcp", redisAddress); err != nil { fog.Warn("redis.Dial: %s", err) time.Sleep(5 * time.Second) continue } result, err := redisConn.Do(commandName, args...) redisConn.Close() if err != nil { fog.Warn("RedisDo: %s", err) time.Sleep(1 * time.Second) continue } return result, nil } return nil, fmt.Errorf("RedisDo: failed after %d retries %s", redisRetryCount, err) }
//redis 连接 func InitRedis() redis.Conn { c, err := redis.Dial(Network, Address+":"+Port) if err != nil { log.Fatal(err) } return c }
func TestOrderMaker(t *testing.T) { url := "http://calback:4444" wg := new(sync.WaitGroup) wg.Add(1) mux := http.NewServeMux() mux.HandleFunc("/orders", func(rw http.ResponseWriter, rq *http.Request) { assert.Equal(t, http.MethodPost, rq.Method) var order commons.Order commons.UnmarshalOrderFromHttp(rq, &order) c, _ := redis.Dial("tcp", "192.168.99.100:6379") defer c.Close() exist, _ := c.Do("EXISTS", order.Id) assert.Equal(t, int64(1), exist.(int64)) assert.True(t, strings.HasPrefix(order.CallBackUrl, url+"/bill/playerId/")) wg.Done() rw.WriteHeader(200) }) srv := httptest.NewServer(mux) unReg := make(chan commons.Registration) reg := commons.Registration{PlayerId: "playerId", Ip: srv.URL} startNewOrderMaker(url, "192.168.99.100:6379", reg, unReg) wg.Wait() }
func main() { N := 8 c, err := redis.Dial("tcp", ":6379") if err != nil { panic(err) } defer c.Close() var S, end string for i := 0; i < N; i++ { S = S + "A" end = end + "D" } now := N - 1 c.Do("SELECT", "1") a := List{} for S != end { switch S[now] { case 'A': { S = S[:now] + "B" + S[now+1:] now = N - 1 } case 'B': { S = S[:now] + "C" + S[now+1:] now = N - 1 } case 'C': { S = S[:now] + "D" + S[now+1:] now = N - 1 } case 'D': { S = S[:now] + "A" + S[now+1:] now-- continue } } //fmt.Println(S) //SI[S] = get(S, c) a = append(a, Entry{S, get(S, c)}) } /* for k, v := range SI { e := Entry{k, v} a = append(a, e) } */ fmt.Println("!") sort.Sort(a) fmt.Println(a) }
func getRedis() (redis.Conn, error) { urlParts, err := url.Parse(RedisURL) if err != nil { return nil, fmt.Errorf("error parsing Redis url: %s", err) } auth := "" if urlParts.User != nil { if password, ok := urlParts.User.Password(); ok { auth = password } } c, err := redis.Dial("tcp", urlParts.Host) if err != nil { return nil, fmt.Errorf("error connecting to Redis: %s", err) } if len(auth) > 0 { _, err = c.Do("AUTH", auth) if err != nil { return nil, fmt.Errorf("error authenticating with Redis: %s", err) } } if len(urlParts.Path) > 1 { db := strings.TrimPrefix(urlParts.Path, "/") _, err = c.Do("SELECT", db) if err != nil { return nil, fmt.Errorf("error selecting Redis db: %s", err) } } return c, nil }
func Test_Redis(t *testing.T) { pool := newPool("123.56.98.103:6379", "") conn := pool.Get() fmt.Println(conn) // go func() { cc, err := redis.Dial("tcp", "123.56.98.103:6379") if err != nil { panic(err) } cc.Do("select", "9") psc := redis.PubSubConn{Conn: cc} psc.Subscribe("products") time.Sleep(1 * time.Second) for { switch v := psc.Receive().(type) { case redis.Subscription: fmt.Printf("%s: %s %d\n", v.Channel, v.Kind, v.Count) case redis.Message: //单个订阅subscribe fmt.Printf("%s: message: %s\n", v.Channel, v.Data) case redis.PMessage: //模式订阅psubscribe fmt.Printf("PMessage: %s %s %s\n", v.Pattern, v.Channel, v.Data) case error: fmt.Println("error", v) time.Sleep(1 * time.Second) } } // }() }
func NewRedisClient(config *Configure, logger *log4jzl.Log4jzl) (*RedisClient, error) { counter := &RedisClient{} counter.config = config counter.logger = logger counter.pool = &redis.Pool{ MaxIdle: 30, IdleTimeout: 240 * time.Second, Dial: func() (redis.Conn, error) { host, _ := config.GetRedisHost() port, _ := config.GetRedisPort() connStr := fmt.Sprintf("%v:%v", host, port) c, err := redis.Dial("tcp", connStr) if err != nil { return nil, err } return c, err }, TestOnBorrow: func(c redis.Conn, t time.Time) error { _, err := c.Do("PING") return err }, } return counter, nil }