Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/henrywhitaker3/pool
Generic golang connection pool
https://github.com/henrywhitaker3/pool
Last synced: about 2 months ago
JSON representation
Generic golang connection pool
- Host: GitHub
- URL: https://github.com/henrywhitaker3/pool
- Owner: henrywhitaker3
- Created: 2024-08-06T20:26:07.000Z (5 months ago)
- Default Branch: main
- Last Pushed: 2024-08-24T21:00:06.000Z (5 months ago)
- Last Synced: 2024-08-24T22:18:46.617Z (5 months ago)
- Language: Go
- Size: 23.4 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Pool
A generic connection pool for go.
## Usage
### Installation
```
go get github.com/henrywhitaker3/pool
```### Creating a pool
To create a pool, you connection item must implement the `Connection` interface:
```go
type Connection interface{
Close() error
}
```You can then create a new pool:
```go
import (
"fmt"
"time""github.com/henrywhitaker3/pool"
)type conn struct{}
func (c *conn) Hello() {
fmt.Println("hello!")
}func (c *conn) Close() error {
return nil
}func main() {
pool, err := pool.New(pool.PoolOptions[*conn]{
Connect: func() (*conn, error) {
return &conn{}, nil
},
Connections: 5,
ProbeInterval: time.Second,
})
if err != nil {
panic(err)
}conn, ok := pool.Next()
if !ok {
panic("no connection in pool")
}
conn.Hello()
}
```### Use your own logger
By default, a pool will use the builtin `log` package to print to stdout, you can pass your own logger:
```go
pool.PoolOptions[*conn]{
Logger: myLogger,
}
```### Metrics
You can provide prometheus metrics to the pool:
```go
pool.PoolOptions[*conn]{
Metrics: &pool.PoolMetrics{
// The number of healthy connections in the pool
Connections: connectionsGauge,
// The number of errors when creating a new pool connection
ConnectionErrors: connectionErrorCounter,
}
}
```