Handler: Use a buffer pool

This commit is contained in:
Carl Johnson 2016-12-11 10:10:04 -05:00
parent 5afbb7f3e9
commit 6a94b58990
1 changed files with 18 additions and 1 deletions

View File

@ -4,11 +4,28 @@ import (
"io"
"log"
"net/http"
"sync"
)
var pool sync.Pool
func getBuffer() []byte {
x := pool.Get()
if buf, ok := x.([]byte); ok {
return buf
} else {
return make([]byte, 100*1<<10)
}
}
func putBuffer(buf []byte) {
pool.Put(buf)
}
func Honeypot(w http.ResponseWriter, r *http.Request) {
log.Printf("Started writing: %v", r.URL)
buf := make([]byte, 100*1<<10)
buf := getBuffer()
defer putBuffer(buf)
io.WriteString(w, "<HTML>\n<BODY>\n")
n, err := io.CopyBuffer(w, DefaultMarkovMap, buf)
log.Printf("Wrote: %d (%v)", n, err)