pool: return specified size of buffer

This commit is contained in:
nadoo committed 2020-04-19 23:20:15 +08:00
1 parent 5326c0a901
commit a0542a028e
16 files changed
+158 -139

No files matched your search

+3 -2
View File
@@ -1,7 +1,6 @@
package http
import (
"bytes"
"encoding/base64"
"errors"
"net"
@@ -9,6 +8,7 @@ import (
"github.com/nadoo/glider/common/conn"
"github.com/nadoo/glider/common/log"
"github.com/nadoo/glider/common/pool"
"github.com/nadoo/glider/proxy"
)
@@ -33,7 +33,7 @@ func (s *HTTP) Dial(network, addr string) (net.Conn, error) {
return nil, err
}
var buf bytes.Buffer
buf := pool.GetWriteBuffer()
buf.WriteString("CONNECT " + addr + " HTTP/1.1\r\n")
buf.WriteString("Host: " + addr + "\r\n")
buf.WriteString("Proxy-Connection: Keep-Alive\r\n")
@@ -46,6 +46,7 @@ func (s *HTTP) Dial(network, addr string) (net.Conn, error) {
// header ended
buf.WriteString("\r\n")
_, err = rc.Write(buf.Bytes())
pool.PutWriteBuffer(buf)
if err != nil {
return nil, err
}
+6 -6
View File
@@ -5,8 +5,8 @@
package http
import (
"bytes"
"encoding/base64"
"io"
"net/textproto"
"net/url"
"strings"
@@ -81,17 +81,17 @@ func cleanHeaders(header textproto.MIMEHeader) {
header.Del("Upgrade")
}
func writeStartLine(buf *bytes.Buffer, s1, s2, s3 string) {
buf.WriteString(s1 + " " + s2 + " " + s3 + "\r\n")
func writeStartLine(w io.Writer, s1, s2, s3 string) {
w.Write([]byte(s1 + " " + s2 + " " + s3 + "\r\n"))
}
func writeHeaders(buf *bytes.Buffer, header textproto.MIMEHeader) {
func writeHeaders(w io.Writer, header textproto.MIMEHeader) {
for key, values := range header {
for _, v := range values {
buf.WriteString(key + ": " + v + "\r\n")
w.Write([]byte(key + ": " + v + "\r\n"))
}
}
buf.WriteString("\r\n")
w.Write([]byte("\r\n"))
}
func extractUserPass(auth string) (username, password string, ok bool) {
+12 -6
View File
@@ -2,7 +2,6 @@ package http
import (
"bufio"
"bytes"
"fmt"
"io"
"net"
@@ -11,6 +10,7 @@ import (
"github.com/nadoo/glider/common/conn"
"github.com/nadoo/glider/common/log"
"github.com/nadoo/glider/common/pool"
"github.com/nadoo/glider/proxy"
)
@@ -129,7 +129,10 @@ func (s *HTTP) servHTTP(req *request, c *conn.Conn) {
// copy the left request bytes to remote server. eg. length specificed or chunked body.
go func() {
if _, err := c.Reader().Peek(1); err == nil {
io.Copy(rc, c)
b := pool.GetBuffer(conn.TCPBufSize)
io.CopyBuffer(rc, c, b)
pool.PutBuffer(b)
rc.SetDeadline(time.Now())
c.SetDeadline(time.Now())
}
@@ -156,12 +159,15 @@ func (s *HTTP) servHTTP(req *request, c *conn.Conn) {
header.Set("Proxy-Connection", "close")
header.Set("Connection", "close")
var buf bytes.Buffer
writeStartLine(&buf, proto, code, status)
writeHeaders(&buf, header)
buf := pool.GetWriteBuffer()
writeStartLine(buf, proto, code, status)
writeHeaders(buf, header)
log.F("[http] %s <-> %s", c.RemoteAddr(), req.target)
c.Write(buf.Bytes())
pool.PutWriteBuffer(buf)
io.Copy(c, r)
b := pool.GetBuffer(conn.TCPBufSize)
io.CopyBuffer(c, r, b)
pool.PutBuffer(b)
}