gpool

package
v2.9.6 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 4, 2025 License: MIT Imports: 8 Imported by: 14

Documentation

Overview

Package gpool provides object-reusable concurrent-safe pool.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type ExpireFunc

type ExpireFunc = TPoolExpireFunc[any]

ExpireFunc Destruction function for object.

type NewFunc

type NewFunc = TPoolNewFunc[any]

NewFunc Creation function for object.

type Pool

type Pool struct {
	*TPool[any]
}

Pool is an Object-Reusable Pool.

func New

func New(ttl time.Duration, newFunc NewFunc, expireFunc ...ExpireFunc) *Pool

New creates and returns a new object pool. To ensure execution efficiency, the expiration time cannot be modified once it is set.

Note the expiration logic: ttl = 0 : not expired; ttl < 0 : immediate expired after use; ttl > 0 : timeout expired;

Example
package main

import (
	"database/sql"
	"fmt"
	"time"

	"github.com/gogf/gf/v2/container/gpool"
)

func main() {
	type DBConn struct {
		Conn *sql.Conn
	}

	dbConnPool := gpool.New(time.Hour,
		func() (any, error) {
			dbConn := new(DBConn)
			return dbConn, nil
		},
		func(i any) {
			// sample : close db conn
			// i.(DBConn).Conn.Close()
		})

	fmt.Println(dbConnPool.TTL)

}
Output:

1h0m0s

func (*Pool) Clear

func (p *Pool) Clear()

Clear clears pool, which means it will remove all items from pool.

Example
package main

import (
	"database/sql"
	"fmt"
	"time"

	"github.com/gogf/gf/v2/container/gpool"
)

func main() {
	type DBConn struct {
		Conn  *sql.Conn
		Limit int
	}

	dbConnPool := gpool.New(time.Hour,
		func() (any, error) {
			dbConn := new(DBConn)
			dbConn.Limit = 10
			return dbConn, nil
		},
		func(i any) {
			i.(*DBConn).Limit = 0
			// sample : close db conn
			// i.(DBConn).Conn.Close()
		})

	conn, _ := dbConnPool.Get()
	dbConnPool.MustPut(conn)
	dbConnPool.MustPut(conn)
	fmt.Println(dbConnPool.Size())
	dbConnPool.Clear()
	fmt.Println(dbConnPool.Size())

}
Output:

2
0

func (*Pool) Close

func (p *Pool) Close()

Close closes the pool. If `p` has ExpireFunc, then it automatically closes all items using this function before it's closed. Commonly you do not need to call this function manually.

Example
package main

import (
	"database/sql"
	"fmt"
	"time"

	"github.com/gogf/gf/v2/container/gpool"
)

func main() {
	type DBConn struct {
		Conn  *sql.Conn
		Limit int
	}
	var (
		newFunc = func() (any, error) {
			dbConn := new(DBConn)
			dbConn.Limit = 10
			return dbConn, nil
		}
		closeFunc = func(i any) {
			fmt.Println("Close The Pool")
			// sample : close db conn
			// i.(DBConn).Conn.Close()
		}
	)
	dbConnPool := gpool.New(time.Hour, newFunc, closeFunc)

	conn, _ := dbConnPool.Get()
	dbConnPool.MustPut(conn)

	dbConnPool.Close()

	// wait for pool close
	time.Sleep(time.Second * 1)

	// May Output:
	// Close The Pool
}

func (*Pool) Get

func (p *Pool) Get() (any, error)

Get picks and returns an item from pool. If the pool is empty and NewFunc is defined, it creates and returns one from NewFunc.

Example
package main

import (
	"database/sql"
	"fmt"
	"time"

	"github.com/gogf/gf/v2/container/gpool"
)

func main() {
	type DBConn struct {
		Conn  *sql.Conn
		Limit int
	}

	dbConnPool := gpool.New(time.Hour,
		func() (any, error) {
			dbConn := new(DBConn)
			dbConn.Limit = 10
			return dbConn, nil
		},
		func(i any) {
			// sample : close db conn
			// i.(DBConn).Conn.Close()
		})

	conn, err := dbConnPool.Get()
	if err == nil {
		fmt.Println(conn.(*DBConn).Limit)
	}

}
Output:

10

