Sfoglia il codice sorgente

Improve Strings function doc.

Fixes #97
Gary Burd 11 anni fa
parent
commit
3235c235f3
2 ha cambiato i file con 42 aggiunte e 14 eliminazioni
  1. 39 12
      redis/conn.go
  2. 3 2
      redis/reply.go

+ 39 - 12
redis/conn.go

@@ -53,27 +53,54 @@ type conn struct {
 
 // Dial connects to the Redis server at the given network and address.
 func Dial(network, address string) (Conn, error) {
-	c, err := net.Dial(network, address)
-	if err != nil {
-		return nil, err
-	}
-	return NewConn(c, 0, 0), nil
+	dialer := Dialer{}
+	return dialer.Dial(network, address)
 }
 
 // DialTimeout acts like Dial but takes timeouts for establishing the
 // connection to the server, writing a command and reading a reply.
 func DialTimeout(network, address string, connectTimeout, readTimeout, writeTimeout time.Duration) (Conn, error) {
-	var c net.Conn
-	var err error
-	if connectTimeout > 0 {
-		c, err = net.DialTimeout(network, address, connectTimeout)
-	} else {
-		c, err = net.Dial(network, address)
+	netDialer := net.Dialer{Timeout: connectTimeout}
+	dialer := Dialer{
+		NetDial:      netDialer.Dial,
+		ReadTimeout:  readTimeout,
+		WriteTimeout: writeTimeout,
 	}
+	return dialer.Dial(network, address)
+}
+
+// A Dialer specifies options for connecting to a Redis server.
+type Dialer struct {
+	// NetDial specifies the dial function for creating TCP connections. If
+	// NetDial is nil, then net.Dial is used.
+	NetDial func(network, addr string) (net.Conn, error)
+
+	// ReadTimeout specifies the timeout for reading a single command
+	// reply. If ReadTimeout is zero, then no timeout is used.
+	ReadTimeout time.Duration
+
+	// WriteTimeout specifies the timeout for writing a single command.  If
+	// WriteTimeout is zero, then no timeout is used.
+	WriteTimeout time.Duration
+}
+
+// Dial connects to the Redis server at address on the named network.
+func (d *Dialer) Dial(network, address string) (Conn, error) {
+	dial := d.NetDial
+	if dial == nil {
+		dial = net.Dial
+	}
+	netConn, err := dial(network, address)
 	if err != nil {
 		return nil, err
 	}
-	return NewConn(c, readTimeout, writeTimeout), nil
+	return &conn{
+		conn:         netConn,
+		bw:           bufio.NewWriter(netConn),
+		br:           bufio.NewReader(netConn),
+		readTimeout:  d.ReadTimeout,
+		writeTimeout: d.WriteTimeout,
+	}, nil
 }
 
 // NewConn returns a new Redigo connection for the given net connection.

+ 3 - 2
redis/reply.go

@@ -242,8 +242,9 @@ func Values(reply interface{}, err error) ([]interface{}, error) {
 }
 
 // Strings is a helper that converts an array command reply to a []string. If
-// err is not equal to nil, then Strings returns nil, err. If one of the array
-// items is not a bulk string or nil, then Strings returns an error.
+// err is not equal to nil, then Strings returns nil, err. Nil array items are
+// converted to "" in the output slice. Strings returns an error if an array
+// item is not a bulk string or nil.
 func Strings(reply interface{}, err error) ([]string, error) {
 	if err != nil {
 		return nil, err