Skip to content

Commit

Permalink
add documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
Soreing committed Feb 11, 2023
1 parent 867fff0 commit 6ecc385
Showing 1 changed file with 32 additions and 0 deletions.
32 changes: 32 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# 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()
```

0 comments on commit 6ecc385

Please sign in to comment.