Update example usage in README.md
This commit is contained in:
@@ -22,31 +22,45 @@ own.
|
|||||||
## Example Usage
|
## Example Usage
|
||||||
|
|
||||||
```go
|
```go
|
||||||
constructor := func(context.Context) (net.Conn, error) {
|
package main
|
||||||
return net.Dial("tcp", "127.0.0.1:8080")
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"log"
|
||||||
|
"net"
|
||||||
|
|
||||||
|
"github.com/jackc/puddle/v2"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
constructor := func(context.Context) (net.Conn, error) {
|
||||||
|
return net.Dial("tcp", "127.0.0.1:8080")
|
||||||
|
}
|
||||||
|
destructor := func(value net.Conn) {
|
||||||
|
value.Close()
|
||||||
|
}
|
||||||
|
maxPoolSize := int32(10)
|
||||||
|
|
||||||
|
pool, err := puddle.NewPool(&puddle.Config[net.Conn]{Constructor: constructor, Destructor: destructor, MaxSize: maxPoolSize})
|
||||||
|
if err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Acquire resource from the pool.
|
||||||
|
res, err := pool.Acquire(context.Background())
|
||||||
|
if err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Use resource.
|
||||||
|
_, err = res.Value().Write([]byte{1})
|
||||||
|
if err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Release when done.
|
||||||
|
res.Release()
|
||||||
}
|
}
|
||||||
destructor := func(value net.Conn) {
|
|
||||||
value.Close()
|
|
||||||
}
|
|
||||||
maxPoolSize := 10
|
|
||||||
|
|
||||||
pool := puddle.NewPool[net.Conn](&puddle.Config[int]{Constructor: constructor, Destructor: destructor, MaxSize: maxPoolSize})
|
|
||||||
|
|
||||||
// Acquire resource from the pool.
|
|
||||||
res, err := pool.Acquire(context.Background())
|
|
||||||
if err != nil {
|
|
||||||
// ...
|
|
||||||
}
|
|
||||||
|
|
||||||
// Use resource.
|
|
||||||
_, err = res.Value().Write([]byte{1})
|
|
||||||
if err != nil {
|
|
||||||
// ...
|
|
||||||
}
|
|
||||||
|
|
||||||
// Release when done.
|
|
||||||
res.Release()
|
|
||||||
|
|
||||||
```
|
```
|
||||||
|
|
||||||
## Status
|
## Status
|
||||||
|
|||||||
Reference in New Issue
Block a user