pool.go 2.8 KB

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