Convert Figma logo to code with AI

gojek logoheimdall

An enhanced HTTP client for Go

2,613
217
2,613
48

Top Related Projects

Netflix's Hystrix latency and fault tolerance library, for Go

Circuit Breaker implemented in Go

Resiliency patterns for golang

Circuit Breakers in Go

Quick Overview

Heimdall is an enhanced HTTP client for Go, offering features like circuit breaking, retries, and timeouts. It aims to make HTTP calls more resilient and fault-tolerant, particularly useful in distributed systems and microservices architectures.

Pros

  • Built-in circuit breaker functionality for improved fault tolerance
  • Configurable retry mechanisms with exponential backoff
  • Supports request timeouts and customizable backoff strategies
  • Easy integration with existing Go applications

Cons

  • Limited documentation and examples for advanced use cases
  • May introduce additional complexity for simple HTTP requests
  • Requires careful configuration to avoid unintended side effects
  • Not actively maintained (last commit over 2 years ago as of 2023)

Code Examples

Creating a basic Heimdall client:

import "github.com/gojek/heimdall/v7/httpclient"

timeout := 1000 * time.Millisecond
client := httpclient.NewClient(
    httpclient.WithHTTPTimeout(timeout),
    httpclient.WithRetryCount(2),
)

Making a GET request with the Heimdall client:

res, err := client.Get("https://api.example.com", nil)
if err != nil {
    // Handle error
}
defer res.Body.Close()
// Process response

Using the circuit breaker functionality:

import "github.com/gojek/heimdall/v7/hystrix"

client := hystrix.NewClient(
    hystrix.WithHTTPTimeout(1000 * time.Millisecond),
    hystrix.WithHystrixTimeout(1100 * time.Millisecond),
    hystrix.WithMaxConcurrentRequests(100),
    hystrix.WithErrorPercentThreshold(25),
)

Getting Started

To use Heimdall in your Go project:

  1. Install the package:

    go get -u github.com/gojek/heimdall/v7
    
  2. Import and use in your code:

    import "github.com/gojek/heimdall/v7/httpclient"
    
    client := httpclient.NewClient()
    res, err := client.Get("https://api.example.com", nil)
    if err != nil {
        // Handle error
    }
    // Process response
    
  3. Configure the client as needed, using the various options available in the package.

Competitor Comparisons

Netflix's Hystrix latency and fault tolerance library, for Go

Pros of hystrix-go

  • More mature and widely adopted in the Go community
  • Provides a comprehensive circuit breaker implementation with fallback mechanisms
  • Offers real-time metrics and monitoring capabilities

Cons of hystrix-go

  • Less actively maintained compared to Heimdall
  • Focused primarily on circuit breaking, lacking other HTTP client features
  • May require additional libraries for complete HTTP client functionality

Code Comparison

hystrix-go:

hystrix.Go("my_command", func() error {
    // Make HTTP request
    return nil
}, func(err error) error {
    // Fallback logic
    return nil
})

Heimdall:

client := heimdall.NewHTTPClient(timeout)
response, err := client.Get("http://example.com", nil)
if err != nil {
    // Handle error
}

Key Differences

  1. Focus: hystrix-go is primarily a circuit breaker library, while Heimdall is a full-featured HTTP client with built-in resiliency patterns.
  2. Features: Heimdall offers retries, circuit breaking, and timeouts in a single package, whereas hystrix-go focuses on circuit breaking.
  3. Ease of use: Heimdall provides a simpler API for making HTTP requests with resiliency, while hystrix-go requires more setup for similar functionality.
  4. Flexibility: hystrix-go can be used for various types of operations, not just HTTP requests, offering more versatility in certain scenarios.

Both libraries have their strengths, and the choice between them depends on specific project requirements and preferences.

Circuit Breaker implemented in Go

Pros of gobreaker

  • Lightweight and focused solely on circuit breaking functionality
  • Simple API with easy integration into existing Go projects
  • Supports custom state change hooks for advanced use cases

Cons of gobreaker

  • Limited to circuit breaking, lacking other HTTP client features
  • No built-in retry mechanism or timeout handling
  • Less actively maintained compared to Heimdall

Code Comparison

Heimdall (HTTP client with circuit breaker):

client := heimdall.NewHTTPClient(
    heimdall.WithHTTPTimeout(1000 * time.Millisecond),
    heimdall.WithRetryCount(2),
    heimdall.WithRetrier(heimdall.NewRetrier(heimdall.NewConstantBackoff(100*time.Millisecond))),
)

gobreaker (Circuit breaker only):

cb := gobreaker.NewCircuitBreaker(gobreaker.Settings{
    Name:        "my-circuit-breaker",
    MaxRequests: 3,
    Interval:    5 * time.Second,
    Timeout:     30 * time.Second,
})

