本文整理汇总了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)
}
示例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)
}
示例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
}
示例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
},
}
}
示例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
}
}
}
}
示例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
}
示例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
},
}
}
示例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
}
}
示例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)
}
示例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
}
示例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()
}
示例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))
}
示例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")
}
}
示例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)
}
}
示例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")
}
}