func (*Pool) MustPut added in v2.3.0

func (p *Pool) MustPut(value any)

MustPut puts an item to pool, it panics if any error occurs.

func (*Pool) Put

func (p *Pool) Put(value any) error

Put puts an item to pool.

Example
package main

import (
	"database/sql"
	"fmt"
	"time"

	"github.com/gogf/gf/v2/container/gpool"
)

func main() {
	type DBConn struct {
		Conn  *sql.Conn
		Limit int
	}

	dbConnPool := gpool.New(time.Hour,
		func() (any, error) {
			dbConn := new(DBConn)
			dbConn.Limit = 10
			return dbConn, nil
		},
		func(i any) {
			// sample : close db conn
			// i.(DBConn).Conn.Close()
		})

	// get db conn
	conn, _ := dbConnPool.Get()
	// modify this conn's limit
	conn.(*DBConn).Limit = 20

	// example : do same db operation
	// conn.(*DBConn).Conn.QueryContext(context.Background(), "select * from user")

	// put back conn
	dbConnPool.MustPut(conn)

	fmt.Println(conn.(*DBConn).Limit)

}
Output:

20

func (*Pool) Size

func (p *Pool) Size() int

Size returns the count of available items of pool.

Example
package main

import (
	"database/sql"
	"fmt"
	"time"

	"github.com/gogf/gf/v2/container/gpool"
)

func main() {
	type DBConn struct {
		Conn  *sql.Conn
		Limit int
	}

	dbConnPool := gpool.New(time.Hour,
		func() (any, error) {
			dbConn := new(DBConn)
			dbConn.Limit = 10
			return dbConn, nil
		},
		func(i any) {
			// sample : close db conn
			// i.(DBConn).Conn.Close()
		})

	conn, _ := dbConnPool.Get()
	fmt.Println(dbConnPool.Size())
	dbConnPool.MustPut(conn)
	dbConnPool.MustPut(conn)
	fmt.Println(dbConnPool.Size())

}
Output:

0
2

type TPool added in v2.9.6

type TPool[T any] struct {
	TTL     time.Duration     // Time To Live for pool items.
	NewFunc func() (T, error) // Callback function to create pool item.
	// ExpireFunc is the function for expired items destruction.
	// This function needs to be defined when the pool items
	// need to perform additional destruction operations.
	// Eg: net.Conn, os.File, etc.
	ExpireFunc func(T)
	// contains filtered or unexported fields
}

TPool is an Object-Reusable Pool.

func NewTPool added in v2.9.6

func NewTPool[T any](ttl time.Duration, newFunc TPoolNewFunc[T], expireFunc ...TPoolExpireFunc[T]) *TPool[T]

NewTPool creates and returns a new object pool. To ensure execution efficiency, the expiration time cannot be modified once it is set.

Note the expiration logic: ttl = 0 : not expired; ttl < 0 : immediate expired after use; ttl > 0 : timeout expired;

func (*TPool[T]) Clear added in v2.9.6

func (p *TPool[T]) Clear()

Clear clears pool, which means it will remove all items from pool.

func (*TPool[T]) Close added in v2.9.6

func (p *TPool[T]) Close()

Close closes the pool. If `p` has ExpireFunc, then it automatically closes all items using this function before it's closed. Commonly you do not need to call this function manually.

func (*TPool[T]) Get added in v2.9.6

func (p *TPool[T]) Get() (value T, err error)

Get picks and returns an item from pool. If the pool is empty and NewFunc is defined, it creates and returns one from NewFunc.

func (*TPool[T]) MustPut added in v2.9.6

func (p *TPool[T]) MustPut(value T)

MustPut puts an item to pool, it panics if any error occurs.

func (*TPool[T]) Put added in v2.9.6

func (p *TPool[T]) Put(value T) error

Put puts an item to pool.

func (*TPool[T]) Size added in v2.9.6

func (p *TPool[T]) Size() int

Size returns the count of available items of pool.

type TPoolExpireFunc added in v2.9.6

type TPoolExpireFunc[T any] func(T)

TPoolExpireFunc Destruction function for object.

type TPoolNewFunc added in v2.9.6

type TPoolNewFunc[T any] func() (T, error)

TPoolNewFunc Creation function for object.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL