Skip to content

Commit

Permalink
Remove dependency on leakybuf by importing code.
Browse files Browse the repository at this point in the history
  • Loading branch information
cyfdecyf committed Sep 10, 2014
1 parent 211bdfc commit 9064175
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 2 deletions.
40 changes: 40 additions & 0 deletions shadowsocks/leakybuf.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// Provides leaky buffer, based on the example in Effective Go.
package shadowsocks

type LeakyBuf struct {
bufSize int // size of each buffer
freeList chan []byte
}

// NewLeakyBuf creates a leaky buffer which can hold at most n buffer, each
// with bufSize bytes.
func NewLeakyBuf(n, bufSize int) *LeakyBuf {
return &LeakyBuf{
bufSize: bufSize,
freeList: make(chan []byte, n),
}
}

// Get returns a buffer from the leaky buffer or create a new buffer.
func (lb *LeakyBuf) Get() (b []byte) {
select {
case b = <-lb.freeList:
default:
b = make([]byte, lb.bufSize)
}
return
}

// Put add the buffer into the free buffer pool for reuse. Panic if the buffer
// size is not the same with the leaky buffer's. This is intended to expose
// error usage of leaky buffer.
func (lb *LeakyBuf) Put(b []byte) {
if len(b) != lb.bufSize {
panic("invalid buffer size that's put into leaky buffer")
}
select {
case lb.freeList <- b:
default:
}
return
}
3 changes: 1 addition & 2 deletions shadowsocks/pipe.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package shadowsocks

import (
// "io"
"github.com/cyfdecyf/leakybuf"
"net"
"time"
)
Expand All @@ -21,7 +20,7 @@ func SetReadTimeout(c net.Conn) {
const bufSize = 4096
const nBuf = 2048

var pipeBuf = leakybuf.NewLeakyBuf(nBuf, bufSize)
var pipeBuf = NewLeakyBuf(nBuf, bufSize)

// PipeThenClose copies data from src to dst, closes dst when done.
func PipeThenClose(src, dst net.Conn, timeoutOpt int) {
Expand Down

0 comments on commit 9064175

Please sign in to comment.