Make simpler example
This commit is contained in:
+42
-52
@@ -591,82 +591,72 @@ func TestStress(t *testing.T) {
|
|||||||
pool.Close()
|
pool.Close()
|
||||||
}
|
}
|
||||||
|
|
||||||
func exampleDummyServer(laddr string, acceptCount int, recvCountChan chan int) {
|
func startAcceptOnceDummyServer(laddr string) {
|
||||||
ln, err := net.Listen("tcp", laddr)
|
ln, err := net.Listen("tcp", laddr)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatalln("Listen:", err)
|
log.Fatalln("Listen:", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
for i := 0; i < acceptCount; i++ {
|
// Listen one time
|
||||||
|
go func() {
|
||||||
conn, err := ln.Accept()
|
conn, err := ln.Accept()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatalln("Accept:", err)
|
log.Fatalln("Accept:", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
go func() {
|
for {
|
||||||
recvCount := 0
|
buf := make([]byte, 1)
|
||||||
for {
|
_, err := conn.Read(buf)
|
||||||
buf := make([]byte, 1)
|
if err != nil {
|
||||||
_, err := conn.Read(buf)
|
return
|
||||||
if err != nil {
|
|
||||||
recvCountChan <- recvCount
|
|
||||||
return
|
|
||||||
}
|
|
||||||
recvCount += 1
|
|
||||||
}
|
}
|
||||||
}()
|
}
|
||||||
}
|
}()
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func Example_Pool() {
|
func ExamplePool() {
|
||||||
// Dummy server
|
// Dummy server
|
||||||
maxPoolSize := 4
|
|
||||||
serverRecvCountChan := make(chan int)
|
|
||||||
laddr := "127.0.0.1:8080"
|
laddr := "127.0.0.1:8080"
|
||||||
|
startAcceptOnceDummyServer(laddr)
|
||||||
|
|
||||||
// exampleDummyServer only listens maxPoolSize times so if the pool tried to
|
// Pool creation
|
||||||
// connect more than that the pool would receive an error.
|
constructor := func(context.Context) (interface{}, error) {
|
||||||
go exampleDummyServer(laddr, maxPoolSize, serverRecvCountChan)
|
return net.Dial("tcp", laddr)
|
||||||
|
}
|
||||||
|
destructor := func(value interface{}) {
|
||||||
|
value.(net.Conn).Close()
|
||||||
|
}
|
||||||
|
maxPoolSize := 10
|
||||||
|
|
||||||
// Pool usage
|
pool := puddle.NewPool(constructor, destructor, maxPoolSize)
|
||||||
pool := puddle.NewPool(
|
|
||||||
func(context.Context) (interface{}, error) { return net.Dial("tcp", laddr) },
|
|
||||||
func(value interface{}) { value.(net.Conn).Close() },
|
|
||||||
maxPoolSize,
|
|
||||||
)
|
|
||||||
|
|
||||||
clientCount := 32
|
// Use pool multiple times
|
||||||
opPerClientCount := 100
|
for i := 0; i < 10; i++ {
|
||||||
wg := &sync.WaitGroup{}
|
// Acquire resource
|
||||||
|
res, err := pool.Acquire(context.Background())
|
||||||
|
if err != nil {
|
||||||
|
log.Fatalln("Acquire", err)
|
||||||
|
}
|
||||||
|
|
||||||
for i := 0; i < clientCount; i++ {
|
// Type-assert value and use
|
||||||
wg.Add(1)
|
_, err = res.Value().(net.Conn).Write([]byte{1})
|
||||||
go func() {
|
if err != nil {
|
||||||
for i := 0; i < opPerClientCount; i++ {
|
log.Fatalln("Write", err)
|
||||||
res, err := pool.Acquire(context.Background())
|
}
|
||||||
if err != nil {
|
|
||||||
log.Fatalln("Acquire", err)
|
// Release when done.
|
||||||
}
|
res.Release()
|
||||||
_, err = res.Value().(net.Conn).Write([]byte{1})
|
|
||||||
if err != nil {
|
|
||||||
log.Fatalln("Write", err)
|
|
||||||
}
|
|
||||||
res.Release()
|
|
||||||
}
|
|
||||||
wg.Done()
|
|
||||||
}()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
wg.Wait()
|
stats := pool.Stat()
|
||||||
pool.Close()
|
pool.Close()
|
||||||
totalRecv := <-serverRecvCountChan
|
|
||||||
totalRecv += <-serverRecvCountChan
|
|
||||||
totalRecv += <-serverRecvCountChan
|
|
||||||
totalRecv += <-serverRecvCountChan
|
|
||||||
|
|
||||||
fmt.Println("Ops:", totalRecv)
|
fmt.Println("Connections:", stats.TotalResources())
|
||||||
|
fmt.Println("Acquires:", stats.AcquireCount())
|
||||||
// Output:
|
// Output:
|
||||||
// Ops: 3200
|
// Connections: 1
|
||||||
|
// Acquires: 10
|
||||||
}
|
}
|
||||||
|
|
||||||
func BenchmarkPoolAcquireAndRelease(b *testing.B) {
|
func BenchmarkPoolAcquireAndRelease(b *testing.B) {
|
||||||
|
|||||||
Reference in New Issue
Block a user