delegate.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. // Copyright 2019 Yunion
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package httputils
  15. import (
  16. "net"
  17. "time"
  18. )
  19. type connDelegate struct {
  20. conn net.Conn
  21. socketTimeout time.Duration
  22. finalTimeout time.Duration
  23. }
  24. func getConnDelegate(conn net.Conn, socketTimeout, finalTimeout time.Duration) *connDelegate {
  25. return &connDelegate{
  26. conn: conn,
  27. socketTimeout: socketTimeout,
  28. finalTimeout: finalTimeout,
  29. }
  30. }
  31. func (delegate *connDelegate) Read(b []byte) (n int, err error) {
  32. delegate.SetReadDeadline(time.Now().Add(delegate.socketTimeout))
  33. n, err = delegate.conn.Read(b)
  34. delegate.SetReadDeadline(time.Now().Add(delegate.finalTimeout))
  35. return n, err
  36. }
  37. func (delegate *connDelegate) Write(b []byte) (n int, err error) {
  38. delegate.SetWriteDeadline(time.Now().Add(delegate.socketTimeout))
  39. n, err = delegate.conn.Write(b)
  40. finalTimeout := time.Now().Add(delegate.finalTimeout)
  41. delegate.SetWriteDeadline(finalTimeout)
  42. delegate.SetReadDeadline(finalTimeout)
  43. return n, err
  44. }
  45. func (delegate *connDelegate) Close() error {
  46. return delegate.conn.Close()
  47. }
  48. func (delegate *connDelegate) LocalAddr() net.Addr {
  49. return delegate.conn.LocalAddr()
  50. }
  51. func (delegate *connDelegate) RemoteAddr() net.Addr {
  52. return delegate.conn.RemoteAddr()
  53. }
  54. // SetDeadline sets the read and write deadlines associated
  55. // with the connection. It is equivalent to calling both
  56. // SetReadDeadline and SetWriteDeadline.
  57. //
  58. // A deadline is an absolute time after which I/O operations
  59. // fail with a timeout (see type Error) instead of
  60. // blocking. The deadline applies to all future and pending
  61. // I/O, not just the immediately following call to Read or
  62. // Write. After a deadline has been exceeded, the connection
  63. // can be refreshed by setting a deadline in the future.
  64. //
  65. // An idle timeout can be implemented by repeatedly extending
  66. // the deadline after successful Read or Write calls.
  67. //
  68. // A zero value for t means I/O operations will not time out.
  69. //
  70. // Note that if a TCP connection has keep-alive turned on,
  71. // which is the default unless overridden by Dialer.KeepAlive
  72. // or ListenConfig.KeepAlive, then a keep-alive failure may
  73. // also return a timeout error. On Unix systems a keep-alive
  74. // failure on I/O can be detected using
  75. // errors.Is(err, syscall.ETIMEDOUT).
  76. func (delegate *connDelegate) SetDeadline(t time.Time) error {
  77. return delegate.conn.SetDeadline(t)
  78. }
  79. // SetReadDeadline sets the deadline for future Read calls
  80. // and any currently-blocked Read call.
  81. // A zero value for t means Read will not time out.
  82. func (delegate *connDelegate) SetReadDeadline(t time.Time) error {
  83. return delegate.conn.SetReadDeadline(t)
  84. }
  85. // SetWriteDeadline sets the deadline for future Write calls
  86. // and any currently-blocked Write call.
  87. // Even if write times out, it may return n > 0, indicating that
  88. // some of the data was successfully written.
  89. // A zero value for t means Write will not time out.
  90. func (delegate *connDelegate) SetWriteDeadline(t time.Time) error {
  91. return delegate.conn.SetWriteDeadline(t)
  92. }