当前位置: 首页>>代码示例>>Golang>>正文


Golang redis.Dial函数代码示例

本文整理汇总了Golang中github.com/garyburd/redigo/redis.Dial函数的典型用法代码示例。如果您正苦于以下问题:Golang Dial函数的具体用法?Golang Dial怎么用?Golang Dial使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。


在下文中一共展示了Dial函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。

示例1: open

// 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)
}
开发者ID:jackdong16,项目名称:machinery,代码行数:8,代码来源:redis.go

示例2: open

// 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)
}
开发者ID:denkhaus,项目名称:machinery,代码行数:8,代码来源:redis.go

示例3: Open

// 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
}
开发者ID:keepzero,项目名称:redisconn,代码行数:34,代码来源:redis.go

示例4: newPool

// 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
		},
	}
}
开发者ID:jackdong16,项目名称:machinery,代码行数:29,代码来源:redis.go

示例5: NewReceiverFunc

// 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
			}
		}
	}
}
开发者ID:agilemobiledev,项目名称:pingd,代码行数:62,代码来源:redis.go

示例6: NewRedisClient

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
}
开发者ID:johnernaut,项目名称:goatee,代码行数:30,代码来源:pubsub.go

示例7: NewRedisPool

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
		},
	}
}
开发者ID:nagae-memooff,项目名称:surgemq,代码行数:27,代码来源:utils.go

示例8: TestAutoStart

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
	}
}
开发者ID:Songmu,项目名称:go-test-redisserver,代码行数:31,代码来源:redistest_test.go

示例9: TestSelect

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)
}
开发者ID:insionng,项目名称:xcodis,代码行数:32,代码来源:cmd_connection_test.go

示例10: New

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
}
开发者ID:pfeairheller,项目名称:gopi,代码行数:27,代码来源:client.go

示例11: TestAuthCmd

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()
}
开发者ID:IceiceFire,项目名称:reborn,代码行数:34,代码来源:router_test.go

示例12: open

// 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))
}
开发者ID:gooops,项目名称:machinery,代码行数:9,代码来源:redis.go

示例13: Test_TimeoutKeepAlive

// 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")
	}
}
开发者ID:vube,项目名称:redigolock,代码行数:58,代码来源:redigolock_test.go

示例14: TestRedisRestart

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)
	}
}
开发者ID:jcru,项目名称:codis,代码行数:57,代码来源:router_test.go

示例15: TestReadTimeout

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")
	}
}
开发者ID:doubledutch,项目名称:dd-vote,代码行数:55,代码来源:conn_test.go


注:本文中的github.com/garyburd/redigo/redis.Dial函数示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。