https://github.com/ewohltman/pool
Go library that wraps http.Client to provide seamless higher-level connection pooling features
https://github.com/ewohltman/pool
connection-pool go golang pool pooling
Last synced: 10 months ago
JSON representation
Go library that wraps http.Client to provide seamless higher-level connection pooling features
- Host: GitHub
- URL: https://github.com/ewohltman/pool
- Owner: ewohltman
- License: mit
- Created: 2017-07-13T01:15:34.000Z (almost 9 years ago)
- Default Branch: master
- Last Pushed: 2018-10-28T23:05:50.000Z (over 7 years ago)
- Last Synced: 2024-06-19T06:53:56.802Z (almost 2 years ago)
- Topics: connection-pool, go, golang, pool, pooling
- Language: Go
- Homepage:
- Size: 10.7 KB
- Stars: 42
- Watchers: 3
- Forks: 5
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# pool - An HTTP Client with autonomous connection pooling and rate limiting
[](https://godoc.org/github.com/ewohltman/pool)
[](https://goreportcard.com/report/github.com/ewohltman/pool)
[](https://travis-ci.org/ewohltman/pool)
`pool` wraps a standard `*http.Client` to add the ability to put a maximum number of connections in the pool for the client and the requests-per-second it can perform requests at.
It 'overloads' the `http.Client.Do(req *http.Request) (*http.Response, error)` method to implement the extended functionality. By doing so, existing codebases do not need to heavily re-factor how they already do their logic to see the effect of the more finely-tuned client.
Functions that take in a `pool.Client` allow for the ability to take in either a `*pool.PClient` or an `*http.Client` (since they both implement `Do(req *http.Request) (*http.Response, error)`). The function must operate on the argument's Do method since it will satisfy the interface for all types passed into it. See `doPoolTest(client Client) error` in pool_test.go for an example.
## Installation
- - -
`go get -u github.com/ewohltman/pool`
## Usage
- - -
```go
// Some other examples are in pool_test.go
package main
import (
"fmt"
"io/ioutil"
"net/http"
"net/url"
"os"
"github.com/ewohltman/pool"
)
func main() {
standardLibClient := &http.Client{}
pooledClient := pool.NewPClient(standardLibClient, 25, 200)
urlString := "https://yourFavoriteWebsite.com/"
reqURL, err := url.Parse(urlString)
if err != nil {
fmt.Printf("[ERROR] Unable to parse: %s", urlString)
os.Exit(1)
}
resp, err := pooledClient.Do(&http.Request{URL: reqURL})
if err != nil {
fmt.Printf("[ERROR] Unable to perform request: %s", err)
os.Exit(2)
}
defer resp.Body.Close()
_, err = ioutil.ReadAll(resp.Body)
if err != nil {
fmt.Printf("[ERROR] Unable to read response body: %s", err)
os.Exit(3)
}
}
```