Skip to content

Instantly share code, notes, and snippets.

@obscuren
Last active August 29, 2015 14:25
Show Gist options
  • Save obscuren/2f3d98dcab675134e7d1 to your computer and use it in GitHub Desktop.
Save obscuren/2f3d98dcab675134e7d1 to your computer and use it in GitHub Desktop.
type IntPool struct {
pool chan *big.Int
}
// NewPool creates a new pool of big.Int
func NewIntPool(max int) *IntPool {
return &IntPool{
pool: make(chan *big.Int, max),
}
}
// Borrow an Int from the pool.
func (p *IntPool) Borrow() *big.Int {
var c *big.Int
select {
case c = <-p.pool:
default:
c = new(big.Int)
}
return c
}
// Return returns an Intto the pool.
func (p *IntPool) Return(i ...*big.Int) {
for _, c := range i {
select {
case p.pool <- c:
default:
// let it go, let it go...
return
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment