瀏覽代碼

Improve pool documentation.

Gary Burd 11 年之前
父節點
當前提交
bd5c6d56f5
共有 1 個文件被更改,包括 39 次插入30 次删除
  1. 39 30
      redis/pool.go

+ 39 - 30
redis/pool.go

@@ -35,41 +35,51 @@ var errPoolClosed = errors.New("redigo: connection pool closed")
 //
 //
 // The following example shows how to use a pool in a web application. The
 // The following example shows how to use a pool in a web application. The
 // application creates a pool at application startup and makes it available to
 // application creates a pool at application startup and makes it available to
-// request handlers, possibly using a global variable:
+// request handlers using a global variable.
 //
 //
-//      var server string           // host:port of server
-//      var password string
-//      ...
+//  func newPool(server, password string) *redis.Pool {
+//      return &redis.Pool{
+//          MaxIdle: 3,
+//          IdleTimeout: 240 * time.Second,
+//          Dial: func () (redis.Conn, error) {
+//              c, err := redis.Dial("tcp", server)
+//              if err != nil {
+//                  return nil, err
+//              }
+//              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
+//          },
+//      }
+//  }
 //
 //
-//      pool = &redis.Pool{
-//              MaxIdle: 3,
-//              IdleTimeout: 240 * time.Second,
-//              Dial: func () (redis.Conn, error) {
-//                  c, err := redis.Dial("tcp", server)
-//                  if err != nil {
-//                      return nil, err
-//                  }
-//                  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
-//			    },
-//          }
+//  var (
+//      pool *redis.Pool
+//      redisServer = flag.String("redisServer", ":6379", "")
+//      redisPassword = flag.String("redisPassword", "", "")
+//  )
 //
 //
-// This pool has a maximum of three connections to the server specified by the
-// variable "server". Each connection is authenticated using a password.
+//  func main() {
+//      floag.Parse()
+//      pool = newPool(*redisServer, *redisPassword)
+//      ...
+//  }
 //
 //
 // A request handler gets a connection from the pool and closes the connection
 // A request handler gets a connection from the pool and closes the connection
 // when the handler is done:
 // when the handler is done:
 //
 //
-//  conn := pool.Get()
-//  defer conn.Close()
-//  // do something with the connection
+//  func serveHome(w http.ResponseWriter, r *http.Request) {
+//      conn := pool.Get()
+//      defer conn.Close()
+//      ....
+//  }
+//
 type Pool struct {
 type Pool struct {
 
 
 	// Dial is an application supplied function for creating new connections.
 	// Dial is an application supplied function for creating new connections.
@@ -108,8 +118,7 @@ type idleConn struct {
 	t time.Time
 	t time.Time
 }
 }
 
 
-// NewPool returns a pool that uses newPool to create connections as needed.
-// The pool keeps a maximum of maxIdle idle connections.
+// NewPool is a convenience function for initializing a pool.
 func NewPool(newFn func() (Conn, error), maxIdle int) *Pool {
 func NewPool(newFn func() (Conn, error), maxIdle int) *Pool {
 	return &Pool{Dial: newFn, MaxIdle: maxIdle}
 	return &Pool{Dial: newFn, MaxIdle: maxIdle}
 }
 }