Home

Awesome

gentleman-consul Build Status GoDoc Coverage Status Go Report Card

gentleman's v2 plugin for easy service discovery using Consul.

Provides transparent retry/backoff support for resilient and reactive HTTP client capabilities.
It also allows you to use custom retry strategies, such as constant or exponential retries.

Installation

go get -u gopkg.in/h2non/gentleman-consul.v2

Versions

API

See godoc reference for detailed API documentation.

Examples

See examples directory for featured examples.

Simple request

package main

import (
  "fmt"

  "gopkg.in/h2non/gentleman.v2"
	"gopkg.in/h2non/gentleman-consul.v2"
)

func main() {
  // Create a new client
  cli := gentleman.New()

  // Register Consul's plugin at client level
  cli.Use(consul.New(consul.NewConfig("demo.consul.io", "web")))

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

  // 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 info
  fmt.Printf("Server URL: %s\n", res.RawRequest.URL.String())
  fmt.Printf("Response status: %d\n", res.StatusCode)
  fmt.Printf("Server header: %s\n", res.Header.Get("Server"))
}

Custom retry strategy

package main

import (
  "fmt"
	"time"

	"gopkg.in/h2non/gentleman.v2"
	"gopkg.in/h2non/gentleman-consul.v1"
	"gopkg.in/eapache/go-resiliency.v1/retrier"
)

func main() {
  // Create a new client
  cli := gentleman.New()

  // Configure Consul plugin
  config := consul.NewConfig("demo.consul.io", "web")

  // Use a custom retrier strategy with max 10 retry attempts
  config.Retrier = retrier.New(retrier.ConstantBackoff(10, time.Duration(25*time.Millisecond)), nil)

  // Register Consul's plugin at client level
  cli.Use(consul.New(config))

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

  // 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 info
  fmt.Printf("Server URL: %s\n", res.RawRequest.URL.String())
  fmt.Printf("Response status: %d\n", res.StatusCode)
  fmt.Printf("Server header: %s\n", res.Header.Get("Server"))
}

License

MIT - Tomas Aparicio