pool.go 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. // Copyright 2012 Gary Burd
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License"): you may
  4. // not use this file except in compliance with the License. You may obtain
  5. // a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  11. // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  12. // License for the specific language governing permissions and limitations
  13. // under the License.
  14. package redis
  15. // Pool maintains a pool of connections.
  16. //
  17. // The following example shows how to use a pool in a web application. The
  18. // application creates a pool at application startup and makes it available to
  19. // request handlers, possibly using a global variable:
  20. //
  21. // var server string // host:port of server
  22. // var password string
  23. // ...
  24. //
  25. // pool = redis.NewPool(func () (c redis.Conn, err error) {
  26. // c, err = redis.Dial(server)
  27. // if err != nil {
  28. // err = c.Do("AUTH", password)
  29. // }
  30. // return
  31. // }, 3)
  32. //
  33. // This pool has a maximum of three connections to the server specified by the
  34. // variable "server". Each connection is authenticated using a password.
  35. //
  36. // A request handler gets a connection from the pool and closes the connection
  37. // when the handler is done:
  38. //
  39. // conn, err := pool.Get()
  40. // if err != nil {
  41. // // handle the error
  42. // }
  43. // defer conn.Close()
  44. // // do something with the connection
  45. //
  46. // Close() returns the connection to the pool if there's room in the pool and
  47. // the connection does not have a permanent error. Otherwise, Close() releases
  48. // the resources used by the connection.
  49. type Pool struct {
  50. newFn func() (Conn, error)
  51. conns chan Conn
  52. }
  53. type pooledConnection struct {
  54. Conn
  55. pool *Pool
  56. }
  57. // NewPool returns a new connection pool. The pool uses newFn to create
  58. // connections as needed and maintains a maximum of maxIdle idle connections.
  59. func NewPool(newFn func() (Conn, error), maxIdle int) *Pool {
  60. return &Pool{newFn: newFn, conns: make(chan Conn, maxIdle)}
  61. }
  62. // Get returns an idle connection from the pool if available or creates a new
  63. // connection. The caller should Close() the connection to return the
  64. // connection to the pool.
  65. func (p *Pool) Get() (Conn, error) {
  66. var c Conn
  67. select {
  68. case c = <-p.conns:
  69. default:
  70. var err error
  71. c, err = p.newFn()
  72. if err != nil {
  73. return nil, err
  74. }
  75. }
  76. return &pooledConnection{Conn: c, pool: p}, nil
  77. }
  78. func (c *pooledConnection) Close() error {
  79. if c.Conn == nil {
  80. return nil
  81. }
  82. if c.Err() != nil {
  83. return nil
  84. }
  85. select {
  86. case c.pool.conns <- c.Conn:
  87. default:
  88. c.Conn.Close()
  89. }
  90. c.Conn = nil
  91. return nil
  92. }