pool.go 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. package proxy
  2. import (
  3. "sync"
  4. "time"
  5. )
  6. type ConnPool interface {
  7. initAutoFill(async bool) (err error)
  8. Get() (conn any, err error)
  9. Put(conn any)
  10. ReleaseAll()
  11. Len() (length int)
  12. }
  13. type netPool struct {
  14. connects chan any
  15. lock *sync.Mutex
  16. config poolConfig
  17. }
  18. type poolConfig struct {
  19. Factory func() (any, error)
  20. IsActive func(any) bool
  21. Release func(any)
  22. InitialCap int
  23. MaxCap int
  24. }
  25. func NewConnPool(poolConfig poolConfig) (pool ConnPool, err error) {
  26. p := netPool{
  27. config: poolConfig,
  28. connects: make(chan any, poolConfig.MaxCap),
  29. lock: &sync.Mutex{},
  30. }
  31. if poolConfig.MaxCap > 0 {
  32. err = p.initAutoFill(false)
  33. if err == nil {
  34. _ = p.initAutoFill(true)
  35. }
  36. }
  37. return &p, nil
  38. }
  39. func (p *netPool) initAutoFill(async bool) (err error) {
  40. var worker = func() (err error) {
  41. for {
  42. if p.Len() <= p.config.InitialCap/2 {
  43. p.lock.Lock()
  44. errN := 0
  45. for i := 0; i < p.config.InitialCap; i++ {
  46. c, factoryErr := p.config.Factory()
  47. if factoryErr != nil {
  48. errN++
  49. if async {
  50. continue
  51. } else {
  52. p.lock.Unlock()
  53. return factoryErr
  54. }
  55. }
  56. select {
  57. case p.connects <- c:
  58. default:
  59. p.config.Release(c)
  60. break
  61. }
  62. if p.Len() >= p.config.InitialCap {
  63. break
  64. }
  65. }
  66. if errN > 0 {
  67. logger.Sugar().Infof("fill conn pool fail , ERRN:%d", errN)
  68. }
  69. p.lock.Unlock()
  70. }
  71. if !async {
  72. return
  73. }
  74. time.Sleep(time.Second * 2)
  75. }
  76. }
  77. if async {
  78. go func() {
  79. _ = worker()
  80. }()
  81. } else {
  82. err = worker()
  83. }
  84. return
  85. }
  86. func (p *netPool) Get() (conn any, err error) {
  87. p.lock.Lock()
  88. defer p.lock.Unlock()
  89. select {
  90. case conn = <-p.connects:
  91. if p.config.IsActive(conn) {
  92. return
  93. }
  94. p.config.Release(conn)
  95. default:
  96. conn, err = p.config.Factory()
  97. if err != nil {
  98. return nil, err
  99. }
  100. return conn, nil
  101. }
  102. return
  103. }
  104. func (p *netPool) Put(conn any) {
  105. if conn == nil {
  106. return
  107. }
  108. p.lock.Lock()
  109. defer p.lock.Unlock()
  110. if !p.config.IsActive(conn) {
  111. p.config.Release(conn)
  112. }
  113. select {
  114. case p.connects <- conn:
  115. default:
  116. p.config.Release(conn)
  117. }
  118. }
  119. func (p *netPool) ReleaseAll() {
  120. p.lock.Lock()
  121. defer p.lock.Unlock()
  122. close(p.connects)
  123. for c := range p.connects {
  124. p.config.Release(c)
  125. }
  126. p.connects = make(chan any, p.config.InitialCap)
  127. }
  128. func (p *netPool) Len() (length int) {
  129. return len(p.connects)
  130. }