mirror of
https://github.com/gospider007/requests.git
synced 2025-12-24 13:57:52 +08:00
修复内存泄漏
This commit is contained in:
@@ -3,6 +3,7 @@ package requests
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"net"
|
||||
"strings"
|
||||
@@ -319,13 +320,7 @@ func (obj *Client) request(ctx context.Context, option *RequestOption) (response
|
||||
if option.Referer != "" {
|
||||
reqs.Header.Set("Referer", option.Referer)
|
||||
} else if reqs.URL.Scheme != "" && reqs.URL.Host != "" {
|
||||
referBuild := builderPool.Get().(*strings.Builder)
|
||||
referBuild.WriteString(reqs.URL.Scheme)
|
||||
referBuild.WriteString("://")
|
||||
referBuild.WriteString(reqs.URL.Host)
|
||||
reqs.Header.Set("Referer", referBuild.String())
|
||||
referBuild.Reset()
|
||||
builderPool.Put(referBuild)
|
||||
reqs.Header.Set("Referer", fmt.Sprintf("%s://%s", reqs.URL.Scheme, reqs.URL.Host))
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -271,11 +271,7 @@ func (obj *Response) ReadBody() (err error) {
|
||||
return errors.New("already read body")
|
||||
}
|
||||
obj.readBody = true
|
||||
bBody := bufferPool.Get().(*bytes.Buffer)
|
||||
defer func() {
|
||||
bBody.Reset()
|
||||
bufferPool.Put(bBody)
|
||||
}()
|
||||
bBody := bytes.NewBuffer(nil)
|
||||
if obj.bar && obj.ContentLength() > 0 {
|
||||
_, err = io.Copy(&barBody{
|
||||
bar: bar.NewClient(obj.response.ContentLength),
|
||||
|
||||
@@ -5,6 +5,7 @@ import (
|
||||
"context"
|
||||
"crypto/tls"
|
||||
"errors"
|
||||
"fmt"
|
||||
"net"
|
||||
"net/textproto"
|
||||
"net/url"
|
||||
@@ -35,14 +36,7 @@ func (obj *reqTask) inPool() bool {
|
||||
}
|
||||
|
||||
func getKey(ctxData *reqCtxData, req *http.Request) (key string) {
|
||||
b := builderPool.Get().(*strings.Builder)
|
||||
b.WriteString(getAddr(ctxData.proxy))
|
||||
b.WriteString("@")
|
||||
b.WriteString(getAddr(req.URL))
|
||||
key = b.String()
|
||||
b.Reset()
|
||||
builderPool.Put(b)
|
||||
return
|
||||
return fmt.Sprintf("%s@%s", getAddr(ctxData.proxy), getAddr(req.URL))
|
||||
}
|
||||
|
||||
type roundTripper struct {
|
||||
|
||||
54
tools.go
54
tools.go
@@ -2,7 +2,6 @@ package requests
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"bytes"
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
@@ -13,7 +12,6 @@ import (
|
||||
"net/url"
|
||||
"strconv"
|
||||
"strings"
|
||||
"sync"
|
||||
_ "unsafe"
|
||||
|
||||
"github.com/gospider007/ja3"
|
||||
@@ -43,18 +41,12 @@ func getAddr(uurl *url.URL) (addr string) {
|
||||
}
|
||||
_, port, _ := net.SplitHostPort(uurl.Host)
|
||||
if port == "" {
|
||||
bs := builderPool.Get().(*strings.Builder)
|
||||
bs.WriteString(uurl.Host)
|
||||
bs.WriteString(":")
|
||||
if uurl.Scheme == "https" {
|
||||
bs.WriteString("443")
|
||||
port = "443"
|
||||
} else {
|
||||
bs.WriteString("80")
|
||||
port = "80"
|
||||
}
|
||||
addr = bs.String()
|
||||
bs.Reset()
|
||||
builderPool.Put(bs)
|
||||
return
|
||||
return fmt.Sprintf("%s:%s", uurl.Host, port)
|
||||
}
|
||||
return uurl.Host
|
||||
}
|
||||
@@ -131,18 +123,7 @@ func httpWrite(r *http.Request, w *bufio.Writer, orderHeaders []string) (err err
|
||||
if r.Header.Get("Content-Length") == "" && shouldSendContentLength(r) {
|
||||
r.Header.Set("Content-Length", fmt.Sprint(r.ContentLength))
|
||||
}
|
||||
bs := builderPool.Get().(*strings.Builder)
|
||||
defer func() {
|
||||
bs.Reset()
|
||||
builderPool.Put(bs)
|
||||
}()
|
||||
bs.WriteString(r.Method)
|
||||
bs.WriteString(" ")
|
||||
bs.WriteString(ruri)
|
||||
bs.WriteString(" ")
|
||||
bs.WriteString(r.Proto)
|
||||
bs.WriteString("\r\n")
|
||||
if _, err = w.WriteString(bs.String()); err != nil {
|
||||
if _, err = w.WriteString(fmt.Sprintf("%s %s %s\r\n", r.Method, ruri, r.Proto)); err != nil {
|
||||
return err
|
||||
}
|
||||
for _, k := range orderHeaders {
|
||||
@@ -154,12 +135,7 @@ func httpWrite(r *http.Request, w *bufio.Writer, orderHeaders []string) (err err
|
||||
continue
|
||||
}
|
||||
for _, v := range vs {
|
||||
bs.Reset()
|
||||
bs.WriteString(k)
|
||||
bs.WriteString(": ")
|
||||
bs.WriteString(v)
|
||||
bs.WriteString("\r\n")
|
||||
if _, err = w.WriteString(bs.String()); err != nil {
|
||||
if _, err = w.WriteString(fmt.Sprintf("%s: %s\r\n", k, v)); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
@@ -174,13 +150,7 @@ func httpWrite(r *http.Request, w *bufio.Writer, orderHeaders []string) (err err
|
||||
continue
|
||||
}
|
||||
for _, v := range vs {
|
||||
|
||||
bs.Reset()
|
||||
bs.WriteString(k)
|
||||
bs.WriteString(": ")
|
||||
bs.WriteString(v)
|
||||
bs.WriteString("\r\n")
|
||||
if _, err = w.WriteString(bs.String()); err != nil {
|
||||
if _, err = w.WriteString(fmt.Sprintf("%s: %s\r\n", k, v)); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
@@ -197,18 +167,6 @@ func httpWrite(r *http.Request, w *bufio.Writer, orderHeaders []string) (err err
|
||||
return w.Flush()
|
||||
}
|
||||
|
||||
var bufferPool sync.Pool
|
||||
var builderPool sync.Pool
|
||||
|
||||
func init() {
|
||||
bufferPool.New = func() interface{} {
|
||||
return bytes.NewBuffer(nil)
|
||||
}
|
||||
builderPool.New = func() interface{} {
|
||||
return &strings.Builder{}
|
||||
}
|
||||
}
|
||||
|
||||
func newRequestWithContext(ctx context.Context, method string, u *url.URL, body io.Reader) (*http.Request, error) {
|
||||
req := (&http.Request{}).WithContext(ctx)
|
||||
if method == "" {
|
||||
|
||||
Reference in New Issue
Block a user