连接池

来源:互联网 发布:达内java资源 编辑:程序博客网 时间:2024/06/10 16:12
var (MAX_POOL_SIZE  =  20ch               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}

0 0