Skip to content

Commit

Permalink
use bufpool
Browse files Browse the repository at this point in the history
  • Loading branch information
ls0f committed Nov 19, 2017
1 parent 25f100c commit 4bde986
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 3 deletions.
10 changes: 8 additions & 2 deletions http.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 {
Expand All @@ -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()
Expand Down
3 changes: 2 additions & 1 deletion local.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package cracker

import (
"context"
"crypto/tls"
"crypto/x509"
"errors"
Expand All @@ -10,7 +11,7 @@ import (
"net/http"
"os"
"time"
"context"

g "github.com/golang/glog"
"gopkg.in/bufio.v1"
)
Expand Down

0 comments on commit 4bde986

Please sign in to comment.