github.com/gospider007/requests@v0.0.0-20240506025355-c73d46169a23/rw.go (about)

     1  package requests
     2  
     3  import (
     4  	"errors"
     5  	"io"
     6  )
     7  
     8  type readWriteCloser struct {
     9  	body io.ReadCloser
    10  	conn *connecotr
    11  }
    12  
    13  func (obj *readWriteCloser) Conn() *connecotr {
    14  	return obj.conn
    15  }
    16  func (obj *readWriteCloser) Read(p []byte) (n int, err error) {
    17  	return obj.body.Read(p)
    18  }
    19  func (obj *readWriteCloser) InPool() bool {
    20  	return obj.conn.inPool
    21  }
    22  func (obj *readWriteCloser) Proxy() string {
    23  	return obj.conn.proxy
    24  }
    25  
    26  var ErrgospiderBodyClose = errors.New("gospider body close error")
    27  
    28  func (obj *readWriteCloser) Close() (err error) {
    29  	if !obj.InPool() {
    30  		obj.ForceCloseConn()
    31  	} else {
    32  		err = obj.body.Close() //reuse conn
    33  		obj.conn.bodyCnl(ErrgospiderBodyClose)
    34  	}
    35  	return
    36  }
    37  
    38  // safe close conn
    39  func (obj *readWriteCloser) CloseConn() {
    40  	if !obj.InPool() {
    41  		obj.ForceCloseConn()
    42  	} else {
    43  		obj.conn.bodyCnl(errors.New("readWriterCloser close conn"))
    44  		obj.conn.closeCnl(errors.New("readWriterCloser close conn"))
    45  	}
    46  }
    47  
    48  // force close conn
    49  func (obj *readWriteCloser) ForceCloseConn() {
    50  	obj.conn.Close()
    51  }