Pool - Usage

Basic Usage

package main

import (
    "github.com/gogf/gf/v2/container/gpool"
    "fmt"
    "time"
)

func main () {
    // Create an object pool with an expiration time of 1 second
    p := gpool.New(time.Second, nil)

    // Get an object from the pool, return nil and error information
    fmt.Println(p.Get())

    // Put an object into the pool
    p.Put(1)

    // Get an object from the pool again, return 1
    fmt.Println(p.Get())

    // After waiting for 2 seconds, try again and find the object has expired, return nil and error information
    time.Sleep(2*time.Second)
    fmt.Println(p.Get())
}

Create and Destroy Methods

We can specify dynamic create and destroy methods.

package main

import (
    "fmt"
    "github.com/gogf/gf/v2/container/gpool"
    "github.com/gogf/gf/v2/net/gtcp"
    "github.com/gogf/gf/v2/os/glog"
    "time"
)

func main() {
    // Create an object reuse pool with an object expiration time of 3 seconds, and specify create and destroy methods
    p := gpool.New(3*time.Second, func() (interface{}, error) {
        return gtcp.NewConn("www.baidu.com:80")
    }, func(i interface{}) {
        glog.Println("expired")
        i.(*gtcp.Conn).Close()
    })
    conn, err := p.Get()
    if err != nil {
        panic(err)
    }
    result, err := conn.(*gtcp.Conn).SendRecv([]byte("HEAD / HTTP/1.1\n\n"), -1)
    if err != nil {
        panic(err)
    }
    fmt.Println(string(result))
    // Put back into the pool for reuse
    p.Put(conn)
    // Wait for a while to observe the expiration method call
    time.Sleep(4*time.Second)
}

After execution, the terminal outputs:

HTTP/1.1 302 Found
Connection: Keep-Alive
Content-Length: 17931
Content-Type: text/html
Date: Wed, 29 May 2019 11:23:20 GMT
Etag: "54d9749e-460b"
Server: bfe/1.0.8.18

2019-05-29 19:23:24.732 expired