resource_pool.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. package resource_pool
  2. import (
  3. "time"
  4. )
  5. type Options struct {
  6. // The maximum number of active resource handles per resource location. (A
  7. // non-positive value indicates the number of active resource handles is
  8. // unbounded).
  9. MaxActiveHandles int32
  10. // The maximum number of idle resource handles per resource location that
  11. // are kept alive by the resource pool.
  12. MaxIdleHandles uint32
  13. // The maximum amount of time an idle resource handle can remain alive (if
  14. // specified).
  15. MaxIdleTime *time.Duration
  16. // This limits the number of concurrent Open calls (there's no limit when
  17. // OpenMaxConcurrency is non-positive).
  18. OpenMaxConcurrency int
  19. // This function creates a resource handle (e.g., a connection) for a
  20. // resource location. The function must be thread-safe.
  21. Open func(resourceLocation string) (
  22. handle interface{},
  23. err error)
  24. // This function destroys a resource handle and performs the necessary
  25. // cleanup to free up resources. The function must be thread-safe.
  26. Close func(handle interface{}) error
  27. // This specifies the now time function. When the function is non-nil, the
  28. // resource pool will use the specified function instead of time.Now to
  29. // generate the current time.
  30. NowFunc func() time.Time
  31. }
  32. func (o Options) getCurrentTime() time.Time {
  33. if o.NowFunc == nil {
  34. return time.Now()
  35. } else {
  36. return o.NowFunc()
  37. }
  38. }
  39. // A generic interface for managed resource pool. All resource pool
  40. // implementations must be threadsafe.
  41. type ResourcePool interface {
  42. // This returns the number of active resource handles.
  43. NumActive() int32
  44. // This returns the highest number of actives handles for the entire
  45. // lifetime of the pool. If the pool contains multiple sub-pools, the
  46. // high water mark is the max of the sub-pools' high water marks.
  47. ActiveHighWaterMark() int32
  48. // This returns the number of alive idle handles. NOTE: This is only used
  49. // for testing.
  50. NumIdle() int
  51. // This associates a resource location to the resource pool; afterwhich,
  52. // the user can get resource handles for the resource location.
  53. Register(resourceLocation string) error
  54. // This dissociates a resource location from the resource pool; afterwhich,
  55. // the user can no longer get resource handles for the resource location.
  56. // If the given resource location corresponds to a sub-pool, the unregistered
  57. // sub-pool will enter lame duck mode.
  58. Unregister(resourceLocation string) error
  59. // This returns the list of registered resource location entries.
  60. ListRegistered() []string
  61. // This gets an active resource handle from the resource pool. The
  62. // handle will remain active until one of the following is called:
  63. // 1. handle.Release()
  64. // 2. handle.Discard()
  65. // 3. pool.Release(handle)
  66. // 4. pool.Discard(handle)
  67. Get(key string) (ManagedHandle, error)
  68. // This releases an active resource handle back to the resource pool.
  69. Release(handle ManagedHandle) error
  70. // This discards an active resource from the resource pool.
  71. Discard(handle ManagedHandle) error
  72. // Enter the resource pool into lame duck mode. The resource pool
  73. // will no longer return resource handles, and all idle resource handles
  74. // are closed immediately (including active resource handles that are
  75. // released back to the pool afterward).
  76. EnterLameDuckMode()
  77. }