func main() { fac := func() (net.Conn, error) { return net.Dial("tcp", "127.0.0.1:7530") } p, err := spool.NewChannelPool(5, 30, fac) if err != nil { log.Println("create conn pool failed=>", err) } conn, err := p.Get() if err != nil { log.Println("get conn failed=>", err) } log.Println("current conn num is :=>", p.Len()) conn.Close() log.Println("current conn num is :=>", p.Len()) select {} }
func (this *Conn) NewPool(conf Conn) (Pool, error) { this = &conf if this.Host == "" { this.Host = "127.0.0.1" } if this.Port == "" { this.Port = "7530" } if this.Max == 0 { this.Max = 20 } if this.Min == 0 { this.Min = 5 } addr := this.Host + ":" + this.Port fac := func() (net.Conn, error) { return net.Dial("tcp", addr) } return spool.NewChannelPool(this.Min, this.Max, fac) }