Documentation
¶
Overview ¶
xpool is a type safe object pool build on top of sync.Pool
It is easy to use, just give a function that can create an object of a given type T as argument of New and it will return an implementation of Pool interface:
pool := xpool.New(func() io.ReadWriter {
return new(bytes.Buffer)
})
rw := pool.Get()
defer pool.Put(rw)
For monadic objects, when we need to reset the object to an initial state before put it back to the pool, there are two alternative constructors:
- NewWithResetter verify if the type T is a Resetter and call Reset() method.
- NewWithCustomResetter allow add a generic callback func(T) to perform some more complex operations, if needed.
Another alternative is to use https://github.com/peczenyj/xpool/monadic subpackage package.
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Pool ¶
type Pool[T any] interface { // Get fetch one item from object pool // If needed, will create another object. Get() T // Put return the object to the pull. // It may reset the object before put it back to sync pool. Put(object T) }
Pool is a type-safe object pool interface. This interface is parameterized on one generic types:
- T is reserved for the type of the object that will be stored on the pool.
For convenience, a pointer to sync.Pool is a Pool[any]
func New ¶
New is the constructor of an Pool for a given generic type T. Receives the constructor of the type T.
Example ¶
package main
import (
"bytes"
"fmt"
"io"
"os"
"github.com/peczenyj/xpool"
)
func main() {
// pool can infer T from constructor
pool := xpool.New(func() io.ReadWriter {
return new(bytes.Buffer)
})
rw := pool.Get()
defer pool.Put(rw)
// your favorite usage of rw
fmt.Fprint(rw, "example")
_, _ = io.Copy(os.Stdout, rw)
}
Output: example
func NewWithCustomResetter ¶ added in v0.5.0
NewWithDefaultResetter is an alternative constructor of an Pool for a given generic type T. We can specify a special resetter, to be called before return the object from the pool. Be careful, the custom resetter must be thread safe. Will panic if onPutResetter is nil.
Example ¶
package main
import (
"crypto/sha256"
"fmt"
"hash"
"github.com/peczenyj/xpool"
)
func main() {
// pool can infer T from constructor
var pool xpool.Pool[hash.Hash] = xpool.NewWithCustomResetter(sha256.New,
func(h hash.Hash) {
h.Reset()
fmt.Println("hash resetted with success")
},
)
var hasher hash.Hash = pool.Get() // get a new hash.Hash interface
_, _ = hasher.Write([]byte(`payload`))
fmt.Printf("%x\n", hasher.Sum(nil))
pool.Put(hasher) // reset it before put back to sync pool.
}
Output: 239f59ed55e737c77147cf55ad0c1b030b6d7ee748a7426952f9b852d5a935e5 hash resetted with success
func NewWithResetter ¶ added in v0.1.0
NewWithResetter is an alternative constructor of an Pool for a given generic type T. T must be a Resetter, before put the object back to object pool we will call Reset().
Example ¶
package main
import (
"crypto/sha256"
"fmt"
"hash"
"github.com/peczenyj/xpool"
)
func main() {
// pool can infer T from constructor
var pool xpool.Pool[hash.Hash] = xpool.NewWithResetter(sha256.New)
var hasher hash.Hash = pool.Get() // get a new hash.Hash interface
defer pool.Put(hasher) // reset it before put back to sync pool.
_, _ = hasher.Write([]byte(`payload`))
fmt.Printf("%x\n", hasher.Sum(nil))
}
Output: 239f59ed55e737c77147cf55ad0c1b030b6d7ee748a7426952f9b852d5a935e5