Skip to content

Commit

Permalink
buf size
Browse files Browse the repository at this point in the history
  • Loading branch information
ls0f committed Mar 21, 2017
1 parent ac14d41 commit 7c109d7
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 4 deletions.
2 changes: 2 additions & 0 deletions server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,10 @@ func main() {

addr := flag.String("addr", "", "listen addr")
secret := flag.String("secret", "", "secret")
bufSize := flag.Int("buf", 1, "buf size, Unit is MB")
flag.Parse()
p := proxy.NewHttpProxy(*addr, *secret)
p.SetBufSize(*bufSize * 1024)
p.Listen()

}
14 changes: 13 additions & 1 deletion src/proxy/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,17 +36,24 @@ const (
timeout = 10
signTTL = 10
heartTTL = 30
bufSize = 10240
)

type httpProxy struct {
addr string
secret string
proxyMap map[string]*proxyConn
sync.Mutex
// buf size
bs int
}

func NewHttpProxy(addr, secret string) *httpProxy {
return &httpProxy{addr: addr, secret: secret, proxyMap: make(map[string]*proxyConn)}
return &httpProxy{addr: addr,
secret: secret,
proxyMap: make(map[string]*proxyConn),
bs: bufSize,
}
}

func (hp *httpProxy) Listen() {
Expand All @@ -62,6 +69,10 @@ func (hp *httpProxy) Listen() {
}
}

func (hp *httpProxy) SetBufSize(bs int) {
hp.bs = bs
}

func (hp *httpProxy) verify(r *http.Request) error {
ts := r.Header.Get("timestamp")
sign := r.Header.Get("sign")
Expand Down Expand Up @@ -184,6 +195,7 @@ func (hp *httpProxy) connect(w http.ResponseWriter, r *http.Request) {
log.Printf("Connect to %s: success ...\n", addr)
proxyID := uuid.New()
pc := newProxyConn(remote, proxyID)
pc.SetBufSize(hp.bs)
hp.Lock()
hp.proxyMap[proxyID] = pc
hp.Unlock()
Expand Down
1 change: 1 addition & 0 deletions src/proxy/local.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"strings"
"sync"
"time"

"gopkg.in/bufio.v1"
)

Expand Down
12 changes: 9 additions & 3 deletions src/proxy/proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@ import (
)

const (
sendTimeOut = 3
bufSize = 10240
sendTimeOut = 15
)

type dataBodyTyp struct {
Expand All @@ -21,15 +20,22 @@ type proxyConn struct {
readChannel chan dataBodyTyp
writeChannel chan dataBodyTyp
close chan bool
// buf size
bs int
}

func newProxyConn(remote net.Conn, uuid string) *proxyConn {
return &proxyConn{remote: remote, uuid: uuid, readChannel: make(chan dataBodyTyp, 10),
writeChannel: make(chan dataBodyTyp, 10),
close: make(chan bool),
bs: bufSize,
}
}

func (pc *proxyConn) SetBufSize(bs int) {
pc.bs = bs
}

func (pc *proxyConn) sendMsg(body dataBodyTyp) {
select {
case pc.readChannel <- body:
Expand All @@ -41,7 +47,7 @@ func (pc *proxyConn) work() {

go func() {
for {
buf := make([]byte, bufSize)
buf := make([]byte, pc.bs)
pc.remote.SetReadDeadline(time.Now().Add(timeout * time.Second))
n, err := pc.remote.Read(buf)
if n > 0 {
Expand Down

0 comments on commit 7c109d7

Please sign in to comment.