pool.go 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. package trace
  2. import "time"
  3. // PoolTrace is passed into radix.NewPool via radix.PoolWithTrace, and contains
  4. // callbacks which will be triggered for specific events during the Pool's
  5. // runtime.
  6. //
  7. // All callbacks are called synchronously.
  8. type PoolTrace struct {
  9. // ConnCreated is called when the Pool creates a new connection. The
  10. // provided Err indicates whether the connection successfully completed.
  11. ConnCreated func(PoolConnCreated)
  12. // ConnClosed is called before closing the connection.
  13. ConnClosed func(PoolConnClosed)
  14. // DoCompleted is called after command execution. Must consider race condition
  15. // for manipulating variables in DoCompleted callback since DoComplete
  16. // function can be called in many go-routines.
  17. DoCompleted func(PoolDoCompleted)
  18. // InitCompleted is called after pool fills its connections
  19. InitCompleted func(PoolInitCompleted)
  20. }
  21. // PoolCommon contains information which is passed into all Pool-related
  22. // callbacks.
  23. type PoolCommon struct {
  24. // Network and Addr indicate the network/address the Pool was created with
  25. // (useful for differentiating different redis instances in a Cluster).
  26. Network, Addr string
  27. // PoolSize and BufferSize indicate the Pool size and buffer size that the
  28. // Pool was initialized with.
  29. PoolSize, BufferSize int
  30. }
  31. // PoolConnCreatedReason enumerates all the different reasons a connection might
  32. // be created and trigger a ConnCreated trace.
  33. type PoolConnCreatedReason string
  34. // All possible values of PoolConnCreatedReason.
  35. const (
  36. // PoolConnCreatedReasonInitialization indicates a connection was being
  37. // created during initialization of the Pool (i.e. within NewPool).
  38. PoolConnCreatedReasonInitialization PoolConnCreatedReason = "initialization"
  39. // PoolConnCreatedReasonRefill indicates a connection was being created
  40. // during a refill event (see radix.PoolRefillInterval).
  41. PoolConnCreatedReasonRefill PoolConnCreatedReason = "refill"
  42. // PoolConnCreatedReasonPoolEmpty indicates a connection was being created
  43. // because the Pool was empty and an Action requires one. See the
  44. // radix.PoolOnEmpty options.
  45. PoolConnCreatedReasonPoolEmpty PoolConnCreatedReason = "pool empty"
  46. )
  47. // PoolConnCreated is passed into the PoolTrace.ConnCreated callback whenever
  48. // the Pool creates a new connection.
  49. type PoolConnCreated struct {
  50. PoolCommon
  51. // The reason the connection was created.
  52. Reason PoolConnCreatedReason
  53. // How long it took to create the connection.
  54. ConnectTime time.Duration
  55. // If connection creation failed, this is the error it failed with.
  56. Err error
  57. }
  58. // PoolConnClosedReason enumerates all the different reasons a connection might
  59. // be closed and trigger a ConnClosed trace.
  60. type PoolConnClosedReason string
  61. // All possible values of PoolConnClosedReason.
  62. const (
  63. // PoolConnClosedReasonPoolClosed indicates a connection was closed because
  64. // the Close method was called on Pool.
  65. PoolConnClosedReasonPoolClosed PoolConnClosedReason = "pool closed"
  66. // PoolConnClosedReasonBufferDrain indicates a connection was closed due to
  67. // a buffer drain event. See radix.PoolOnFullBuffer.
  68. PoolConnClosedReasonBufferDrain PoolConnClosedReason = "buffer drained"
  69. // PoolConnClosedReasonPoolFull indicates a connection was closed due to
  70. // the Pool already being full. See The radix.PoolOnFullClose options.
  71. PoolConnClosedReasonPoolFull PoolConnClosedReason = "pool full"
  72. // PoolConnClosedReasonConnExpired indicates a connection was closed because
  73. // the connection was expired. See The radix.PoolMaxLifetime options.
  74. PoolConnClosedReasonConnExpired PoolConnClosedReason = "conn expired"
  75. )
  76. // PoolConnClosed is passed into the PoolTrace.ConnClosed callback whenever the
  77. // Pool closes a connection.
  78. type PoolConnClosed struct {
  79. PoolCommon
  80. // AvailCount indicates the total number of connections the Pool is holding
  81. // on to which are available for usage at the moment the trace occurs.
  82. AvailCount int
  83. // The reason the connection was closed.
  84. Reason PoolConnClosedReason
  85. }
  86. // PoolDoCompleted is passed into the PoolTrace.DoCompleted callback whenever Pool finished to run
  87. // Do function.
  88. type PoolDoCompleted struct {
  89. PoolCommon
  90. // AvailCount indicates the total number of connections the Pool is holding
  91. // on to which are available for usage at the moment the trace occurs.
  92. AvailCount int
  93. // How long it took to send command.
  94. ElapsedTime time.Duration
  95. // This is the error returned from redis.
  96. Err error
  97. }
  98. // PoolInitCompleted is passed into the PoolTrace.InitCompleted callback whenever Pool initialized.
  99. // This must be called once.
  100. type PoolInitCompleted struct {
  101. PoolCommon
  102. // AvailCount indicates the total number of connections the Pool is holding
  103. // on to which are available for usage at the moment the trace occurs.
  104. AvailCount int
  105. // How long it took to fill all connections.
  106. ElapsedTime time.Duration
  107. }