Browse files

add password and db to client

  • Loading branch information...
1 parent b81da29 commit f6900db40a3742bc195b4d85f0e85ccaa236e94d Simon Zimmermann committed May 7, 2012
Showing with 45 additions and 14 deletions.
  1. +33 −3 exp/connection.go
  2. +11 −9 exp/godis.go
  3. +1 −1 schema/key.go
  4. +0 −1 schema/tag.go
View
36 exp/connection.go
@@ -31,15 +31,45 @@ type Conn struct {
// NewConn then returns a Conn struct which implements the Connection
// interface. It's easy to use this interface to create your own
// redis client or to simply talk to the redis database.
-func NewConn(addr, proto string) (*Conn, error) {
- c, err := net.Dial(proto, addr)
+func NewConn(addr, proto string, db int, password string) (*Conn, error) {
+ conn, err := net.Dial(proto, addr)
if err != nil {
return nil, err
}
ConnSum++
- return &Conn{bufin.NewReader(c), c}, nil
+ c := &Conn{bufin.NewReader(conn), conn}
+
+ if password != "" {
+ e := c.Write("AUTH", password)
+
+ if e != nil {
+ return nil, e
+ }
+
+ _, e = c.Read()
+
+ if e != nil {
+ return nil, e
+ }
+ }
+
+ if db != 0 {
+ e := c.Write("SELECT", db)
+
+ if e != nil {
+ return nil, e
+ }
+
+ _, e = c.Read()
+
+ if e != nil {
+ return nil, e
+ }
+ }
+
+ return c, nil
}
// Read reads one reply of the socket connection. If there is no reply waiting
View
20 exp/godis.go
@@ -65,20 +65,22 @@ import (
// in a pool. The size of the pool can be adjusted with by setting the
// MaxConnections variable before creating a client.
type Client struct {
- Addr string
- Proto string
- pool *connPool
+ Addr string
+ Proto string
+ Db int
+ Password string
+ pool *connPool
}
// NewClient expects a addr like "tcp:127.0.0.1:6379"
// It returns a new *Client.
-func NewClient(addr string) *Client {
+func NewClient(addr string, db int, password string) *Client {
if addr == "" {
addr = "tcp:127.0.0.1:6379"
}
na := strings.SplitN(addr, ":", 2)
- return &Client{Addr: na[1], Proto: na[0], pool: newConnPool()}
+ return &Client{na[1], na[0], db, password, newConnPool()}
}
// Call is the canonical way of talking to Redis. It accepts any
@@ -106,7 +108,7 @@ func (c *Client) connect() (conn Connection, err error) {
conn = c.pool.pop()
if conn == nil {
- conn, err = NewConn(c.Addr, c.Proto)
+ conn, err = NewConn(c.Addr, c.Proto, c.Db, c.Password)
if err != nil {
return nil, err
@@ -133,9 +135,9 @@ type AsyncClient struct {
// NewAsyncClient expects a addr like "tcp:127.0.0.1:6379"
// It returns a new *Client.
-func NewAsyncClient(addr string) *AsyncClient {
+func NewAsyncClient(addr string, db int, password string) *AsyncClient {
return &AsyncClient{
- NewClient(addr),
+ NewClient(addr, db, password),
bytes.NewBuffer(make([]byte, 0, 1024*16)),
nil,
0,
@@ -158,7 +160,7 @@ func (ac *AsyncClient) Call(args ...interface{}) (err error) {
// Read returns a Reply or error.
func (ac *AsyncClient) Read() (*Reply, error) {
if ac.conn == nil {
- conn, e := NewConn(ac.Addr, ac.Proto)
+ conn, e := NewConn(ac.Addr, ac.Proto, ac.Db, ac.Password)
if e != nil {
return nil, e
View
2 schema/key.go
@@ -1,4 +1,4 @@
-package schema
+package schema
import (
"fmt"
View
1 schema/tag.go
@@ -38,4 +38,3 @@ func (o tagOptions) Contains(optionName string) bool {
}
return false
}
-

0 comments on commit f6900db

Please sign in to comment.