gitee.com/quant1x/gox@v1.21.2/pool/README.md (about)

     1  # pool
     2  [![PkgGoDev](https://pkg.go.dev/badge/github.com/silenceper/pool)](https://pkg.go.dev/github.com/silenceper/pool)
     3  [![Go Report Card](https://goreportcard.com/badge/github.com/silenceper/pool)](https://goreportcard.com/report/github.com/silenceper/pool)
     4  
     5  
     6  [中文文档](./README_ZH_CN.md)
     7  
     8  A golang universal network connection pool.
     9  
    10  ## Feature:
    11  
    12  - More versatile, The connection type in the connection pool is `interface{}`, making it more versatile
    13  - More configurable, The connection supports setting the maximum idle time, the timeout connection will be closed and discarded, which can avoid the problem of automatic connection failure when idle
    14  - Support user setting `ping` method, used to check the connectivity of connection, invalid connection will be discarded
    15  - Support connection waiting, When the connection pool is full, support for connection waiting (like the go db connection pool)
    16  
    17  ## Basic Usage:
    18  
    19  ```go
    20  
    21  //factory Specify the method to create the connection
    22  factory := func() (interface{}, error) { return net.Dial("tcp", "127.0.0.1:4000") }
    23  
    24  //close Specify the method to close the connection
    25  close := func(v interface{}) error { return v.(net.Conn).Close() }
    26  
    27  //ping Specify the method to detect whether the connection is invalid
    28  //ping := func(v interface{}) error { return nil }
    29  
    30  //Create a connection pool: Initialize the number of connections to 5, the maximum idle connection is 20, and the maximum concurrent connection is 30
    31  poolConfig := &pool.Config{
    32  	InitialCap: 5,
    33  	MaxIdle:   20,
    34  	MaxCap:     30,
    35  	Factory:    factory,
    36  	Close:      close,
    37  	//Ping:       ping,
    38  	//The maximum idle time of the connection, the connection exceeding this time will be closed, which can avoid the problem of automatic failure when connecting to EOF when idle
    39  	IdleTimeout: 15 * time.Second,
    40  }
    41  p, err := pool.NewChannelPool(poolConfig)
    42  if err != nil {
    43  	fmt.Println("err=", err)
    44  }
    45  
    46  //Get a connection from the connection pool
    47  v, err := p.Get()
    48  
    49  //do something
    50  //conn=v.(net.Conn)
    51  
    52  //Put the connection back into the connection pool, when the connection is no longer in use
    53  p.Put(v)
    54  
    55  //Release all connections in the connection pool, when resources need to be destroyed
    56  p.Release()
    57  
    58  //View the number of connections in the current connection pool
    59  current := p.Len()
    60  
    61  
    62  ```
    63  
    64  
    65  #### Remarks:
    66  The connection pool implementation refers to pool [https://github.com/fatih/pool](https://github.com/fatih/pool) , thanks.
    67  
    68  
    69  ## License
    70  
    71  The MIT License (MIT) - see LICENSE for more details