server: pool: remove pool implementation

Quite a lot of code for a tiny improvement. Lease the Pool boolean
option in server so current code does not break. Will be removed
in later versions.
This commit is contained in:
Miek Gieben 2014-04-10 12:59:50 +00:00
parent fcbb2e7a9a
commit 2ec512f89b
2 changed files with 2 additions and 76 deletions

61
pool.go
View File

@ -1,61 +0,0 @@
// Copyright 2011 Miek Gieben. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package dns
// Inspired copied from:
// http://blog.cloudflare.com/recycling-memory-buffers-in-go
// ... carries no license ...
import (
"container/list"
"time"
)
func mkBuf(size int) []byte { return make([]byte, size) }
type queued struct {
when time.Time
buf []byte
}
func pool(size, bufsize int) (get, give chan []byte) {
get = make(chan []byte, bufsize)
give = make(chan []byte, bufsize)
go func() {
q := new(list.List)
for {
e := q.Front()
if e == nil {
q.PushFront(queued{when: time.Now(), buf: mkBuf(size)})
e = q.Front()
}
timeout := time.NewTimer(time.Minute)
select {
case b := <-give:
timeout.Stop()
q.PushFront(queued{when: time.Now(), buf: b})
case get <- e.Value.(queued).buf:
timeout.Stop()
q.Remove(e)
case <-timeout.C:
e := q.Front()
for e != nil {
n := e.Next()
if time.Since(e.Value.(queued).when) > time.Minute {
q.Remove(e)
e.Value = nil
}
e = n
}
}
}
}()
return
}

View File

@ -209,10 +209,8 @@ type Server struct {
IdleTimeout func() time.Duration
// Secret(s) for Tsig map[<zonename>]<base64 secret>.
TsigSecret map[string]string
// If true use a pool to recycle buffers for UDP queries.
// If true use a pool to recycle buffers for UDP queries, this is now a noop.
Pool bool
// channels for the pool
get, give chan []byte
}
// ListenAndServe starts a nameserver on the configured address in *Server.
@ -224,9 +222,6 @@ func (srv *Server) ListenAndServe() error {
if srv.UDPSize == 0 {
srv.UDPSize = MinMsgSize
}
if srv.Pool {
srv.get, srv.give = pool(srv.UDPSize, 400) // Arbitrary number.
}
switch srv.Net {
case "tcp", "tcp4", "tcp6":
a, e := net.ResolveTCPAddr(srv.Net, addr)
@ -308,9 +303,6 @@ func (srv *Server) serve(a net.Addr, h Handler, m []byte, u *net.UDPConn, t *net
Redo:
req := new(Msg)
err := req.Unpack(m)
if srv.Pool && u != nil {
srv.give <- m[:srv.UDPSize]
}
if err != nil { // Send a FormatError back
x := new(Msg)
x.SetRcodeFormatError(req)
@ -394,12 +386,7 @@ func (srv *Server) readTCP(conn *net.TCPConn, timeout time.Duration) ([]byte, er
}
func (srv *Server) readUDP(conn *net.UDPConn, timeout time.Duration) ([]byte, net.Addr, error) {
var m []byte
if srv.Pool {
m = <-srv.get
} else {
m = make([]byte, srv.UDPSize)
}
m := make([]byte, srv.UDPSize)
n, a, e := conn.ReadFromUDP(m)
if e != nil || n == 0 {
if e != nil {