connection_pool.go 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. package net2
  2. import (
  3. "net"
  4. "time"
  5. )
  6. type ConnectionOptions struct {
  7. // The maximum number of connections that can be active per host at any
  8. // given time (A non-positive value indicates the number of connections
  9. // is unbounded).
  10. MaxActiveConnections int32
  11. // The maximum number of idle connections per host that are kept alive by
  12. // the connection pool.
  13. MaxIdleConnections uint32
  14. // The maximum amount of time an idle connection can alive (if specified).
  15. MaxIdleTime *time.Duration
  16. // This limits the number of concurrent Dial calls (there's no limit when
  17. // DialMaxConcurrency is non-positive).
  18. DialMaxConcurrency int
  19. // Dial specifies the dial function for creating network connections.
  20. // If Dial is nil, net.DialTimeout is used, with timeout set to 1 second.
  21. Dial func(network string, address string) (net.Conn, error)
  22. // This specifies the now time function. When the function is non-nil, the
  23. // connection pool will use the specified function instead of time.Now to
  24. // generate the current time.
  25. NowFunc func() time.Time
  26. // This specifies the timeout for any Read() operation.
  27. // Note that setting this to 0 (i.e. not setting it) will make
  28. // read operations block indefinitely.
  29. ReadTimeout time.Duration
  30. // This specifies the timeout for any Write() operation.
  31. // Note that setting this to 0 (i.e. not setting it) will make
  32. // write operations block indefinitely.
  33. WriteTimeout time.Duration
  34. }
  35. func (o ConnectionOptions) getCurrentTime() time.Time {
  36. if o.NowFunc == nil {
  37. return time.Now()
  38. } else {
  39. return o.NowFunc()
  40. }
  41. }
  42. // A generic interface for managed connection pool. All connection pool
  43. // implementations must be threadsafe.
  44. type ConnectionPool interface {
  45. // This returns the number of active connections that are on loan.
  46. NumActive() int32
  47. // This returns the highest number of active connections for the entire
  48. // lifetime of the pool.
  49. ActiveHighWaterMark() int32
  50. // This returns the number of idle connections that are in the pool.
  51. NumIdle() int
  52. // This associates (network, address) to the connection pool; afterwhich,
  53. // the user can get connections to (network, address).
  54. Register(network string, address string) error
  55. // This dissociate (network, address) from the connection pool;
  56. // afterwhich, the user can no longer get connections to
  57. // (network, address).
  58. Unregister(network string, address string) error
  59. // This returns the list of registered (network, address) entries.
  60. ListRegistered() []NetworkAddress
  61. // This gets an active connection from the connection pool. The connection
  62. // will remain active until one of the following is called:
  63. // 1. conn.ReleaseConnection()
  64. // 2. conn.DiscardConnection()
  65. // 3. pool.Release(conn)
  66. // 4. pool.Discard(conn)
  67. Get(network string, address string) (ManagedConn, error)
  68. // This releases an active connection back to the connection pool.
  69. Release(conn ManagedConn) error
  70. // This discards an active connection from the connection pool.
  71. Discard(conn ManagedConn) error
  72. // Enter the connection pool into lame duck mode. The connection pool
  73. // will no longer return connections, and all idle connections are closed
  74. // immediately (including active connections that are released back to the
  75. // pool afterward).
  76. EnterLameDuckMode()
  77. }