Browse Source

Improve pool documentation

Gary Burd 9 years ago
parent
commit
0708def8b0
1 changed files with 51 additions and 32 deletions
  1. 51 32
      redis/pool.go

+ 51 - 32
redis/pool.go

@@ -46,44 +46,26 @@ var (
 //
 //
 // 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 using a global variable. The pool configuration used here
-// is an example, not a recommendation.
+// request handlers using a package level variable. The pool configuration used
+// here is an example, not a recommendation.
 //
 //
-//  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 {
-//              if time.Since(t) < time.Minute {
-//                  return nil
-//              }
-//              _, err := c.Do("PING")
-//              return err
-//          },
-//      }
+//  func newPool(addr string) *redis.Pool {
+//    return &redis.Pool{
+//      MaxIdle: 3,
+//      IdleTimeout: 240 * time.Second,
+//      Dial: func () (redis.Conn, error) { return redis.Dial("tcp", addr) },
+//    }
 //  }
 //  }
 //
 //
 //  var (
 //  var (
-//      pool *redis.Pool
-//      redisServer = flag.String("redisServer", ":6379", "")
-//      redisPassword = flag.String("redisPassword", "", "")
+//    pool *redis.Pool
+//    redisServer = flag.String("redisServer", ":6379", "")
 //  )
 //  )
 //
 //
 //  func main() {
 //  func main() {
-//      flag.Parse()
-//      pool = newPool(*redisServer, *redisPassword)
-//      ...
+//    flag.Parse()
+//    pool = newPool(*redisServer)
+//    ...
 //  }
 //  }
 //
 //
 // 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
@@ -92,7 +74,44 @@ var (
 //  func serveHome(w http.ResponseWriter, r *http.Request) {
 //  func serveHome(w http.ResponseWriter, r *http.Request) {
 //      conn := pool.Get()
 //      conn := pool.Get()
 //      defer conn.Close()
 //      defer conn.Close()
-//      ....
+//      ...
+//  }
+//
+// Use the Dial function to authenticate connections with the AUTH command or
+// select a database with the SELECT command:
+//
+//  pool := &redis.Pool{
+//    // Other pool configuration not shown in this example.
+//    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
+//      }
+//      if _, err := c.Do("SELECT", db); err != nil {
+//        c.Close()
+//        return nil, err
+//      }
+//      return c, nil
+//    }
+//  }
+//
+// Use the TestOnBorrow function to check the health of an idle connection
+// before the connection is returned to the application. This example PINGs
+// connections that have been idle more than a minute:
+//
+//  pool := &redis.Pool{
+//    // Other pool configuration not shown in this example.
+//    TestOnBorrow: func(c redis.Conn, t time.Time) error {
+//      if time.Since(t) < time.Minute {
+//        return nil
+//      }
+//      _, err := c.Do("PING")
+//      return err
+//    },
 //  }
 //  }
 //
 //
 type Pool struct {
 type Pool struct {