Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/soreing/grasp
Golang auto scaling resource pool
https://github.com/soreing/grasp
go golang pool pooling scaling
Last synced: 6 days ago
JSON representation
Golang auto scaling resource pool
- Host: GitHub
- URL: https://github.com/soreing/grasp
- Owner: Soreing
- Created: 2022-11-22T06:43:36.000Z (almost 2 years ago)
- Default Branch: main
- Last Pushed: 2023-02-11T12:40:11.000Z (over 1 year ago)
- Last Synced: 2024-10-09T10:04:01.781Z (28 days ago)
- Topics: go, golang, pool, pooling, scaling
- Language: Go
- Homepage:
- Size: 4.88 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Go Resource Auto Scaling Pool
Grasp is a resource pool which manages the creation and deletion of resources as needed after sudden increase and decrease in request for resources.## Usage
The resource pool can only work with objects that implement the Poolable interface. The interface defines how to release the object when it's been idle for too long.
```golang
type Item struct {
// Some fields
}
func (i *Item) PoolRelease() {
fmt.Println("released")
}
```To create a resource pool, you need to provide a TTL value for the maximum lifetime of idle resources, an upper limit for the number of resources in the pool, as well as a constructor that creates new Poolable resources.
```golang
pl := NewPool(5, time.Second, func() Poolable {
return &Item{/* fields or a constuctor*/}
})
```To get a resource from the pool you need to request for one. The `Acquire` function returns a resource, a done function, and an error. If no errors occured,the the `done` function must be called to release the resource when it is no longer needed.
If there are idle resources, the function returns one. If there are no idle resources, one will be created up to the limit. If there are no resources available, the call blocks until a resource is freed up or the context is canceled.
```golang
res, done, err := pl.Acquire(context.TODO())
```To stop using the pool, call the `Close` function, which cleans up all the resources and waits for all in use resources to be released before returning.
```golang
pl.Close()
```