pool.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. package internal
  2. import (
  3. "net"
  4. "sync"
  5. "time"
  6. "v2ray.com/core/common/signal"
  7. )
  8. // ConnectionRecyler is the interface for recycling connections.
  9. type ConnectionRecyler interface {
  10. // Put returns a connection back to a connection pool.
  11. Put(ConnectionID, net.Conn)
  12. }
  13. type NoOpConnectionRecyler struct{}
  14. func (NoOpConnectionRecyler) Put(ConnectionID, net.Conn) {}
  15. // ExpiringConnection is a connection that will expire in certain time.
  16. type ExpiringConnection struct {
  17. conn net.Conn
  18. expire time.Time
  19. }
  20. // Expired returns true if the connection has expired.
  21. func (ec *ExpiringConnection) Expired() bool {
  22. return ec.expire.Before(time.Now())
  23. }
  24. // Pool is a connection pool.
  25. type Pool struct {
  26. sync.RWMutex
  27. connsByDest map[ConnectionID][]*ExpiringConnection
  28. cleanupToken *signal.Semaphore
  29. }
  30. // NewConnectionPool creates a new Pool.
  31. func NewConnectionPool() *Pool {
  32. p := &Pool{
  33. connsByDest: make(map[ConnectionID][]*ExpiringConnection),
  34. cleanupToken: signal.NewSemaphore(1),
  35. }
  36. return p
  37. }
  38. // Get returns a connection with matching connection ID. Nil if not found.
  39. func (p *Pool) Get(id ConnectionID) net.Conn {
  40. p.Lock()
  41. defer p.Unlock()
  42. list, found := p.connsByDest[id]
  43. if !found {
  44. return nil
  45. }
  46. connIdx := -1
  47. for idx, conn := range list {
  48. if !conn.Expired() {
  49. connIdx = idx
  50. break
  51. }
  52. }
  53. if connIdx == -1 {
  54. return nil
  55. }
  56. listLen := len(list)
  57. conn := list[connIdx]
  58. if connIdx != listLen-1 {
  59. list[connIdx] = list[listLen-1]
  60. }
  61. list = list[:listLen-1]
  62. p.connsByDest[id] = list
  63. return conn.conn
  64. }
  65. func (p *Pool) isEmpty() bool {
  66. p.RLock()
  67. defer p.RUnlock()
  68. return len(p.connsByDest) == 0
  69. }
  70. func (p *Pool) cleanup() {
  71. defer p.cleanupToken.Signal()
  72. for !p.isEmpty() {
  73. time.Sleep(time.Second * 5)
  74. expiredConns := make([]net.Conn, 0, 16)
  75. p.Lock()
  76. for dest, list := range p.connsByDest {
  77. validConns := make([]*ExpiringConnection, 0, len(list))
  78. for _, conn := range list {
  79. if conn.Expired() {
  80. expiredConns = append(expiredConns, conn.conn)
  81. } else {
  82. validConns = append(validConns, conn)
  83. }
  84. }
  85. if len(validConns) != len(list) {
  86. p.connsByDest[dest] = validConns
  87. }
  88. }
  89. p.Unlock()
  90. for _, conn := range expiredConns {
  91. conn.Close()
  92. }
  93. }
  94. }
  95. // Put implements ConnectionRecyler.Put().
  96. func (p *Pool) Put(id ConnectionID, conn net.Conn) {
  97. expiringConn := &ExpiringConnection{
  98. conn: conn,
  99. expire: time.Now().Add(time.Second * 4),
  100. }
  101. p.Lock()
  102. defer p.Unlock()
  103. list, found := p.connsByDest[id]
  104. if !found {
  105. list = []*ExpiringConnection{expiringConn}
  106. } else {
  107. list = append(list, expiringConn)
  108. }
  109. p.connsByDest[id] = list
  110. select {
  111. case <-p.cleanupToken.Wait():
  112. go p.cleanup()
  113. default:
  114. }
  115. }