dns: use pool buffer if possible

This commit is contained in:
nadoo committed 2020-04-22 19:37:10 +08:00
1 parent 45f4240f73
commit 76fddcc29c
8 files changed
+63 -35

No files matched your search

+9 -3
View File
@@ -8,6 +8,7 @@ import (
"time"
"github.com/nadoo/glider/common/log"
"github.com/nadoo/glider/common/pool"
"github.com/nadoo/glider/proxy"
)
@@ -59,7 +60,8 @@ func (s *Server) ListenAndServeUDP(wg *sync.WaitGroup) {
log.F("[dns] listening UDP on %s", s.addr)
for {
reqBytes := make([]byte, 2+UDPMaxLen)
reqBytes := pool.GetBuffer(2 + UDPMaxLen)
n, caddr, err := c.ReadFrom(reqBytes[2:])
if err != nil {
log.F("[dns] local read error: %v", err)
@@ -75,6 +77,8 @@ func (s *Server) ListenAndServeUDP(wg *sync.WaitGroup) {
go func() {
respBytes, err := s.Client.Exchange(reqBytes[:2+n], caddr.String(), false)
defer pool.PutBuffer(reqBytes)
if err != nil {
log.F("[dns] error in exchange: %s", err)
return
@@ -125,7 +129,9 @@ func (s *Server) ServeTCP(c net.Conn) {
return
}
reqBytes := make([]byte, reqLen+2)
reqBytes := pool.GetBuffer(int(reqLen) + 2)
defer pool.PutBuffer(reqBytes)
_, err := io.ReadFull(c, reqBytes[2:])
if err != nil {
log.F("[dns-tcp] error in read reqBytes %s", err)
@@ -140,7 +146,7 @@ func (s *Server) ServeTCP(c net.Conn) {
return
}
if err := binary.Write(c, binary.BigEndian, respBytes); err != nil {
if _, err := c.Write(respBytes); err != nil {
log.F("[dns-tcp] error in local write respBytes: %s", err)
return
}