Heimdall provides a more comprehensive HTTP client with built-in circuit breaking, retries, and timeouts. gobreaker focuses solely on circuit breaking functionality, requiring additional implementation for HTTP client features. Heimdall offers a higher-level abstraction, while gobreaker provides more granular control over circuit breaking behavior.

Resiliency patterns for golang

Pros of go-resiliency

  • Lightweight and focused on specific resiliency patterns
  • Provides more granular control over retry mechanisms
  • Easier to integrate into existing codebases due to its simplicity

Cons of go-resiliency

  • Less feature-rich compared to Heimdall
  • Lacks built-in HTTP client functionality
  • Doesn't provide out-of-the-box metrics or instrumentation

Code Comparison

go-resiliency (Retry example):

r := retry.New(3, 1*time.Second)
err := r.Run(func() error {
    // Your code here
    return nil
})

Heimdall (Retry example):

backoff := heimdall.NewConstantBackoff(500 * time.Millisecond)
retrier := heimdall.NewRetrier(backoff)
client := heimdall.NewHTTPClient(
    heimdall.WithHTTPTimeout(1000 * time.Millisecond),
    heimdall.WithRetrier(retrier),
    heimdall.WithRetryCount(3),
)

Both libraries offer resiliency patterns, but go-resiliency focuses on specific patterns like retries and circuit breakers, while Heimdall provides a more comprehensive HTTP client with built-in resiliency features. go-resiliency is simpler and more flexible, while Heimdall offers a higher-level abstraction with additional functionality out of the box.

Circuit Breakers in Go

Pros of circuitbreaker

  • Lightweight and simple implementation focused solely on circuit breaking
  • Written in Ruby, making it a natural choice for Ruby projects
  • Easy to integrate into existing Ruby applications

Cons of circuitbreaker

  • Limited features compared to Heimdall's more comprehensive HTTP client functionality
  • Less actively maintained, with fewer recent updates and contributions
  • Lacks built-in retries and timeouts, which are included in Heimdall

Code Comparison

circuitbreaker:

cb = CircuitBreaker.new do |cb|
  cb.timeout = 5
  cb.failure_threshold = 5
  cb.failure_timeout = 10
end

cb.execute { # Your code here }

Heimdall:

client := heimdall.NewHTTPClient(timeout)
client.SetRetryCount(2)
client.SetRetrier(heimdall.NewRetrier(heimdall.NewConstantBackoff(10*time.Millisecond)))

response, err := client.Get("http://example.com", nil)

Both libraries implement circuit breaking functionality, but Heimdall provides a more feature-rich HTTP client with built-in retries and timeouts. circuitbreaker focuses solely on the circuit breaking pattern, while Heimdall offers a broader set of tools for making resilient HTTP requests in Go applications.

Convert Figma logo designs to code with AI

Visual Copilot

Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.

Try Visual Copilot

README

Heimdall

Build Status

Description

Heimdall is an HTTP client that helps your application make a large number of requests, at scale. With Heimdall, you can:

  • Use a hystrix-like circuit breaker to control failing requests
  • Add synchronous in-memory retries to each request, with the option of setting your own retrier strategy
  • Create clients with different timeouts for every request

All HTTP methods are exposed as a fluent interface.

Installation

go get -u github.com/gojek/heimdall/v7

Usage

Importing the package

This package can be used by adding the following import statement to your .go files.

import "github.com/gojek/heimdall/v7/httpclient" 

Making a simple GET request

The below example will print the contents of the google home page:

// Create a new HTTP client with a default timeout
timeout := 1000 * time.Millisecond
client := httpclient.NewClient(httpclient.WithHTTPTimeout(timeout))

// Use the clients GET method to create and execute the request
res, err := client.Get("http://google.com", nil)
if err != nil{
	panic(err)
}

// Heimdall returns the standard *http.Response object
body, err := ioutil.ReadAll(res.Body)
fmt.Println(string(body))

You can also use the *http.Request object with the http.Do interface :

timeout := 1000 * time.Millisecond
client := httpclient.NewClient(httpclient.WithHTTPTimeout(timeout))

// Create an http.Request instance
req, _ := http.NewRequest(http.MethodGet, "http://google.com", nil)
// Call the `Do` method, which has a similar interface to the `http.Do` method
res, err := client.Do(req)
if err != nil {
	panic(err)
}

body, err := ioutil.ReadAll(res.Body)
fmt.Println(string(body))

Creating a hystrix-like circuit breaker

To import hystrix package of heimdall.

import "github.com/gojek/heimdall/v7/hystrix"

You can use the hystrix.NewClient function to create a client wrapped in a hystrix-like circuit breaker:

// Create a new hystrix-wrapped HTTP client with the command name, along with other required options
client := hystrix.NewClient(
	hystrix.WithHTTPTimeout(10 * time.Millisecond),
	hystrix.WithCommandName("google_get_request"),
	hystrix.WithHystrixTimeout(1000 * time.Millisecond),
	hystrix.WithMaxConcurrentRequests(30),
	hystrix.WithErrorPercentThreshold(20),
	hystrix.WithStatsDCollector("localhost:8125", "myapp.hystrix"),
)

// The rest is the same as the previous example

In the above example, there are two timeout values used: one for the hystrix configuration, and one for the HTTP client configuration. The former determines the time at which hystrix should register an error, while the latter determines when the client itself should return a timeout error. Unless you have any special requirements, both of these would have the same values.

You can choose to export hystrix metrics to a statsD collector with the hystrix.WithStatsDCollector(<statsd addr>, <metrics-prefix>) option when initializing the client as shown above.

Creating a hystrix-like circuit breaker with fallbacks

You can use the hystrix.NewClient function to create a client wrapped in a hystrix-like circuit breaker by passing in your own custom fallbacks:

The fallback function will trigger when your code returns an error, or whenever it is unable to complete based on a variety of health checks.

How your fallback function should look like you should pass in a function whose signature looks like following

func(err error) error {
    // your logic for handling the error/outage condition
    return err
}

Example

// Create a new fallback function
fallbackFn := func(err error) error {
    _, err := http.Post("post_to_channel_two")
    return err
}

timeout := 10 * time.Millisecond

// Create a new hystrix-wrapped HTTP client with the fallbackFunc as fall-back function
client := hystrix.NewClient(
	hystrix.WithHTTPTimeout(timeout),
	hystrix.WithCommandName("MyCommand"),
	hystrix.WithHystrixTimeout(1100 * time.Millisecond),
	hystrix.WithMaxConcurrentRequests(100),
	hystrix.WithErrorPercentThreshold(20),
	hystrix.WithSleepWindow(10),
	hystrix.WithRequestVolumeThreshold(10),
	hystrix.WithFallbackFunc(fallbackFn),
})

// The rest is the same as the previous example

In the above example, the fallbackFunc is a function which posts to channel two in case posting to channel one fails.

Creating an HTTP client with a retry mechanism

// First set a backoff mechanism. Constant backoff increases the backoff at a constant rate
backoffInterval := 2 * time.Millisecond
// Define a maximum jitter interval. It must be more than 1*time.Millisecond
maximumJitterInterval := 5 * time.Millisecond

backoff := heimdall.NewConstantBackoff(backoffInterval, maximumJitterInterval)

// Create a new retry mechanism with the backoff
retrier := heimdall.NewRetrier(backoff)

timeout := 1000 * time.Millisecond
// Create a new client, sets the retry mechanism, and the number of times you would like to retry
client := httpclient.NewClient(
	httpclient.WithHTTPTimeout(timeout),
	httpclient.WithRetrier(retrier),
	httpclient.WithRetryCount(4),
)

// The rest is the same as the first example

Or create client with exponential backoff

// First set a backoff mechanism. Exponential Backoff increases the backoff at a exponential rate

initalTimeout := 2*time.Millisecond            // Inital timeout
maxTimeout := 9*time.Millisecond               // Max time out
exponentFactor := 2                            // Multiplier
maximumJitterInterval := 2*time.Millisecond    // Max jitter interval. It must be more than 1*time.Millisecond

backoff := heimdall.NewExponentialBackoff(initalTimeout, maxTimeout, exponentFactor, maximumJitterInterval)

// Create a new retry mechanism with the backoff
retrier := heimdall.NewRetrier(backoff)

timeout := 1000 * time.Millisecond
// Create a new client, sets the retry mechanism, and the number of times you would like to retry
client := httpclient.NewClient(
	httpclient.WithHTTPTimeout(timeout),
	httpclient.WithRetrier(retrier),
	httpclient.WithRetryCount(4),
)

// The rest is the same as the first example

This will create an HTTP client which will retry every 500 milliseconds incase the request fails. The library also comes with an Exponential Backoff

Custom retry mechanisms

Heimdall supports custom retry strategies. To do this, you will have to implement the Backoff interface:

type Backoff interface {
	Next(retry int) time.Duration
}

Let's see an example of creating a client with a linearly increasing backoff time:

First, create the backoff mechanism:

type linearBackoff struct {
	backoffInterval int
}

func (lb *linearBackoff) Next(retry int) time.Duration{
	if retry <= 0 {
		return 0 * time.Millisecond
	}
	return time.Duration(retry * lb.backoffInterval) * time.Millisecond
}

This will create a backoff mechanism, where the retry time will increase linearly for each retry attempt. We can use this to create the client, just like the last example:

backoff := &linearBackoff{100}
retrier := heimdall.NewRetrier(backoff)

timeout := 1000 * time.Millisecond
// Create a new client, sets the retry mechanism, and the number of times you would like to retry
client := httpclient.NewClient(
	httpclient.WithHTTPTimeout(timeout),
	httpclient.WithRetrier(retrier),
	httpclient.WithRetryCount(4),
)

// The rest is the same as the first example

Heimdall also allows you to simply pass a function that returns the retry timeout. This can be used to create the client, like:

linearRetrier := NewRetrierFunc(func(retry int) time.Duration {
	if retry <= 0 {
		return 0 * time.Millisecond
	}
	return time.Duration(retry) * time.Millisecond
})

timeout := 1000 * time.Millisecond
client := httpclient.NewClient(
	httpclient.WithHTTPTimeout(timeout),
	httpclient.WithRetrier(linearRetrier),
	httpclient.WithRetryCount(4),
)

Custom HTTP clients

Heimdall supports custom HTTP clients. This is useful if you are using a client imported from another library and/or wish to implement custom logging, cookies, headers etc for each request that you make with your client.

Under the hood, the httpClient struct now accepts Doer, which is the standard interface implemented by HTTP clients (including the standard library's net/*http.Client)

Let's say we wish to add authorization headers to all our requests.

We can define our client myHTTPClient

type myHTTPClient struct {
	client http.Client
}

func (c *myHTTPClient) Do(request *http.Request) (*http.Response, error) {
	request.SetBasicAuth("username", "passwd")
	return c.client.Do(request)
}

And set this with httpclient.NewClient(httpclient.WithHTTPClient(&myHTTPClient{client: http.DefaultClient}))

Now, each sent request will have the Authorization header to use HTTP basic authentication with the provided username and password.

This can be done for the hystrix client as well

client := httpclient.NewClient(
	httpclient.WithHTTPClient(&myHTTPClient{
		client: http.Client{Timeout: 25 * time.Millisecond},
	}),
)

// The rest is the same as the first example

Plugins

To add a plugin to an existing client, use the AddPlugin method of the client.

An example, with the request logger plugin:

// import "github.com/gojek/heimdall/v7/plugins"

client := heimdall.NewHTTPClient(timeout)
requestLogger := plugins.NewRequestLogger(nil, nil)
client.AddPlugin(requestLogger)
// use the client as before

req, _ := http.NewRequest(http.MethodGet, "http://google.com", nil)
res, err := client.Do(req)
if err != nil {
	panic(err)
}
// This will log:
//23/Jun/2018 12:48:04 GET http://google.com 200 [412ms]
// to STDOUT

A plugin is an interface whose methods get called during key events in a requests lifecycle:

  • OnRequestStart is called just before the request is made
  • OnRequestEnd is called once the request has successfully executed
  • OnError is called is the request failed

Each method is called with the request object as an argument, with OnRequestEnd, and OnError additionally being called with the response and error instances respectively. For a simple example on how to write plugins, look at the request logger plugin.

Documentation

Further documentation can be found on pkg.go.dev

FAQ

Can I replace the standard Go HTTP client with Heimdall?

Yes, you can. Heimdall implements the standard HTTP Do method, along with useful wrapper methods that provide all the functionality that a regular Go HTTP client provides.


When should I use Heimdall?

If you are making a large number of HTTP requests, or if you make requests among multiple distributed nodes, and wish to make your systems more fault tolerant, then Heimdall was made for you.

Heimdall makes use of multiple mechanisms to make HTTP requests more fault tolerant:

  1. Retries - If a request fails, Heimdall retries behind the scenes, and returns the result if one of the retries are successful.
  2. Circuit breaking - If Heimdall detects that too many of your requests are failing, or that the number of requests sent are above a configured threshold, then it "opens the circuit" for a short period of time, which prevents any more requests from being made. This gives your downstream systems time to recover.

So does this mean that I shouldn't use Heimdall for small scale applications?

Although Heimdall was made keeping large scale systems in mind, it's interface is simple enough to be used for any type of systems. In fact, we use it for our pet projects as well. Even if you don't require retries or circuit breaking features, the simpler HTTP client provides sensible defaults with a simpler interface, and can be upgraded easily should the need arise.


Can I contribute to make Heimdall better?

Please do! We are looking for any kind of contribution to improve Heimdalls core funtionality and documentation. When in doubt, make a PR!

License

Copyright 2018-2020, GO-JEK Tech (http://gojek.tech)

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.