Skip to content

Commit

Permalink
add password and db to client
Browse files Browse the repository at this point in the history
  • Loading branch information
Simon Zimmermann committed May 7, 2012
1 parent b81da29 commit f6900db
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 14 deletions.
36 changes: 33 additions & 3 deletions exp/connection.go
Expand Up @@ -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
Expand Down
20 changes: 11 additions & 9 deletions exp/godis.go
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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,
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion schema/key.go
@@ -1,4 +1,4 @@
package schema
package schema

import (
"fmt"
Expand Down
1 change: 0 additions & 1 deletion schema/tag.go
Expand Up @@ -38,4 +38,3 @@ func (o tagOptions) Contains(optionName string) bool {
}
return false
}

0 comments on commit f6900db

Please sign in to comment.