pool.go 2.5 KB

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