Skip to content

Instantly share code, notes, and snippets.

@starchou
Created January 17, 2015 10:50
Show Gist options
  • Save starchou/cf059f7f9e939d1eda55 to your computer and use it in GitHub Desktop.
Save starchou/cf059f7f9e939d1eda55 to your computer and use it in GitHub Desktop.
simple pool of golang
var (
MAX_POOL_SIZE = 20
ch chan *Client
)
func getPool() *Client {
if ch == nil {
ch = make(chan *Client, MAX_POOL_SIZE)
}
if len(ch) == 0 {
go func() {
for i := 0; i < MAX_POOL_SIZE/2; i++ {
conn, err := NewClient()
if err != nil {
println(err)
} else {
setPool(conn)
}
}
}()
}
return <-ch
}
func setPool(conn *Client) {
if ch == nil {
ch = make(chan *Client, MAX_POOL_SIZE)
}
if len(ch) == MAX_POOL_SIZE {
conn.Close()
return
}
ch <- conn
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment