net_timeout.go 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. package util
  2. import (
  3. "net"
  4. "time"
  5. "github.com/chrislusf/seaweedfs/weed/stats"
  6. )
  7. // Listener wraps a net.Listener, and gives a place to store the timeout
  8. // parameters. On Accept, it will wrap the net.Conn with our own Conn for us.
  9. type Listener struct {
  10. net.Listener
  11. ReadTimeout time.Duration
  12. WriteTimeout time.Duration
  13. }
  14. func (l *Listener) Accept() (net.Conn, error) {
  15. c, err := l.Listener.Accept()
  16. if err != nil {
  17. return nil, err
  18. }
  19. stats.ConnectionOpen()
  20. tc := &Conn{
  21. Conn: c,
  22. ReadTimeout: l.ReadTimeout,
  23. WriteTimeout: l.WriteTimeout,
  24. }
  25. return tc, nil
  26. }
  27. // Conn wraps a net.Conn, and sets a deadline for every read
  28. // and write operation.
  29. type Conn struct {
  30. net.Conn
  31. ReadTimeout time.Duration
  32. WriteTimeout time.Duration
  33. isClosed bool
  34. }
  35. func (c *Conn) Read(b []byte) (count int, e error) {
  36. if c.ReadTimeout != 0 {
  37. err := c.Conn.SetReadDeadline(time.Now().Add(c.ReadTimeout))
  38. if err != nil {
  39. return 0, err
  40. }
  41. }
  42. count, e = c.Conn.Read(b)
  43. if e == nil {
  44. stats.BytesIn(int64(count))
  45. }
  46. return
  47. }
  48. func (c *Conn) Write(b []byte) (count int, e error) {
  49. if c.WriteTimeout != 0 {
  50. // minimum 4KB/s
  51. err := c.Conn.SetWriteDeadline(time.Now().Add(c.WriteTimeout * time.Duration(len(b)/40000+1)))
  52. if err != nil {
  53. return 0, err
  54. }
  55. }
  56. count, e = c.Conn.Write(b)
  57. if e == nil {
  58. stats.BytesOut(int64(count))
  59. }
  60. return
  61. }
  62. func (c *Conn) Close() error {
  63. err := c.Conn.Close()
  64. if err == nil {
  65. if !c.isClosed {
  66. stats.ConnectionClose()
  67. c.isClosed = true
  68. }
  69. }
  70. return err
  71. }
  72. func NewListener(addr string, timeout time.Duration) (net.Listener, error) {
  73. l, err := net.Listen("tcp", addr)
  74. if err != nil {
  75. return nil, err
  76. }
  77. tl := &Listener{
  78. Listener: l,
  79. ReadTimeout: timeout,
  80. WriteTimeout: timeout,
  81. }
  82. return tl, nil
  83. }