Add PipePool of different implementations

This commit is contained in:
Tevin Zhang 2019-02-12 18:39:31 +08:00
parent e298e4beb3
commit 562d62828d
3 changed files with 47 additions and 28 deletions

View File

@ -1,31 +1,6 @@
package tcp
import "sync"
type pipePool struct {
pool sync.Pool
}
func newPipePool() pipePool {
return pipePool{sync.Pool{
New: func() interface{} {
return make(chan error, 1)
}},
}
}
func (p *pipePool) getPipe() chan error {
return p.pool.Get().(chan error)
}
func (p *pipePool) putBackPipe(pipe chan error) {
p.cleanPipe(pipe)
p.pool.Put(pipe)
}
func (p *pipePool) cleanPipe(pipe chan error) {
select {
case <-pipe:
default:
}
type pipePool interface {
getPipe() chan error
putBackPipe(chan error)
}

13
pipe_pool_dummy.go Normal file
View File

@ -0,0 +1,13 @@
package tcp
type pipePoolDummy struct{}
func newPipePoolDummy() *pipePoolDummy {
return &pipePoolDummy{}
}
func (*pipePoolDummy) getPipe() chan error {
return make(chan error, 1)
}
func (*pipePoolDummy) putBackPipe(pipe chan error) {}

31
pipe_pool_sync_pool.go Normal file
View File

@ -0,0 +1,31 @@
package tcp
import "sync"
type pipePoolSyncPool struct {
pool sync.Pool
}
func newPipePoolSyncPool() *pipePoolSyncPool {
return &pipePoolSyncPool{sync.Pool{
New: func() interface{} {
return make(chan error, 1)
}},
}
}
func (p *pipePoolSyncPool) getPipe() chan error {
return p.pool.Get().(chan error)
}
func (p *pipePoolSyncPool) putBackPipe(pipe chan error) {
p.cleanPipe(pipe)
p.pool.Put(pipe)
}
func (p *pipePoolSyncPool) cleanPipe(pipe chan error) {
select {
case <-pipe:
default:
}
}