go-commons-pool alternatives and similar packages
Based on the "Miscellaneous" category.
Alternatively, view go-commons-pool alternatives based on common mentions on social networks and blogs.
-
golang-standards/project-layout
Standard Go Project Layout -
go-formatter
A curated list of awesome Go frameworks, libraries and software -
archiver
Easily create & extract archives, and compress & decompress files of various formats -
ardanlabs/service
Starter code for writing web services in Go using Kubernetes. -
go-multierror
A Go (golang) package for representing a list of errors as a single error. -
modern-go-application
Modern Go Application example -
go-restful-api
An idiomatic Go REST API starter kit (boilerplate) following the SOLID principles and Clean Architecture -
ghorg
Quickly clone an entire org/users repositories into one directory - Supports GitHub, GitLab, Bitbucket, and more 🥚 -
xstrings
Implements string functions widely used in other languages but absent in Go. -
go-chat-bot
IRC, Slack, Telegram and RocketChat bot written in go -
go-shortid
Super short, fully unique, non-sequential and URL friendly Ids -
shoutrrr
Notification library for gophers and their furry friends. -
vfs for golang
Virtual filesystem library written in golang -
health
An easy to use, extensible health check library for Go applications. -
container
A lightweight yet powerful IoC dependency injection container for the Go programming language -
gountries
Gountries provides: Countries (ISO-3166-1), Country Subdivisions(ISO-3166-2), Currencies (ISO 4217), Geo Coordinates(ISO-6709) as well as translations, country borders and other stuff exposed as struct data. -
golang-templates/seed
Go application GitHub repository template. -
banner
An easy way to add useful startup banners into your Go applications -
go-starter
An opinionated production-ready SQL-/Swagger-first RESTful JSON API written in Go, highly integrated with VSCode DevContainers by allaboutapps. -
gotoprom
Type-safe Prometheus metrics builder library for golang -
antch
Antch, a fast, powerful and extensible web crawling & scraping framework for Go -
countries
Countries - ISO-639, ISO-3166 countries codes with subdivisions and names, ISO-4217 currency designators, ITU-T E.164 IDD phone codes, countries capitals, UN M.49 codes, IANA ccTLD countries domains, IOC/NOC and FIFA codes, VERY VERY FAST, compatible with Databases/JSON/BSON/GOB/XML/CSV, Emoji countries flags and currencies support, Unicode CLDR. -
healthcheck
An simple, easily extensible and concurrent health-check library for Go services
Access the most powerful time series database as a service
* Code Quality Rankings and insights are calculated and provided by Lumnify.
They vary from L1 to L5 with "L5" being the highest.
Do you think we are missing an alternative of go-commons-pool or a related project?
README
Go Commons Pool
The Go Commons Pool is a generic object pool for Golang, direct rewrite from Apache Commons Pool.
Features
- Support custom PooledObjectFactory.
- Rich pool configuration option, can precise control pooled object lifecycle. See ObjectPoolConfig.
- Pool LIFO (last in, first out) or FIFO (first in, first out)
- Pool cap config
- Pool object validate config
- Pool object borrow block and max waiting time config
- Pool object eviction config
- Pool object abandon config
Pool Configuration Option
Configuration option table, more detail description see ObjectPoolConfig
Option | Default | Description |
---|---|---|
LIFO | true | If pool is LIFO (last in, first out) |
MaxTotal | 8 | The cap of pool |
MaxIdle | 8 | Max "idle" instances in the pool |
MinIdle | 0 | Min "idle" instances in the pool |
TestOnCreate | false | Validate when object is created |
TestOnBorrow | false | Validate when object is borrowed |
TestOnReturn | false | Validate when object is returned |
TestWhileIdle | false | Validate when object is idle, see TimeBetweenEvictionRuns |
BlockWhenExhausted | true | Whether to block when the pool is exhausted |
MinEvictableIdleTime | 30m | Eviction configuration,see DefaultEvictionPolicy |
SoftMinEvictableIdleTime | math.MaxInt64 | Eviction configuration,see DefaultEvictionPolicy |
NumTestsPerEvictionRun | 3 | The maximum number of objects to examine during each run evictor goroutine |
TimeBetweenEvictionRuns | 0 | The number of milliseconds to sleep between runs of the evictor goroutine, less than 1 mean not run |
Usage
Use Simple Factory
import (
"context"
"fmt"
"strconv"
"sync/atomic"
"github.com/jolestar/go-commons-pool/v2"
)
func Example_simple() {
type myPoolObject struct {
s string
}
v := uint64(0)
factory := pool.NewPooledObjectFactorySimple(
func(context.Context) (interface{}, error) {
return &myPoolObject{
s: strconv.FormatUint(atomic.AddUint64(&v, 1), 10),
},
nil
})
ctx := context.Background()
p := pool.NewObjectPoolWithDefaultConfig(ctx, factory)
obj, err := p.BorrowObject(ctx)
if err != nil {
panic(err)
}
o := obj.(*myPoolObject)
fmt.Println(o.s)
err = p.ReturnObject(ctx, obj)
if err != nil {
panic(err)
}
// Output: 1
}
Use Custom Factory
import (
"context"
"fmt"
"strconv"
"sync/atomic"
"github.com/jolestar/go-commons-pool/v2"
)
type MyPoolObject struct {
s string
}
type MyCustomFactory struct {
v uint64
}
func (f *MyCustomFactory) MakeObject(ctx context.Context) (*pool.PooledObject, error) {
return pool.NewPooledObject(
&MyPoolObject{
s: strconv.FormatUint(atomic.AddUint64(&f.v, 1), 10),
}),
nil
}
func (f *MyCustomFactory) DestroyObject(ctx context.Context, object *pool.PooledObject) error {
// do destroy
return nil
}
func (f *MyCustomFactory) ValidateObject(ctx context.Context, object *pool.PooledObject) bool {
// do validate
return true
}
func (f *MyCustomFactory) ActivateObject(ctx context.Context, object *pool.PooledObject) error {
// do activate
return nil
}
func (f *MyCustomFactory) PassivateObject(ctx context.Context, object *pool.PooledObject) error {
// do passivate
return nil
}
func Example_customFactory() {
ctx := context.Background()
p := pool.NewObjectPoolWithDefaultConfig(ctx, &MyCustomFactory{})
p.Config.MaxTotal = 100
obj1, err := p.BorrowObject(ctx)
if err != nil {
panic(err)
}
o := obj1.(*MyPoolObject)
fmt.Println(o.s)
err = p.ReturnObject(ctx, obj1)
if err != nil {
panic(err)
}
// Output: 1
}
For more examples please see pool_test.go
and example_simple_test.go
, example_customFactory_test.go
.
Note
PooledObjectFactory.MakeObject must return a pointer, not value. The following code will complain error.
p := pool.NewObjectPoolWithDefaultConfig(ctx, pool.NewPooledObjectFactorySimple(
func(context.Context) (interface{}, error) {
return "hello", nil
}))
obj, _ := p.BorrowObject()
p.ReturnObject(obj)
The right way is:
p := pool.NewObjectPoolWithDefaultConfig(ctx, pool.NewPooledObjectFactorySimple(
func(context.Context) (interface{}, error) {
s := "hello"
return &s, nil
}))
For more examples please see example_simple_test.go
.
Dependency
- testify for test
PerformanceTest
The results of running the pool_perf_test is almost equal to the java version PerformanceTest
go test --perf=true
For Apache commons pool user
- Direct use pool.Config.xxx to change pool config
- Default config value is same as java version
- If TimeBetweenEvictionRuns changed after ObjectPool created, should call ObjectPool.StartEvictor to take effect. Java version do this on set method.
- No KeyedObjectPool (TODO)
- No ProxiedObjectPool
- No pool stats (TODO)
FAQ
How to contribute
- Choose one open issue you want to solve, if not create one and describe what you want to change.
- Fork the repository on GitHub.
- Write code to solve the issue.
- Create PR and link to the issue.
- Make sure test and coverage pass.
- Wait maintainers to merge.
中文文档
License
Go Commons Pool is available under the Apache License, Version 2.0.
*Note that all licence references and agreements mentioned in the go-commons-pool README section above
are relevant to that project's source code only.