gtcp_pool.go 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. // Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
  2. //
  3. // This Source Code Form is subject to the terms of the MIT License.
  4. // If a copy of the MIT was not distributed with this file,
  5. // You can obtain one at https://github.com/gogf/gf.
  6. package gtcp
  7. import (
  8. "time"
  9. "github.com/gogf/gf/container/gmap"
  10. "github.com/gogf/gf/container/gpool"
  11. )
  12. // PoolConn is a connection with pool feature for TCP.
  13. // Note that it is NOT a pool or connection manager, it is just a TCP connection object.
  14. type PoolConn struct {
  15. *Conn // Underlying connection object.
  16. pool *gpool.Pool // Connection pool, which is not a real connection pool, but a connection reusable pool.
  17. status int // Status of current connection, which is used to mark this connection usable or not.
  18. }
  19. const (
  20. defaultPoolExpire = 10 * time.Second // Default TTL for connection in the pool.
  21. connStatusUnknown = 0 // Means it is unknown it's connective or not.
  22. connStatusActive = 1 // Means it is now connective.
  23. connStatusError = 2 // Means it should be closed and removed from pool.
  24. )
  25. var (
  26. // addressPoolMap is a mapping for address to its pool object.
  27. addressPoolMap = gmap.NewStrAnyMap(true)
  28. )
  29. // NewPoolConn creates and returns a connection with pool feature.
  30. func NewPoolConn(addr string, timeout ...time.Duration) (*PoolConn, error) {
  31. v := addressPoolMap.GetOrSetFuncLock(addr, func() interface{} {
  32. var pool *gpool.Pool
  33. pool = gpool.New(defaultPoolExpire, func() (interface{}, error) {
  34. if conn, err := NewConn(addr, timeout...); err == nil {
  35. return &PoolConn{conn, pool, connStatusActive}, nil
  36. } else {
  37. return nil, err
  38. }
  39. })
  40. return pool
  41. })
  42. if v, err := v.(*gpool.Pool).Get(); err == nil {
  43. return v.(*PoolConn), nil
  44. } else {
  45. return nil, err
  46. }
  47. }
  48. // Close puts back the connection to the pool if it's active,
  49. // or closes the connection if it's not active.
  50. //
  51. // Note that, if <c> calls Close function closing itself, <c> can not
  52. // be used again.
  53. func (c *PoolConn) Close() error {
  54. if c.pool != nil && c.status == connStatusActive {
  55. c.status = connStatusUnknown
  56. c.pool.Put(c)
  57. } else {
  58. return c.Conn.Close()
  59. }
  60. return nil
  61. }
  62. // Send writes data to the connection. It retrieves a new connection from its pool if it fails
  63. // writing data.
  64. func (c *PoolConn) Send(data []byte, retry ...Retry) error {
  65. err := c.Conn.Send(data, retry...)
  66. if err != nil && c.status == connStatusUnknown {
  67. if v, e := c.pool.Get(); e == nil {
  68. c.Conn = v.(*PoolConn).Conn
  69. err = c.Send(data, retry...)
  70. } else {
  71. err = e
  72. }
  73. }
  74. if err != nil {
  75. c.status = connStatusError
  76. } else {
  77. c.status = connStatusActive
  78. }
  79. return err
  80. }
  81. // Recv receives data from the connection.
  82. func (c *PoolConn) Recv(length int, retry ...Retry) ([]byte, error) {
  83. data, err := c.Conn.Recv(length, retry...)
  84. if err != nil {
  85. c.status = connStatusError
  86. } else {
  87. c.status = connStatusActive
  88. }
  89. return data, err
  90. }
  91. // RecvLine reads data from the connection until reads char '\n'.
  92. // Note that the returned result does not contain the last char '\n'.
  93. func (c *PoolConn) RecvLine(retry ...Retry) ([]byte, error) {
  94. data, err := c.Conn.RecvLine(retry...)
  95. if err != nil {
  96. c.status = connStatusError
  97. } else {
  98. c.status = connStatusActive
  99. }
  100. return data, err
  101. }
  102. // RecvTil reads data from the connection until reads bytes <til>.
  103. // Note that the returned result contains the last bytes <til>.
  104. func (c *PoolConn) RecvTil(til []byte, retry ...Retry) ([]byte, error) {
  105. data, err := c.Conn.RecvTil(til, retry...)
  106. if err != nil {
  107. c.status = connStatusError
  108. } else {
  109. c.status = connStatusActive
  110. }
  111. return data, err
  112. }
  113. // RecvWithTimeout reads data from the connection with timeout.
  114. func (c *PoolConn) RecvWithTimeout(length int, timeout time.Duration, retry ...Retry) (data []byte, err error) {
  115. if err := c.SetreceiveDeadline(time.Now().Add(timeout)); err != nil {
  116. return nil, err
  117. }
  118. defer c.SetreceiveDeadline(time.Time{})
  119. data, err = c.Recv(length, retry...)
  120. return
  121. }
  122. // SendWithTimeout writes data to the connection with timeout.
  123. func (c *PoolConn) SendWithTimeout(data []byte, timeout time.Duration, retry ...Retry) (err error) {
  124. if err := c.SetSendDeadline(time.Now().Add(timeout)); err != nil {
  125. return err
  126. }
  127. defer c.SetSendDeadline(time.Time{})
  128. err = c.Send(data, retry...)
  129. return
  130. }
  131. // SendRecv writes data to the connection and blocks reading response.
  132. func (c *PoolConn) SendRecv(data []byte, receive int, retry ...Retry) ([]byte, error) {
  133. if err := c.Send(data, retry...); err == nil {
  134. return c.Recv(receive, retry...)
  135. } else {
  136. return nil, err
  137. }
  138. }
  139. // SendRecvWithTimeout writes data to the connection and reads response with timeout.
  140. func (c *PoolConn) SendRecvWithTimeout(data []byte, receive int, timeout time.Duration, retry ...Retry) ([]byte, error) {
  141. if err := c.Send(data, retry...); err == nil {
  142. return c.RecvWithTimeout(receive, timeout, retry...)
  143. } else {
  144. return nil, err
  145. }
  146. }