Skip to content

resilient-http/resilient.go

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

30 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

resilient.go Build Status GoDoc Go Report Card

resilient HTTP client for Go built on top of gentleman HTTP toolkit.

Designed for distributed systems with fault tolerance capabilities.

Still beta.

Features

  • Simple, expressive, fuent API.
  • Plugin driven architecture.
  • Middleware oriented.
  • Built on top of net/http package.
  • Retry strategies supporting exponential or constant back off.
  • Service discovery capabitilies via Consul (in progress).

Installation

go get -u gopkg.in/resilient-http/resilient.go.v0

Example

package main

import (
  "fmt"
  "gopkg.in/resilient-http/resilient.go.v0"
  "gopkg.in/h2non/gentleman-consul.v0"
)

func main() {
  // Create a new constant retry client
  cli := resilient.NewRetryClient()
  
  // Use Consul for service discovery
  cli.Use(consul.New(consul.NewConfig("demo.consul.io", "web")))

  // Define base URL
  cli.URL("http://httpbin.org")

  // Create a new request based on the current client
  req := cli.Request()

  // Define the URL path at request level
  req.Path("/status/503")

  // Set a new header field
  req.SetHeader("Client", "gentleman")

  // Perform the request
  res, err := req.Send()
  if err != nil {
    fmt.Printf("Request error: %s\n", err)
    return
  }
  if !res.Ok {
    fmt.Printf("Invalid server response: %d\n", res.StatusCode)
    return
  }

  // Print response status and body as string
  fmt.Printf("Status: %d\n", res.StatusCode)
  fmt.Printf("Body: %s\n", res.String())
}

Custom retry strategy

package main

import (
  "fmt"
  "gopkg.in/resilient-http/resilient.go.v0"
  "gopkg.in/eapache/go-resiliency.v1/retrier"
)

func main() {
  // Create a simple client
  cli := resilient.New()

  // Define base URL
  cli.URL("http://httpbin.org")

  // Register the retry plugin, using a custom exponential retry strategy
  cli.Use(retry.New(retrier.New(retrier.ExponentialBackoff(3, 100*time.Millisecond), nil)))

  // Create a new request based on the current client
  req := cli.Request()

  // Define the URL path at request level
  req.Path("/status/503")

  // Set a new header field
  req.SetHeader("Client", "gentleman")

  // Perform the request
  res, err := req.Send()
  if err != nil {
    fmt.Printf("Request error: %s\n", err)
    return
  }
  if !res.Ok {
    fmt.Printf("Invalid server response: %d\n", res.StatusCode)
    return
  }

  // Print response status and body as string
  fmt.Printf("Status: %d\n", res.StatusCode)
  fmt.Printf("Body: %s\n", res.String())
}

License

MIT - Tomas Aparicio

About

Resilient HTTP client for Go built on top of gentleman toolkit

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages