pool.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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. // ExpiringConnection is a connection that will expire in certain time.
  14. type ExpiringConnection struct {
  15. conn net.Conn
  16. expire time.Time
  17. }
  18. // Expired returns true if the connection has expired.
  19. func (ec *ExpiringConnection) Expired() bool {
  20. return ec.expire.Before(time.Now())
  21. }
  22. // Pool is a connection pool.
  23. type Pool struct {
  24. sync.Mutex
  25. connsByDest map[ConnectionID][]*ExpiringConnection
  26. cleanupOnce signal.Once
  27. }
  28. // NewConnectionPool creates a new Pool.
  29. func NewConnectionPool() *Pool {
  30. return &Pool{
  31. connsByDest: make(map[ConnectionID][]*ExpiringConnection),
  32. }
  33. }
  34. // Get returns a connection with matching connection ID. Nil if not found.
  35. func (p *Pool) Get(id ConnectionID) net.Conn {
  36. p.Lock()
  37. defer p.Unlock()
  38. list, found := p.connsByDest[id]
  39. if !found {
  40. return nil
  41. }
  42. connIdx := -1
  43. for idx, conn := range list {
  44. if !conn.Expired() {
  45. connIdx = idx
  46. break
  47. }
  48. }
  49. if connIdx == -1 {
  50. return nil
  51. }
  52. listLen := len(list)
  53. conn := list[connIdx]
  54. if connIdx != listLen-1 {
  55. list[connIdx] = list[listLen-1]
  56. }
  57. list = list[:listLen-1]
  58. p.connsByDest[id] = list
  59. return conn.conn
  60. }
  61. func (p *Pool) cleanup() {
  62. defer p.cleanupOnce.Reset()
  63. for len(p.connsByDest) > 0 {
  64. time.Sleep(time.Second * 5)
  65. expiredConns := make([]net.Conn, 0, 16)
  66. p.Lock()
  67. for dest, list := range p.connsByDest {
  68. validConns := make([]*ExpiringConnection, 0, len(list))
  69. for _, conn := range list {
  70. if conn.Expired() {
  71. expiredConns = append(expiredConns, conn.conn)
  72. } else {
  73. validConns = append(validConns, conn)
  74. }
  75. }
  76. if len(validConns) != len(list) {
  77. p.connsByDest[dest] = validConns
  78. }
  79. }
  80. p.Unlock()
  81. for _, conn := range expiredConns {
  82. conn.Close()
  83. }
  84. }
  85. }
  86. // Put implements ConnectionRecyler.Put().
  87. func (p *Pool) Put(id ConnectionID, conn net.Conn) {
  88. expiringConn := &ExpiringConnection{
  89. conn: conn,
  90. expire: time.Now().Add(time.Second * 4),
  91. }
  92. p.Lock()
  93. defer p.Unlock()
  94. list, found := p.connsByDest[id]
  95. if !found {
  96. list = []*ExpiringConnection{expiringConn}
  97. } else {
  98. list = append(list, expiringConn)
  99. }
  100. p.connsByDest[id] = list
  101. p.cleanupOnce.Do(func() {
  102. go p.cleanup()
  103. })
  104. }