Skip to content

Latest commit

History

10 Commits

Folders and files

NameName
Last commit message
Last commit date

Repository files navigation

recycleBuffer

What is this?

recycleBuffer is for effective buffer usage in golang and thread-safe. It is wrote with very short and simple code.

Installation

Use following command in your terminal. go get github.com/DiyLecko/recycleBuffer

How to use?

  1. Import import "github.com/DiyLecko/recycleBuffer"
  2. Init with var rb *recycleBuffer.RecycleBuffer = recycleBuffer.Init(8192) // 1th param is bufferSize.
  3. Use buf := <-rb.Get to get buffer
  4. Use rb.Give<- buf to free buffer.

here is an example.

// recycleBuffer_test.gopackage recycleBuffer
import (
"math/rand""runtime""testing""time"
)
funcinit() {
runtime.GOMAXPROCS(runtime.NumCPU())
}
funcTestRecycleBuffer(t*testing.T) {
rb:=Init(8192)
buf:=<-rb.Getrb.Give<-buf
}
vardoneTest2chanbool=make(chanbool)
varendCountTest2int=100vartestCountTest2int=10varcountText2int=0functestRecycleBuffer2(rb*RecycleBuffer) {
fori:=0; i<testCountTest2; i++ {
time.Sleep(time.Millisecond*time.Duration(rand.Int63()%500))
buf:=<-rb.Getgofunc() {
time.Sleep(time.Millisecond*time.Duration(rand.Int63()%1000))
rb.Give<-bufcountText2++ifcountText2==endCountTest2*testCountTest2 {
doneTest2<-true
}
}()
}
}
funcTestRecycleBuffer2(t*testing.T) {
rb:=Init(8192)
fori:=0; i<endCountTest2; i++ {
gotestRecycleBuffer2(rb)
}
<-doneTest2t.Logf("buffer count : %d\n", rb.GetBufferCount())
}

result is

=== RUN TestRecycleBuffer
--- PASS: TestRecycleBuffer (0.00s)
=== RUN TestRecycleBuffer2
--- PASS: TestRecycleBuffer2 (4.58s)
recycleBuffer_test.go:50: buffer count : 206
PASS
ok recycleBuffer	4.588s

In above example, buffer is made 1000 times. But in result, buffer is made only 206 times.

About

Recycler Buffer for GoLang

Resources

Stars

8 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages