Skip to content

Commit

Permalink
Revert "use io.CopyBuffer instead of io.Copy for memory recycle"
Browse files Browse the repository at this point in the history
This reverts commit ad8683d.
  • Loading branch information
xtaci committed Mar 28, 2017
1 parent ae513e9 commit 6496672
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 42 deletions.
23 changes: 2 additions & 21 deletions client/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
"math/rand"
"net"
"os"
"sync"
"time"

"golang.org/x/crypto/pbkdf2"
Expand All @@ -28,11 +27,6 @@ var (
SALT = "kcp-go"
)

// global recycle buffer
var copyBuf sync.Pool

const bufSize = 4096

type compStream struct {
conn net.Conn
w *snappy.Writer
Expand Down Expand Up @@ -73,20 +67,10 @@ func handleClient(sess *smux.Session, p1 io.ReadWriteCloser) {

// start tunnel
p1die := make(chan struct{})
go func() {
buf := copyBuf.Get().([]byte)
io.CopyBuffer(p1, p2, buf)
close(p1die)
copyBuf.Put(buf)
}()
go func() { io.Copy(p1, p2); close(p1die) }()

p2die := make(chan struct{})
go func() {
buf := copyBuf.Get().([]byte)
io.CopyBuffer(p2, p1, buf)
close(p2die)
copyBuf.Put(buf)
}()
go func() { io.Copy(p2, p1); close(p2die) }()

// wait for tunnel termination
select {
Expand All @@ -104,9 +88,6 @@ func checkError(err error) {

func main() {
rand.Seed(int64(time.Now().Nanosecond()))
copyBuf.New = func() interface{} {
return make([]byte, bufSize)
}
if VERSION == "SELFBUILD" {
// add more log flags for debugging
log.SetFlags(log.LstdFlags | log.Lshortfile)
Expand Down
23 changes: 2 additions & 21 deletions server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (
"net/http"
_ "net/http/pprof"
"os"
"sync"
"time"

"golang.org/x/crypto/pbkdf2"
Expand All @@ -29,11 +28,6 @@ var (
SALT = "kcp-go"
)

// global recycle buffer
var copyBuf sync.Pool

const bufSize = 4096

type compStream struct {
conn net.Conn
w *snappy.Writer
Expand Down Expand Up @@ -99,20 +93,10 @@ func handleClient(p1, p2 io.ReadWriteCloser) {

// start tunnel
p1die := make(chan struct{})
go func() {
buf := copyBuf.Get().([]byte)
io.CopyBuffer(p1, p2, buf)
close(p1die)
copyBuf.Put(buf)
}()
go func() { io.Copy(p1, p2); close(p1die) }()

p2die := make(chan struct{})
go func() {
buf := copyBuf.Get().([]byte)
io.CopyBuffer(p2, p1, buf)
close(p2die)
copyBuf.Put(buf)
}()
go func() { io.Copy(p2, p1); close(p2die) }()

// wait for tunnel termination
select {
Expand All @@ -130,9 +114,6 @@ func checkError(err error) {

func main() {
rand.Seed(int64(time.Now().Nanosecond()))
copyBuf.New = func() interface{} {
return make([]byte, bufSize)
}
if VERSION == "SELFBUILD" {
// add more log flags for debugging
log.SetFlags(log.LstdFlags | log.Lshortfile)
Expand Down

0 comments on commit 6496672

Please sign in to comment.