https://github.com/oliwer/thc
Go HTTP client with metrics and circuit breaker
https://github.com/oliwer/thc
circuit-breaker go golang http metrics monitoring
Last synced: 7 days ago
JSON representation
Go HTTP client with metrics and circuit breaker
- Host: GitHub
- URL: https://github.com/oliwer/thc
- Owner: oliwer
- License: mit
- Created: 2018-03-23T11:23:20.000Z (almost 8 years ago)
- Default Branch: master
- Last Pushed: 2018-06-06T15:22:45.000Z (over 7 years ago)
- Last Synced: 2024-06-20T13:28:16.098Z (over 1 year ago)
- Topics: circuit-breaker, go, golang, http, metrics, monitoring
- Language: Go
- Size: 14.6 KB
- Stars: 3
- Watchers: 4
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
THC - Timed HTTP Client for Go
==============================
[](https://godoc.org/github.com/oliwer/thc)
[](https://travis-ci.org/oliwer/thc)
[](https://goreportcard.com/report/oliwer/thc)
THC is a thin wrapper around Go's `http.Client` package which provides the following extra features.
## Metrics
THC exports metrics of your requests using `expvar`. You can observe average times for DNS lookups,
TLS handshakes, TCP sessions and more. Look at [the documentation](https://godoc.org/github.com/oliwer/thc)
for a list of exported metrics.
## Circuit breaker
After a defined number of consecutive failures, THC will switch to an *out of service* state.
In this state, the client will stop sending HTTP requests and instead will return the error
`ErrOutOfService`. It is up to the application to decide what to do in that case. After a
predefined amount of time, the service will be restores and THC will resume to work normally.
# Example
```go
package main
import (
"net/http"
"time"
"github.com/oliwer/thc"
)
var client = &thc.THC{
Client: &http.Client{Timeout: 100 * time.Millisecond},
Name: "example",
MaxErrors: 10,
HealingTime: 20 * time.Second,
}
func init() {
client.PublishExpvar()
}
func main() {
for {
resp, err := client.Get("https://example.com/thing.json")
if err == thc.ErrOutOfService {
// The service is down for 20s. (HealingTime)
}
if err != nil {
// There was an error but we are still OK because
// still under MaxErrors consecutive errors.
}
// Process resp normally...
}
}
```
# Notes
THC requires Go v1.8 or above. It is thread-safe and lock-less.