项目作者: oliwer

项目描述 :
Go HTTP client with metrics and circuit breaker
高级语言: Go
项目地址: git://github.com/oliwer/thc.git
创建时间: 2018-03-23T11:23:20Z
项目社区:https://github.com/oliwer/thc

开源协议:MIT License

下载


THC - Timed HTTP Client for Go

GoDoc
Build Status
Go Report Card

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
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

  1. package main
  2. import (
  3. "net/http"
  4. "time"
  5. "github.com/oliwer/thc"
  6. )
  7. var client = &thc.THC{
  8. Client: &http.Client{Timeout: 100 * time.Millisecond},
  9. Name: "example",
  10. MaxErrors: 10,
  11. HealingTime: 20 * time.Second,
  12. }
  13. func init() {
  14. client.PublishExpvar()
  15. }
  16. func main() {
  17. for {
  18. resp, err := client.Get("https://example.com/thing.json")
  19. if err == thc.ErrOutOfService {
  20. // The service is down for 20s. (HealingTime)
  21. }
  22. if err != nil {
  23. // There was an error but we are still OK because
  24. // still under MaxErrors consecutive errors.
  25. }
  26. // Process resp normally...
  27. }
  28. }

Notes

THC requires Go v1.8 or above. It is thread-safe and lock-less.