managed_connection.go 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. package net2
  2. import (
  3. "fmt"
  4. "net"
  5. "time"
  6. "errors"
  7. "github.com/seaweedfs/seaweedfs/weed/wdclient/resource_pool"
  8. )
  9. // Dial's arguments.
  10. type NetworkAddress struct {
  11. Network string
  12. Address string
  13. }
  14. // A connection managed by a connection pool. NOTE: SetDeadline,
  15. // SetReadDeadline and SetWriteDeadline are disabled for managed connections.
  16. // (The deadlines are set by the connection pool).
  17. type ManagedConn interface {
  18. net.Conn
  19. // This returns the original (network, address) entry used for creating
  20. // the connection.
  21. Key() NetworkAddress
  22. // This returns the underlying net.Conn implementation.
  23. RawConn() net.Conn
  24. // This returns the connection pool which owns this connection.
  25. Owner() ConnectionPool
  26. // This indicates a user is done with the connection and releases the
  27. // connection back to the connection pool.
  28. ReleaseConnection() error
  29. // This indicates the connection is an invalid state, and that the
  30. // connection should be discarded from the connection pool.
  31. DiscardConnection() error
  32. }
  33. // A physical implementation of ManagedConn
  34. type managedConnImpl struct {
  35. addr NetworkAddress
  36. handle resource_pool.ManagedHandle
  37. pool ConnectionPool
  38. options ConnectionOptions
  39. }
  40. // This creates a managed connection wrapper.
  41. func NewManagedConn(
  42. network string,
  43. address string,
  44. handle resource_pool.ManagedHandle,
  45. pool ConnectionPool,
  46. options ConnectionOptions) ManagedConn {
  47. addr := NetworkAddress{
  48. Network: network,
  49. Address: address,
  50. }
  51. return &managedConnImpl{
  52. addr: addr,
  53. handle: handle,
  54. pool: pool,
  55. options: options,
  56. }
  57. }
  58. func (c *managedConnImpl) rawConn() (net.Conn, error) {
  59. h, err := c.handle.Handle()
  60. return h.(net.Conn), err
  61. }
  62. // See ManagedConn for documentation.
  63. func (c *managedConnImpl) RawConn() net.Conn {
  64. h, _ := c.handle.Handle()
  65. return h.(net.Conn)
  66. }
  67. // See ManagedConn for documentation.
  68. func (c *managedConnImpl) Key() NetworkAddress {
  69. return c.addr
  70. }
  71. // See ManagedConn for documentation.
  72. func (c *managedConnImpl) Owner() ConnectionPool {
  73. return c.pool
  74. }
  75. // See ManagedConn for documentation.
  76. func (c *managedConnImpl) ReleaseConnection() error {
  77. return c.handle.Release()
  78. }
  79. // See ManagedConn for documentation.
  80. func (c *managedConnImpl) DiscardConnection() error {
  81. return c.handle.Discard()
  82. }
  83. // See net.Conn for documentation
  84. func (c *managedConnImpl) Read(b []byte) (n int, err error) {
  85. conn, err := c.rawConn()
  86. if err != nil {
  87. return 0, err
  88. }
  89. if c.options.ReadTimeout > 0 {
  90. deadline := c.options.getCurrentTime().Add(c.options.ReadTimeout)
  91. _ = conn.SetReadDeadline(deadline)
  92. }
  93. n, err = conn.Read(b)
  94. if err != nil {
  95. var localAddr string
  96. if conn.LocalAddr() != nil {
  97. localAddr = conn.LocalAddr().String()
  98. } else {
  99. localAddr = "(nil)"
  100. }
  101. var remoteAddr string
  102. if conn.RemoteAddr() != nil {
  103. remoteAddr = conn.RemoteAddr().String()
  104. } else {
  105. remoteAddr = "(nil)"
  106. }
  107. err = fmt.Errorf("Read error from host: %s <-> %s: %v", localAddr, remoteAddr, err)
  108. }
  109. return
  110. }
  111. // See net.Conn for documentation
  112. func (c *managedConnImpl) Write(b []byte) (n int, err error) {
  113. conn, err := c.rawConn()
  114. if err != nil {
  115. return 0, err
  116. }
  117. if c.options.WriteTimeout > 0 {
  118. deadline := c.options.getCurrentTime().Add(c.options.WriteTimeout)
  119. _ = conn.SetWriteDeadline(deadline)
  120. }
  121. n, err = conn.Write(b)
  122. if err != nil {
  123. err = fmt.Errorf("Write error: %v", err)
  124. }
  125. return
  126. }
  127. // See net.Conn for documentation
  128. func (c *managedConnImpl) Close() error {
  129. return c.handle.Discard()
  130. }
  131. // See net.Conn for documentation
  132. func (c *managedConnImpl) LocalAddr() net.Addr {
  133. conn, _ := c.rawConn()
  134. return conn.LocalAddr()
  135. }
  136. // See net.Conn for documentation
  137. func (c *managedConnImpl) RemoteAddr() net.Addr {
  138. conn, _ := c.rawConn()
  139. return conn.RemoteAddr()
  140. }
  141. // SetDeadline is disabled for managed connection (The deadline is set by
  142. // us, with respect to the read/write timeouts specified in ConnectionOptions).
  143. func (c *managedConnImpl) SetDeadline(t time.Time) error {
  144. return errors.New("Cannot set deadline for managed connection")
  145. }
  146. // SetReadDeadline is disabled for managed connection (The deadline is set by
  147. // us with respect to the read timeout specified in ConnectionOptions).
  148. func (c *managedConnImpl) SetReadDeadline(t time.Time) error {
  149. return errors.New("Cannot set read deadline for managed connection")
  150. }
  151. // SetWriteDeadline is disabled for managed connection (The deadline is set by
  152. // us with respect to the write timeout specified in ConnectionOptions).
  153. func (c *managedConnImpl) SetWriteDeadline(t time.Time) error {
  154. return errors.New("Cannot set write deadline for managed connection")
  155. }