-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Remove dependency on leakybuf by importing code.
- Loading branch information
Showing
2 changed files
with
41 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters