优化代码

This commit is contained in:
刘祥超
2024-05-03 15:31:40 +08:00
parent 5eb122f0a3
commit ef1626de53
14 changed files with 117 additions and 111 deletions

View File

@@ -16,7 +16,7 @@ import (
"github.com/TeaOSLab/EdgeNode/internal/goman"
"github.com/TeaOSLab/EdgeNode/internal/remotelogs"
"github.com/TeaOSLab/EdgeNode/internal/rpc"
"github.com/TeaOSLab/EdgeNode/internal/utils"
"github.com/TeaOSLab/EdgeNode/internal/utils/bytepool"
connutils "github.com/TeaOSLab/EdgeNode/internal/utils/conns"
"github.com/iwind/TeaGo/Tea"
"io"
@@ -246,9 +246,9 @@ func (this *HTTPCacheTaskManager) fetchKey(key *pb.HTTPCacheTaskKey) error {
}
// 读取内容,以便于生成缓存
var buf = utils.BytePool16k.Get()
var buf = bytepool.Pool16k.Get()
_, err = io.CopyBuffer(io.Discard, resp.Body, buf.Bytes)
utils.BytePool16k.Put(buf)
bytepool.Pool16k.Put(buf)
if err != nil {
if err != io.EOF {
err = this.simplifyErr(err)

View File

@@ -13,6 +13,7 @@ import (
"github.com/TeaOSLab/EdgeNode/internal/metrics"
"github.com/TeaOSLab/EdgeNode/internal/stats"
"github.com/TeaOSLab/EdgeNode/internal/utils"
"github.com/TeaOSLab/EdgeNode/internal/utils/bytepool"
"github.com/iwind/TeaGo/lists"
"github.com/iwind/TeaGo/maps"
"github.com/iwind/TeaGo/types"
@@ -1985,20 +1986,17 @@ func (this *HTTPRequest) addError(err error) {
}
// 计算合适的buffer size
func (this *HTTPRequest) bytePool(contentLength int64) *utils.BytePool {
func (this *HTTPRequest) bytePool(contentLength int64) *bytepool.Pool {
if contentLength < 0 {
return utils.BytePool16k
return bytepool.Pool16k
}
if contentLength < 8192 { // 8K
return utils.BytePool1k
if contentLength < 8192 { // < 8K
return bytepool.Pool1k
}
if contentLength < 32768 { // 32K
return utils.BytePool16k
if contentLength < 32768 { // < 32K
return bytepool.Pool16k
}
if contentLength < 131072 { // 128K
return utils.BytePool32k
}
return utils.BytePool32k
return bytepool.Pool32k
}
// 检查是否可以忽略错误
@@ -2008,20 +2006,21 @@ func (this *HTTPRequest) canIgnore(err error) bool {
}
// 已读到头
if err == io.EOF || err == io.ErrUnexpectedEOF {
if err == io.EOF || errors.Is(err, io.ErrUnexpectedEOF) {
return true
}
// 网络错误
_, ok := err.(*net.OpError)
var opErr *net.OpError
ok := errors.As(err, &opErr)
if ok {
return true
}
// 客户端主动取消
if err == errWritingToClient ||
err == context.Canceled ||
err == io.ErrShortWrite ||
if errors.Is(err, errWritingToClient) ||
errors.Is(err, context.Canceled) ||
errors.Is(err, io.ErrShortWrite) ||
strings.Contains(err.Error(), "write: connection") ||
strings.Contains(err.Error(), "write: broken pipe") ||
strings.Contains(err.Error(), "write tcp") {

View File

@@ -4,6 +4,7 @@ import (
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs"
"github.com/TeaOSLab/EdgeNode/internal/remotelogs"
"github.com/TeaOSLab/EdgeNode/internal/utils"
"github.com/TeaOSLab/EdgeNode/internal/utils/bytepool"
"github.com/iwind/TeaGo/Tea"
"net/http"
"os"
@@ -103,11 +104,11 @@ func (this *HTTPRequest) doPageLookup(pages []*serverconfigs.HTTPPageConfig, sta
this.writer.Prepare(nil, stat.Size(), status, true)
this.writer.WriteHeader(status)
}
var buf = utils.BytePool1k.Get()
var buf = bytepool.Pool1k.Get()
_, err = utils.CopyWithFilter(this.writer, fp, buf.Bytes, func(p []byte) []byte {
return []byte(this.Format(string(p)))
})
utils.BytePool1k.Put(buf)
bytepool.Pool1k.Put(buf)
if err != nil {
if !this.canIgnore(err) {
remotelogs.Warn("HTTP_REQUEST_PAGE", "write to client failed: "+err.Error())

View File

@@ -8,6 +8,7 @@ import (
"github.com/TeaOSLab/EdgeNode/internal/compressions"
"github.com/TeaOSLab/EdgeNode/internal/remotelogs"
"github.com/TeaOSLab/EdgeNode/internal/utils"
"github.com/TeaOSLab/EdgeNode/internal/utils/bytepool"
"github.com/TeaOSLab/EdgeNode/internal/utils/fnv"
"github.com/TeaOSLab/EdgeNode/internal/utils/minifiers"
"github.com/iwind/TeaGo/lists"
@@ -581,9 +582,9 @@ func (this *HTTPRequest) doOriginRequest(failedOriginIds []int64, failedLnNodeId
// 是否有内容
if resp.ContentLength == 0 && len(resp.TransferEncoding) == 0 {
// 即使内容为0也需要读取一次以便于触发相关事件
var buf = utils.BytePool4k.Get()
var buf = bytepool.Pool4k.Get()
_, _ = io.CopyBuffer(this.writer, resp.Body, buf.Bytes)
utils.BytePool4k.Put(buf)
bytepool.Pool4k.Put(buf)
_ = resp.Body.Close()
respBodyIsClosed = true

View File

@@ -4,6 +4,7 @@ import (
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs"
"github.com/TeaOSLab/EdgeNode/internal/remotelogs"
"github.com/TeaOSLab/EdgeNode/internal/utils"
"github.com/TeaOSLab/EdgeNode/internal/utils/bytepool"
"github.com/iwind/TeaGo/Tea"
"net/http"
"os"
@@ -68,11 +69,11 @@ func (this *HTTPRequest) doShutdown() {
this.ProcessResponseHeaders(this.writer.Header(), http.StatusOK)
this.writer.WriteHeader(http.StatusOK)
}
var buf = utils.BytePool1k.Get()
var buf = bytepool.Pool1k.Get()
_, err = utils.CopyWithFilter(this.writer, fp, buf.Bytes, func(p []byte) []byte {
return []byte(this.Format(string(p)))
})
utils.BytePool1k.Put(buf)
bytepool.Pool1k.Put(buf)
if err != nil {
if !this.canIgnore(err) {
remotelogs.Warn("HTTP_REQUEST_SHUTDOWN", "write to client failed: "+err.Error())

View File

@@ -4,7 +4,7 @@ import (
"bufio"
"bytes"
"errors"
"github.com/TeaOSLab/EdgeNode/internal/utils"
"github.com/TeaOSLab/EdgeNode/internal/utils/bytepool"
"io"
"net/http"
"net/url"
@@ -178,8 +178,8 @@ func (this *HTTPRequest) doWebsocket(requestHost string, isLastRetry bool) (shou
}
// 复制剩余的数据
var buf = utils.BytePool4k.Get()
defer utils.BytePool4k.Put(buf)
var buf = bytepool.Pool4k.Get()
defer bytepool.Pool4k.Put(buf)
for {
n, readErr := originConn.Read(buf.Bytes)
if n > 0 {
@@ -197,9 +197,9 @@ func (this *HTTPRequest) doWebsocket(requestHost string, isLastRetry bool) (shou
_ = originConn.Close()
}()
var buf = utils.BytePool4k.Get()
var buf = bytepool.Pool4k.Get()
_, _ = io.CopyBuffer(originConn, clientConn, buf.Bytes)
utils.BytePool4k.Put(buf)
bytepool.Pool4k.Put(buf)
return
}

View File

@@ -8,7 +8,7 @@ import (
"github.com/TeaOSLab/EdgeNode/internal/goman"
"github.com/TeaOSLab/EdgeNode/internal/remotelogs"
"github.com/TeaOSLab/EdgeNode/internal/stats"
"github.com/TeaOSLab/EdgeNode/internal/utils"
"github.com/TeaOSLab/EdgeNode/internal/utils/bytepool"
"github.com/iwind/TeaGo/types"
"github.com/pires/go-proxyproto"
"net"
@@ -188,9 +188,9 @@ func (this *TCPListener) handleConn(server *serverconfigs.ServerConfig, conn net
// 从源站读取
goman.New(func() {
var originBuf = utils.BytePool16k.Get()
var originBuf = bytepool.Pool16k.Get()
defer func() {
utils.BytePool16k.Put(originBuf)
bytepool.Pool16k.Put(originBuf)
}()
for {
n, err := originConn.Read(originBuf.Bytes)
@@ -214,9 +214,9 @@ func (this *TCPListener) handleConn(server *serverconfigs.ServerConfig, conn net
})
// 从客户端读取
var clientBuf = utils.BytePool16k.Get()
var clientBuf = bytepool.Pool16k.Get()
defer func() {
utils.BytePool16k.Put(clientBuf)
bytepool.Pool16k.Put(clientBuf)
}()
for {
// 是否已达到流量限制

View File

@@ -9,6 +9,7 @@ import (
"github.com/TeaOSLab/EdgeNode/internal/remotelogs"
"github.com/TeaOSLab/EdgeNode/internal/stats"
"github.com/TeaOSLab/EdgeNode/internal/utils"
"github.com/TeaOSLab/EdgeNode/internal/utils/bytepool"
"github.com/iwind/TeaGo/types"
"github.com/pires/go-proxyproto"
"golang.org/x/net/ipv4"
@@ -400,9 +401,9 @@ func NewUDPConn(server *serverconfigs.ServerConfig, clientAddr net.Addr, proxyLi
}
goman.New(func() {
var buf = utils.BytePool4k.Get()
var buf = bytepool.Pool4k.Get()
defer func() {
utils.BytePool4k.Put(buf)
bytepool.Pool4k.Put(buf)
}()
for {