{"id":13413496,"url":"https://github.com/jolestar/go-commons-pool","last_synced_at":"2025-05-14T18:04:44.320Z","repository":{"id":38206239,"uuid":"48696240","full_name":"jolestar/go-commons-pool","owner":"jolestar","description":"a generic object pool for golang","archived":false,"fork":false,"pushed_at":"2023-05-30T21:57:20.000Z","size":407,"stargazers_count":1236,"open_issues_count":4,"forks_count":150,"subscribers_count":51,"default_branch":"master","last_synced_at":"2025-04-13T10:55:08.334Z","etag":null,"topics":["commons-pool","golang","objectpool"],"latest_commit_sha":null,"homepage":"","language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/jolestar.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null}},"created_at":"2015-12-28T14:26:23.000Z","updated_at":"2025-04-11T10:03:26.000Z","dependencies_parsed_at":"2024-01-08T15:34:44.636Z","dependency_job_id":null,"html_url":"https://github.com/jolestar/go-commons-pool","commit_stats":{"total_commits":151,"total_committers":9,"mean_commits":16.77777777777778,"dds":0.304635761589404,"last_synced_commit":"6feea533b762bd807fec7a2cd9d23a8255dd4718"},"previous_names":[],"tags_count":9,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jolestar%2Fgo-commons-pool","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jolestar%2Fgo-commons-pool/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jolestar%2Fgo-commons-pool/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jolestar%2Fgo-commons-pool/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/jolestar","download_url":"https://codeload.github.com/jolestar/go-commons-pool/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":254198453,"owners_count":22030964,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["commons-pool","golang","objectpool"],"created_at":"2024-07-30T20:01:41.683Z","updated_at":"2025-05-14T18:04:39.310Z","avatar_url":"https://github.com/jolestar.png","language":"Go","funding_links":[],"categories":["Misc","Miscellaneous","杂项","Go","\u003cspan id=\"其他-miscellaneous\"\u003e其他 Miscellaneous\u003c/span\u003e","Microsoft Office","其他杂项","其他","Uncategorized"],"sub_categories":["Uncategorized","Advanced Console UIs","未分类的","\u003cspan id=\"高级控制台用户界面-advanced-console-uis\"\u003e高级控制台用户界面 Advanced Console UIs\u003c/span\u003e","暂未分类","交流","Strings","暂未分类这些库被放在这里是因为其他类别似乎都不适合。"],"readme":"Go Commons Pool\n=====\n\n[![Build Status](https://github.com/jolestar/go-commons-pool/workflows/Build/badge.svg)](https://github.com/jolestar/go-commons-pool/actions)\n[![CodeCov](https://codecov.io/gh/jolestar/go-commons-pool/branch/master/graph/badge.svg)](https://codecov.io/gh/jolestar/go-commons-pool)\n[![Go Report Card](https://goreportcard.com/badge/github.com/jolestar/go-commons-pool)](https://goreportcard.com/report/github.com/jolestar/go-commons-pool)\n[![GoDoc](https://godoc.org/github.com/jolestar/go-commons-pool?status.svg)](https://godoc.org/github.com/jolestar/go-commons-pool)\n\nThe Go Commons Pool is a generic object pool for [Golang](https://golang.org/), direct rewrite from [Apache Commons Pool](https://commons.apache.org/proper/commons-pool/).\n\nFeatures\n-------\n\n1. Support custom [PooledObjectFactory](https://godoc.org/github.com/jolestar/go-commons-pool#PooledObjectFactory).\n1. Rich pool configuration option, can precise control pooled object lifecycle. See [ObjectPoolConfig](https://godoc.org/github.com/jolestar/go-commons-pool#ObjectPoolConfig).\n    * Pool LIFO (last in, first out) or FIFO (first in, first out)\n    * Pool cap config\n    * Pool object validate config\n    * Pool object borrow block and max waiting time config\n    * Pool object eviction config\n    * Pool object abandon config\n\nPool Configuration Option\n-------\n\nConfiguration option table, more detail description see [ObjectPoolConfig](https://godoc.org/github.com/jolestar/go-commons-pool#ObjectPoolConfig)\n\n| Option                        | Default        | Description  |\n| ------------------------------|:--------------:| :------------|\n| LIFO                          | true           |If pool is LIFO (last in, first out)|\n| MaxTotal                      | 8              |The cap of pool|\n| MaxIdle                       | 8              |Max \"idle\" instances in the pool |\n| MinIdle                       | 0              |Min \"idle\" instances in the pool |\n| TestOnCreate                  | false          |Validate when object is created|\n| TestOnBorrow                  | false          |Validate when object is borrowed|\n| TestOnReturn                  | false          |Validate when object is returned|\n| TestWhileIdle                 | false          |Validate when object is idle, see TimeBetweenEvictionRuns |\n| BlockWhenExhausted            | true           |Whether to block when the pool is exhausted  |\n| MinEvictableIdleTime          | 30m            |Eviction configuration,see DefaultEvictionPolicy |\n| SoftMinEvictableIdleTime      | math.MaxInt64  |Eviction configuration,see DefaultEvictionPolicy  |\n| NumTestsPerEvictionRun        | 3              |The maximum number of objects to examine during each run evictor goroutine |\n| TimeBetweenEvictionRuns       | 0              |The number of milliseconds to sleep between runs of the evictor goroutine, less than 1 mean not run |\n\nUsage\n-------\n\n### Use Simple Factory\n\n```go\nimport (\n\t\"context\"\n\t\"fmt\"\n\t\"strconv\"\n\t\"sync/atomic\"\n\n\t\"github.com/jolestar/go-commons-pool/v2\"\n)\n\nfunc Example_simple() {\n\ttype myPoolObject struct {\n\t\ts string\n\t}\n\n\tv := uint64(0)\n\tfactory := pool.NewPooledObjectFactorySimple(\n\t\tfunc(context.Context) (interface{}, error) {\n\t\t\treturn \u0026myPoolObject{\n\t\t\t\t\ts: strconv.FormatUint(atomic.AddUint64(\u0026v, 1), 10),\n\t\t\t\t},\n\t\t\t\tnil\n\t\t})\n\n\tctx := context.Background()\n\tp := pool.NewObjectPoolWithDefaultConfig(ctx, factory)\n\n\tobj, err := p.BorrowObject(ctx)\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\n\to := obj.(*myPoolObject)\n\tfmt.Println(o.s)\n\n\terr = p.ReturnObject(ctx, obj)\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\n\t// Output: 1\n}\n```\n\n### Use Custom Factory\n\n```go\nimport (\n\t\"context\"\n\t\"fmt\"\n\t\"strconv\"\n\t\"sync/atomic\"\n\n\t\"github.com/jolestar/go-commons-pool/v2\"\n)\n\ntype MyPoolObject struct {\n\ts string\n}\n\ntype MyCustomFactory struct {\n\tv uint64\n}\n\nfunc (f *MyCustomFactory) MakeObject(ctx context.Context) (*pool.PooledObject, error) {\n\treturn pool.NewPooledObject(\n\t\t\t\u0026MyPoolObject{\n\t\t\t\ts: strconv.FormatUint(atomic.AddUint64(\u0026f.v, 1), 10),\n\t\t\t}),\n\t\tnil\n}\n\nfunc (f *MyCustomFactory) DestroyObject(ctx context.Context, object *pool.PooledObject) error {\n\t// do destroy\n\treturn nil\n}\n\nfunc (f *MyCustomFactory) ValidateObject(ctx context.Context, object *pool.PooledObject) bool {\n\t// do validate\n\treturn true\n}\n\nfunc (f *MyCustomFactory) ActivateObject(ctx context.Context, object *pool.PooledObject) error {\n\t// do activate\n\treturn nil\n}\n\nfunc (f *MyCustomFactory) PassivateObject(ctx context.Context, object *pool.PooledObject) error {\n\t// do passivate\n\treturn nil\n}\n\nfunc Example_customFactory() {\n\tctx := context.Background()\n\tp := pool.NewObjectPoolWithDefaultConfig(ctx, \u0026MyCustomFactory{})\n\tp.Config.MaxTotal = 100\n    \n\tobj1, err := p.BorrowObject(ctx)\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\n\to := obj1.(*MyPoolObject)\n\tfmt.Println(o.s)\n\n\terr = p.ReturnObject(ctx, obj1)\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\n\t// Output: 1\n}\n```\n\nFor more examples please see `pool_test.go` and `example_simple_test.go`, `example_customFactory_test.go`.\n\nNote\n-------\n\nPooledObjectFactory.MakeObject must return a pointer, not value.\nThe following code will complain error.\n\n```golang\np := pool.NewObjectPoolWithDefaultConfig(ctx, pool.NewPooledObjectFactorySimple(\n    func(context.Context) (interface{}, error) {\n        return \"hello\", nil\n    }))\nobj, _ := p.BorrowObject()\np.ReturnObject(obj)\n```\n\nThe right way is:\n\n```golang\np := pool.NewObjectPoolWithDefaultConfig(ctx, pool.NewPooledObjectFactorySimple(\n    func(context.Context) (interface{}, error) {\n        s := \"hello\"\n        return \u0026s, nil\n    }))\n```\n\nFor more examples please see `example_simple_test.go`.\n\nDependency\n-------\n\n* [testify](https://github.com/stretchr/testify) for test\n\nPerformanceTest\n-------\n\nThe results of running the pool_perf_test is almost equal to the java version [PerformanceTest](https://github.com/apache/commons-pool/blob/trunk/src/test/java/org/apache/commons/pool2/performance/PerformanceTest.java)\n\n    go test --perf=true\n\nFor Apache commons pool user\n-------\n\n* Direct use pool.Config.xxx to change pool config\n* Default config value is same as java version\n* If TimeBetweenEvictionRuns changed after ObjectPool created, should call  **ObjectPool.StartEvictor** to take effect. Java version do this on set method.\n* No KeyedObjectPool (TODO)\n* No ProxiedObjectPool\n* No pool stats (TODO)\n\nFAQ\n-------\n\n[FAQ](https://github.com/jolestar/go-commons-pool/wiki/FAQ)\n\nHow to contribute\n-------\n\n* Choose one open issue you want to solve, if not create one and describe what you want to change.\n* Fork the repository on GitHub.\n* Write code to solve the issue.\n* Create PR and link to the issue.\n* Make sure test and coverage pass.\n* Wait maintainers to merge.\n\n中文文档\n-------\n\n* [Go Commons Pool发布以及Golang多线程编程问题总结](http://jolestar.com/go-commons-pool-and-go-concurrent/)\n* [Go Commons Pool 1.0 发布](http://jolestar.com/go-commons-pool-v1-release/)\n\nLicense\n-------\n\nGo Commons Pool is available under the [Apache License, Version 2.0](https://www.apache.org/licenses/LICENSE-2.0.html).\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjolestar%2Fgo-commons-pool","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fjolestar%2Fgo-commons-pool","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjolestar%2Fgo-commons-pool/lists"}