Skip to content

Commit

Permalink
less memory usage
Browse files Browse the repository at this point in the history
  • Loading branch information
xtaci committed Apr 28, 2019
1 parent 2a9fb0b commit 5d015f0
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 8 deletions.
16 changes: 12 additions & 4 deletions client/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,10 +77,18 @@ func handleClient(sess *smux.Session, p1 io.ReadWriteCloser, quiet bool) {
streamCopy := func(dst io.Writer, src io.Reader) chan struct{} {
die := make(chan struct{})
go func() {
buf := xmitBuf.Get().([]byte)
io.CopyBuffer(dst, src, buf)
xmitBuf.Put(buf)
close(die)
if wt, ok := src.(io.WriterTo); ok {
wt.WriteTo(dst)
close(die)
} else if rt, ok := dst.(io.ReaderFrom); ok {
rt.ReadFrom(src)
close(die)
} else {
buf := xmitBuf.Get().([]byte)
io.CopyBuffer(dst, src, buf)
xmitBuf.Put(buf)
close(die)
}
}()
return die
}
Expand Down
16 changes: 12 additions & 4 deletions server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,10 +105,18 @@ func handleClient(p1, p2 io.ReadWriteCloser, quiet bool) {
streamCopy := func(dst io.Writer, src io.Reader) chan struct{} {
die := make(chan struct{})
go func() {
buf := xmitBuf.Get().([]byte)
io.CopyBuffer(dst, src, buf)
xmitBuf.Put(buf)
close(die)
if wt, ok := src.(io.WriterTo); ok {
wt.WriteTo(dst)
close(die)
} else if rt, ok := dst.(io.ReaderFrom); ok {
rt.ReadFrom(src)
close(die)
} else {
buf := xmitBuf.Get().([]byte)
io.CopyBuffer(dst, src, buf)
xmitBuf.Put(buf)
close(die)
}
}()
return die
}
Expand Down

0 comments on commit 5d015f0

Please sign in to comment.