From 4bde986142c86592d4316ee6651504fb7a37e309 Mon Sep 17 00:00:00 2001 From: lovedboy Date: Sun, 19 Nov 2017 21:40:50 +0800 Subject: [PATCH] use bufpool --- http.go | 10 ++++++++-- local.go | 3 ++- 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/http.go b/http.go index 4964c46..4a5814f 100644 --- a/http.go +++ b/http.go @@ -50,6 +50,8 @@ const ( version = "20170803" ) +var bufPool = &sync.Pool{New: func() interface{} { return make([]byte, 1024*8) }} + type httpProxy struct { addr string secret string @@ -138,10 +140,15 @@ func (hp *httpProxy) pull(w http.ResponseWriter, r *http.Request) { if interval == "" { interval = "0" } + buf := bufPool.Get().([]byte) + defer bufPool.Put(buf) t, _ := strconv.ParseInt(interval, 10, 0) if t > 0 { pc.remote.SetReadDeadline(time.Now().Add(time.Duration(t))) - _, err := io.Copy(w, pc.remote) + n, err := pc.remote.Read(buf) + if n > 0 { + w.Write(buf[:n]) + } if err != nil { if err, ok := err.(net.Error); ok && err.Timeout() { } else { @@ -156,7 +163,6 @@ func (hp *httpProxy) pull(w http.ResponseWriter, r *http.Request) { } flusher, _ := w.(http.Flusher) w.Header().Set("Transfer-Encoding", "chunked") - buf := make([]byte, 10240) defer pc.Close() for { flusher.Flush() diff --git a/local.go b/local.go index e833e56..51eaf87 100644 --- a/local.go +++ b/local.go @@ -1,6 +1,7 @@ package cracker import ( + "context" "crypto/tls" "crypto/x509" "errors" @@ -10,7 +11,7 @@ import ( "net/http" "os" "time" - "context" + g "github.com/golang/glog" "gopkg.in/bufio.v1" )