Do the TCP write in one write

Before the tcp writes were done like NSD does them:
2 bytes length, and the rest of the message.

Now a complete buffer is created with LLMMM...MMM, where
LL is the 2 byte length and MMM...MMM is the message to be send.
This makes the reply faster at the cost of doing a realloc of the
message buffer.
This commit is contained in:
Miek Gieben 2012-10-17 12:40:57 +02:00
parent a811e314c0
commit ea947838d8
3 changed files with 10 additions and 24 deletions

View File

@ -253,18 +253,10 @@ func (w *reply) write(p []byte) (n int, err error) {
}
for a := 0; a < attempts; a++ {
setTimeouts(w)
a, b := packUint16(uint16(len(p)))
n, err = w.conn.Write([]byte{a, b})
if err != nil {
if e, ok := err.(net.Error); ok && e.Timeout() {
continue
}
return n, err
}
if n != 2 {
return n, io.ErrShortWrite
}
n, err = w.conn.Write(p)
l := make([]byte, 2)
l[0], l[1] = packUint16(uint16(len(p)))
p = append(l, p...)
n, err := w.conn.Write(p)
if err != nil {
if e, ok := err.(net.Error); ok && e.Timeout() {
continue

View File

@ -8,7 +8,6 @@ package dns
import (
"github.com/miekg/radix"
"io"
"net"
"time"
)
@ -425,7 +424,7 @@ func (w *response) Write(m *Msg) (err error) {
}
// WriteBuf implements the ResponseWriter.WriteBuf method.
func (w *response) WriteBuf(m []byte) (err error) {
func (w *response) WriteBuf(m []byte) error {
switch {
case w._UDP != nil:
_, err := w._UDP.WriteTo(m, w.remoteAddr)
@ -436,15 +435,10 @@ func (w *response) WriteBuf(m []byte) (err error) {
if len(m) > MaxMsgSize {
return &Error{Err: "message too large"}
}
a, b := packUint16(uint16(len(m)))
n, err := w._TCP.Write([]byte{a, b})
if err != nil {
return err
}
if n != 2 {
return io.ErrShortWrite
}
n, err = w._TCP.Write(m)
l := make([]byte, 2)
l[0], l[1] = packUint16(uint16(len(m)))
m = append(l, m...)
n, err := w._TCP.Write(m)
if err != nil {
return err
}

View File

@ -16,7 +16,7 @@ import (
// multilpe goroutines.
type Zone struct {
Origin string // Origin of the zone
olabels []string // origin cut up in labels, to speed up IsSubDomain function
olabels []string // origin cut up in labels, just to speed up the isSubDomain method
Wildcard int // Whenever we see a wildcard name, this is incremented
*radix.Radix // Zone data
mutex *sync.RWMutex