pool.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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. 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) cleanup() {
  66. defer p.cleanupToken.Signal()
  67. for len(p.connsByDest) > 0 {
  68. time.Sleep(time.Second * 5)
  69. expiredConns := make([]net.Conn, 0, 16)
  70. p.Lock()
  71. for dest, list := range p.connsByDest {
  72. validConns := make([]*ExpiringConnection, 0, len(list))
  73. for _, conn := range list {
  74. if conn.Expired() {
  75. expiredConns = append(expiredConns, conn.conn)
  76. } else {
  77. validConns = append(validConns, conn)
  78. }
  79. }
  80. if len(validConns) != len(list) {
  81. p.connsByDest[dest] = validConns
  82. }
  83. }
  84. p.Unlock()
  85. for _, conn := range expiredConns {
  86. conn.Close()
  87. }
  88. }
  89. }
  90. // Put implements ConnectionRecyler.Put().
  91. func (p *Pool) Put(id ConnectionID, conn net.Conn) {
  92. expiringConn := &ExpiringConnection{
  93. conn: conn,
  94. expire: time.Now().Add(time.Second * 4),
  95. }
  96. p.Lock()
  97. defer p.Unlock()
  98. list, found := p.connsByDest[id]
  99. if !found {
  100. list = []*ExpiringConnection{expiringConn}
  101. } else {
  102. list = append(list, expiringConn)
  103. }
  104. p.connsByDest[id] = list
  105. select {
  106. case <-p.cleanupToken.Wait():
  107. go p.cleanup()
  108. default:
  109. }
  110. }